Is it possible to throttle bandwidth when using OkHttp?

不羁岁月 提交于 2021-02-07 14:47:29

问题


Is it possible when using OkHttp to throttle the bandwidth? (possibly using a network interceptor).


回答1:


You can make it work in two ways:

  1. Send request and read stream manually, and throttle while reading there.
  2. Add an Interceptor.

Using OkHttp the best way is Interceptor. There are also a few simple steps:

  1. To inherit the Interceptor interface.
  2. To inherit the ResponseBody class.
  3. In custom ResponceBody override fun source(): BufferedSource needs to return the BandwidthSource's buffer.

Example of BandwidthSource:

class BandwidthSource(
    source: Source,
    private val bandwidthLimit: Int
) : ForwardingSource(source) {

    private var time = getSeconds()

    override fun read(sink: Buffer, byteCount: Long): Long {
        val read = super.read(sink, byteCount)
        throttle(read)
        return read
    }

    private fun throttle(byteCount: Long) {
        val bitsCount = byteCount * BITS_IN_BYTE
        val currentTime = getSeconds()
        val timeDiff = currentTime - time
        if (timeDiff == 0L) {
            return
        }
        val kbps = bitsCount / timeDiff
        if (kbps > bandwidthLimit) {
            val times = (kbps / bandwidthLimit)
            if (times > 0) {
                runBlocking { delay(TimeUnit.SECONDS.toMillis(times)) }
            }
        }
        time = currentTime
    }

    private fun getSeconds(): Long {
        return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())
    }
}


来源:https://stackoverflow.com/questions/50913015/is-it-possible-to-throttle-bandwidth-when-using-okhttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!