okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Possibility to provide executor for TaskRunner for OkHttpClient

Open clayly opened this issue 2 years ago • 2 comments

It's possible to pass ExecutorService to Dispatcher, but looks like OkHttpClient still creates threads by itself, out of user's control, and i think it's happening in TaskRunner, which is created like this:

@JvmField
val INSTANCE = TaskRunner(RealBackend(threadFactory("$okHttpName TaskRunner", daemon = true)))

clayly avatar Jul 22 '22 21:07 clayly

I think this makes sense, we will need for Loom support anyway. It may not be a trivial API to configure. Places that are hardcoded.

https://github.com/square/okhttp/blob/master/okhttp/src/jvmMain/kotlin/okhttp3/Cache.kt#L156 https://github.com/square/okhttp/blob/master/okhttp/src/jvmMain/kotlin/okhttp3/ConnectionPool.kt#L41 https://github.com/square/okhttp/blob/master/okhttp/src/jvmMain/kotlin/okhttp3/OkHttpClient.kt#L233

yschimke avatar Jul 23 '22 09:07 yschimke

@swankjesse any objections? Or too early to make this change?

Specifically passing in a TaskRunner to all components, with a default to INSTANCE.

yschimke avatar Jul 23 '22 09:07 yschimke

@swankjesse is it a good point to consider ConnectionPool.Builder.threadPool(...) ? And a public setter for OkHttpClient.Builder.threadPool(...)

yschimke avatar Jan 03 '23 04:01 yschimke

Making these executor services user-accessible isn’t a clear win...

  • We have very specific expectations for how these are configured. A pool that’s oversized or undersized is going to yield incorrect behavior.
  • We don’t have code to recover from RejectedExcecutionException.
  • The TaskRunner pool is global, not per-OkHttpClient.

@clayly what problem are you attempting to solve here?

swankjesse avatar Jan 04 '23 15:01 swankjesse

@swankjesse Hi! I want to control amount of threads per application for maximum effectiveness / performance in microservices environment, for example Kubernetes at AWS. Another reason is uniformity and explicitness: all frameworks and libraries known to me allow users to control how these libraries and frameworks deal with system resources such as threads.

clayly avatar Jan 04 '23 16:01 clayly

@clayly gotcha, makes sense.

I’d like to hold off on this for now, as OkHttp’s use of threads is currently an implementation detail of the library. Any tuning parameters we offer today will likely work against us as we change the library.

swankjesse avatar Jan 04 '23 19:01 swankjesse

Closing as not a feature we are offering.

yschimke avatar May 21 '23 09:05 yschimke