scoop:scoop.http.client:0.1.0
src/pool/plan.scoop
Selected production source · src/pool/plan.scoop
1
package scoop.http.client3
import scoop.asyncio.IoClosedException4
import scoop.http.asyncio.Http2ConnectionDrainingException5
import scoop.platform.time.MonotonicTimeSource6
import scoop.time.Duration7
import scoop.time.TimeMark9
internal class ClientPoolPlan(val actions: Array<ClientPoolAction>, val delay: Duration?, val done: Boolean)11
internal fun planClientPool(state: ClientState): ClientPoolPlan {12
if (state.lock.withLock { state.closing }) return planClientPoolClose(state)13
val now = MonotonicTimeSource.markNow()14
return state.lock.withLock {15
state.dirty = false16
val actions = ArrayList<ClientPoolAction>()17
while (state.upgrades.size != 0L) actions.add(ClientPoolAction.Upgrade(state.upgrades.removeFirst()))18
maintainClientPool(state, now, actions)19
val pending = state.waiting.size20
var index = 0L21
while (index < pending) {22
val call = state.waiting.removeFirst()23
if (!state.closing && waitingClientCall(call)) {24
if (!assignClientLease(state, call, now, actions)) {25
reserveClientConnection(state, call, now, actions)26
state.waiting.addLast(call)27
}28
}29
index += 1L30
}31
maintainClientPool(state, now, actions)32
var delay: Duration? = None33
if (!state.closing) {34
for (entry in state.connections) {35
if (entry.connecting || entry.closing || entry.draining) continue36
if (entry.active == 0L && !clientPresent(entry.initial))37
delay = earlierClientDelay(delay, state.config.pool.idleTimeout - (now - entry.idle))38
if (!clientPresent(entry.initial)) {39
when (state.config.pool.maxLifetime) {40
case Some(val lifetime) -> delay = earlierClientDelay(delay, lifetime - (now - entry.created))41
case None -> {}42
}43
}44
}45
}46
ClientPoolPlan(actions.toArray(), delay,47
state.closing && state.calls.size == 0L && state.connections.size == 0L && state.retryWaits.size == 0L &&48
state.downloads.size == 0L && state.eventSources.size == 0L && state.webSockets.size == 0L && state.invocations.size == 0L)49
}50
}52
private fun planClientPoolClose(state: ClientState): ClientPoolPlan = state.lock.withLock {53
state.dirty = false54
state.waiting.clear()55
state.upgrades.clear()56
val actions = ArrayList<ClientPoolAction>()57
for (entry in state.connections) {58
entry.draining = true59
if (!entry.connecting && !entry.closing) {60
entry.closing = true61
actions.add(ClientPoolAction.Close(entry))62
}63
}64
ClientPoolPlan(actions.toArray(), None,65
state.calls.size == 0L && state.connections.size == 0L && state.retryWaits.size == 0L &&66
state.downloads.size == 0L && state.eventSources.size == 0L && state.webSockets.size == 0L && state.invocations.size == 0L)67
}69
private fun waitingClientCall(call: ClientCall): Boolean = call.lock.withLock {70
!call.ending && !call.completed && clientPresent(call.dialWaiter)71
}73
private fun earlierClientDelay(current: Duration?, candidate: Duration): Duration? =74
Some(if (!clientPresent(current) || candidate < current!!) candidate else current!!)76
private fun maintainClientPool(state: ClientState, now: TimeMark, actions: ArrayList<ClientPoolAction>) {77
for (entry in state.connections) {78
if (state.closing || (clientPresent(entry.initial) && !waitingClientCall(entry.initial!!))) entry.draining = true79
if (entry.connecting || entry.closing) continue80
val connection = entry.connection!!81
when (connection) {82
case Http1(val value) -> if (entry.active == 0L && !value.isReusable) entry.draining = true83
case Http2(val value) -> if (value.isClosing || value.isDraining) entry.draining = true84
}85
if (!clientPresent(entry.initial)) {86
when (state.config.pool.maxLifetime) {87
case Some(val lifetime) -> if (now - entry.created >= lifetime) entry.draining = true88
case None -> {}89
}90
}91
if (entry.active == 0L && !clientPresent(entry.initial) && now - entry.idle >= state.config.pool.idleTimeout)92
entry.draining = true93
if (entry.draining && (entry.active == 0L || state.closing)) {94
if (clientPresent(entry.initial)) {95
val first = entry.initial!!96
entry.initial = None97
if (waitingClientCall(first)) {98
val error = entry.failure ?: when (connection) {99
case Http2(val value) -> if (value.isDraining) Http2ConnectionDrainingException() else IoClosedException()100
case Http1(_) -> IoClosedException()101
}102
recordClientIoFailure(first, error, when (connection) { case Http2(_) -> true; case Http1(_) -> false })103
actions.add(ClientPoolAction.Reject(first, error))104
}105
}106
entry.closing = true107
actions.add(ClientPoolAction.Close(entry))108
}109
}110
}112
private fun assignClientLease(state: ClientState, call: ClientCall, now: TimeMark, actions: ArrayList<ClientPoolAction>): Boolean {113
for (entry in state.connections) {114
if (entry.origin != call.request.origin || !sameClientRoute(entry.route, call.route) || entry.webSocket != clientPresent(call.sequence.webSocket) ||115
entry.connecting || entry.closing || entry.draining) continue116
if (clientPresent(entry.initial) && entry.initial!! !== call) continue117
val connection = entry.connection!!118
val available = when (connection) {119
case Http1(val value) -> entry.active == 0L && value.isReusable120
case Http2(val value) -> !value.isClosing && !value.isDraining &&121
entry.active < entry.peerStreams && entry.active < state.config.pool.maxStreamsPerHttp2122
}123
if (!available) continue124
val waiter: Continuation<ClientConnectResult<ClientConnection>>? = call.lock.withLock {125
if (call.ending || call.completed) return@withLock None126
val expiry = finishClientPoolWait(call, now)127
if (clientPresent(expiry)) {128
actions.add(ClientPoolAction.Reject(call, expiry!!))129
return@withLock None130
}131
val saved = call.dialWaiter132
if (clientPresent(saved)) {133
call.dialWaiter = None; call.lease = Some(entry)134
observeClientLease(call, entry, connection)135
}136
saved137
}138
if (!clientPresent(waiter)) return true139
entry.active += 1L140
entry.initial = None141
actions.add(ClientPoolAction.Deliver(call, entry, waiter!!))142
return true143
}144
return false145
}147
private fun reserveClientConnection(state: ClientState, call: ClientCall, now: TimeMark, actions: ArrayList<ClientPoolAction>) {148
var sameOrigin = 0L149
for (entry in state.connections) {150
if (entry.origin == call.request.origin) {151
sameOrigin += 1L152
if (sameClientRoute(entry.route, call.route) && entry.webSocket == clientPresent(call.sequence.webSocket) &&153
(entry.connecting || entry.h2cPending || clientPresent(entry.initial))) return154
}155
}156
if (sameOrigin >= state.config.pool.maxConnectionsPerOrigin) {157
drainClientUnusedRoute(state, call, true)158
return159
}160
if (state.connections.size >= state.config.pool.maxConnections) {161
drainClientUnusedRoute(state, call, false)162
return163
}164
val admitted = call.lock.withLock {165
if (call.ending || call.completed) return@withLock false166
val expiry = finishClientPoolWait(call, now)167
if (clientPresent(expiry)) { actions.add(ClientPoolAction.Reject(call, expiry!!)); false } else true168
}169
if (!admitted) return170
val entry = ClientPoolEntry(call.request.origin, call, now)171
entry.h2cPending = !entry.webSocket && call.request.origin.scheme == "http" && state.config.cleartext == HttpCleartextProtocol.H2cUpgrade172
state.connections.add(entry)173
actions.add(ClientPoolAction.Dial(call, entry))174
}176
private fun drainClientUnusedRoute(state: ClientState, call: ClientCall, sameOriginOnly: Boolean) {177
var oldest: ClientPoolEntry? = None178
for (entry in state.connections) {179
val sameOrigin = entry.origin == call.request.origin180
if ((sameOriginOnly && !sameOrigin) || (sameOrigin && sameClientRoute(entry.route, call.route) && entry.webSocket == clientPresent(call.sequence.webSocket)) ||181
entry.connecting || entry.closing || entry.draining || entry.active != 0L || clientPresent(entry.initial)) continue182
if (!clientPresent(oldest) || entry.idle < oldest!!.idle) oldest = Some(entry)183
}184
if (clientPresent(oldest)) oldest!!.draining = true185
}