简体   繁体   中英

Okio Throttler integration with OkHttp

My team is suffering from this issue with slack integration to upload files, so following the comments in that issue I would like to throttle the requests in our Kotlin implementation.

I am trying to integrate Okio Throttler within an OkHttp interceptor, so I have the setup:

val client = OkHttpClient.Builder()
            .retryOnConnectionFailure(false)
            .addInterceptor { chain ->
                val request = chain.request()
                val originalRequestBody = request.body
                val newRequest = if (originalRequestBody != null) {
                    val wrappedRequestBody = ThrottledRequestBody(originalRequestBody)
                    request.newBuilder()
                        .method(request.method, wrappedRequestBody)
                        .build()
                } else {
                    request
                }
                chain.proceed(newRequest)
            }
            .build()
class ThrottledRequestBody(private val delegate: RequestBody) : RequestBody() {
    private val throttler = Throttler().apply {
        bytesPerSecond(1024, 1024 * 4, 1024 * 8)
    }


    override fun contentType(): MediaType? {
        return delegate.contentType()
    }

    override fun writeTo(sink: BufferedSink) {
        delegate.writeTo(throttler.sink(sink).buffer())
    }
}

It seems throttler.sink returns a Sink , but a BufferedSink is required to the method delegate.writeTo , so I called buffer() to get that BufferedSink . Am I doing it wrong ? Is the call for .buffer() breaking the integration?

It's almost perfect. You just need to flush the buffer when you're done otherwise it'll finish with a few bytes inside.

override fun writeTo(sink: BufferedSink) {
  throttler.sink(sink).buffer().use {
    delegate.writeTo(it)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM