1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-07 12:42:57 +03:00

Support minimum connection pool size (#8287)

This PR adds support for configuring the connection pool to proactively create connections to specified hosts. This is useful for "prewarming" the connection pool before it starts being used.

The main public API change is adding `ConnectionPool.setPolicy(Address, AddressPolicy)`. A policy specifies how many _concurrent streams_ should be supported by the pool (which might be satisfied by a single http/2 connection or by N http/1.1 connections).

The main internal change is adding a new task queue to `RealConnectionPool` which checks to see if it needs to create new connections to satisfy existing policies. This task is enqueued any time a policy changes, a connection is closed, or an http/2 connection's settings decrease the number of concurrent streams supported.
This commit is contained in:
Evan Nelson
2024-04-03 13:39:41 -07:00
committed by GitHub
parent 787d19439f
commit c9ba5d732b
12 changed files with 490 additions and 50 deletions

View File

@@ -35,10 +35,13 @@ import okhttp3.internal.RecordingOkAuthenticator
import okhttp3.internal.concurrent.TaskFaker
import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.connection.CallConnectionUser
import okhttp3.internal.connection.FastFallbackExchangeFinder
import okhttp3.internal.connection.RealCall
import okhttp3.internal.connection.RealConnection
import okhttp3.internal.connection.RealConnectionPool
import okhttp3.internal.connection.RealRoutePlanner
import okhttp3.internal.connection.RouteDatabase
import okhttp3.internal.connection.RoutePlanner
import okhttp3.internal.http.RealInterceptorChain
import okhttp3.internal.http.RecordingProxySelector
import okhttp3.tls.HandshakeCertificates
@@ -97,6 +100,7 @@ class TestValueFactory : Closeable {
fun newConnectionPool(
taskRunner: TaskRunner = this.taskRunner,
maxIdleConnections: Int = Int.MAX_VALUE,
routePlanner: RoutePlanner? = null,
): RealConnectionPool {
return RealConnectionPool(
taskRunner = taskRunner,
@@ -104,6 +108,25 @@ class TestValueFactory : Closeable {
keepAliveDuration = 100L,
timeUnit = TimeUnit.NANOSECONDS,
connectionListener = ConnectionListener.NONE,
exchangeFinderFactory = { pool, address, user ->
FastFallbackExchangeFinder(
routePlanner ?: RealRoutePlanner(
taskRunner = taskRunner,
connectionPool = pool,
readTimeoutMillis = 10_000,
writeTimeoutMillis = 10_000,
socketConnectTimeoutMillis = 10_000,
socketReadTimeoutMillis = 10_000,
pingIntervalMillis = 10_000,
retryOnConnectionFailure = false,
fastFallback = true,
address = address,
routeDatabase = RouteDatabase(),
connectionUser = user,
),
taskRunner,
)
},
)
}