Scoop API

scoop:scoop.http.client:0.1.0

src/pool/plan.scoop

Selected production source · src/pool/plan.scoop

1package scoop.http.client
3import scoop.asyncio.IoClosedException
4import scoop.http.asyncio.Http2ConnectionDrainingException
5import scoop.platform.time.MonotonicTimeSource
6import scoop.time.Duration
7import scoop.time.TimeMark
9internal class ClientPoolPlan(val actions: Array<ClientPoolAction>, val delay: Duration?, val done: Boolean)
11internal 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 = false
16 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.size
20 var index = 0L
21 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 += 1L
30 }
31 maintainClientPool(state, now, actions)
32 var delay: Duration? = None
33 if (!state.closing) {
34 for (entry in state.connections) {
35 if (entry.connecting || entry.closing || entry.draining) continue
36 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 }
52private fun planClientPoolClose(state: ClientState): ClientPoolPlan = state.lock.withLock {
53 state.dirty = false
54 state.waiting.clear()
55 state.upgrades.clear()
56 val actions = ArrayList<ClientPoolAction>()
57 for (entry in state.connections) {
58 entry.draining = true
59 if (!entry.connecting && !entry.closing) {
60 entry.closing = true
61 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)
69private fun waitingClientCall(call: ClientCall): Boolean = call.lock.withLock {
70 !call.ending && !call.completed && clientPresent(call.dialWaiter)
73private fun earlierClientDelay(current: Duration?, candidate: Duration): Duration? =
74 Some(if (!clientPresent(current) || candidate < current!!) candidate else current!!)
76private 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 = true
79 if (entry.connecting || entry.closing) continue
80 val connection = entry.connection!!
81 when (connection) {
82 case Http1(val value) -> if (entry.active == 0L && !value.isReusable) entry.draining = true
83 case Http2(val value) -> if (value.isClosing || value.isDraining) entry.draining = true
84 }
85 if (!clientPresent(entry.initial)) {
86 when (state.config.pool.maxLifetime) {
87 case Some(val lifetime) -> if (now - entry.created >= lifetime) entry.draining = true
88 case None -> {}
89 }
90 }
91 if (entry.active == 0L && !clientPresent(entry.initial) && now - entry.idle >= state.config.pool.idleTimeout)
92 entry.draining = true
93 if (entry.draining && (entry.active == 0L || state.closing)) {
94 if (clientPresent(entry.initial)) {
95 val first = entry.initial!!
96 entry.initial = None
97 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 = true
107 actions.add(ClientPoolAction.Close(entry))
108 }
109 }
112private 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) continue
116 if (clientPresent(entry.initial) && entry.initial!! !== call) continue
117 val connection = entry.connection!!
118 val available = when (connection) {
119 case Http1(val value) -> entry.active == 0L && value.isReusable
120 case Http2(val value) -> !value.isClosing && !value.isDraining &&
121 entry.active < entry.peerStreams && entry.active < state.config.pool.maxStreamsPerHttp2
122 }
123 if (!available) continue
124 val waiter: Continuation<ClientConnectResult<ClientConnection>>? = call.lock.withLock {
125 if (call.ending || call.completed) return@withLock None
126 val expiry = finishClientPoolWait(call, now)
127 if (clientPresent(expiry)) {
128 actions.add(ClientPoolAction.Reject(call, expiry!!))
129 return@withLock None
130 }
131 val saved = call.dialWaiter
132 if (clientPresent(saved)) {
133 call.dialWaiter = None; call.lease = Some(entry)
134 observeClientLease(call, entry, connection)
135 }
136 saved
137 }
138 if (!clientPresent(waiter)) return true
139 entry.active += 1L
140 entry.initial = None
141 actions.add(ClientPoolAction.Deliver(call, entry, waiter!!))
142 return true
143 }
144 return false
147private fun reserveClientConnection(state: ClientState, call: ClientCall, now: TimeMark, actions: ArrayList<ClientPoolAction>) {
148 var sameOrigin = 0L
149 for (entry in state.connections) {
150 if (entry.origin == call.request.origin) {
151 sameOrigin += 1L
152 if (sameClientRoute(entry.route, call.route) && entry.webSocket == clientPresent(call.sequence.webSocket) &&
153 (entry.connecting || entry.h2cPending || clientPresent(entry.initial))) return
154 }
155 }
156 if (sameOrigin >= state.config.pool.maxConnectionsPerOrigin) {
157 drainClientUnusedRoute(state, call, true)
158 return
159 }
160 if (state.connections.size >= state.config.pool.maxConnections) {
161 drainClientUnusedRoute(state, call, false)
162 return
163 }
164 val admitted = call.lock.withLock {
165 if (call.ending || call.completed) return@withLock false
166 val expiry = finishClientPoolWait(call, now)
167 if (clientPresent(expiry)) { actions.add(ClientPoolAction.Reject(call, expiry!!)); false } else true
168 }
169 if (!admitted) return
170 val entry = ClientPoolEntry(call.request.origin, call, now)
171 entry.h2cPending = !entry.webSocket && call.request.origin.scheme == "http" && state.config.cleartext == HttpCleartextProtocol.H2cUpgrade
172 state.connections.add(entry)
173 actions.add(ClientPoolAction.Dial(call, entry))
176private fun drainClientUnusedRoute(state: ClientState, call: ClientCall, sameOriginOnly: Boolean) {
177 var oldest: ClientPoolEntry? = None
178 for (entry in state.connections) {
179 val sameOrigin = entry.origin == call.request.origin
180 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)) continue
182 if (!clientPresent(oldest) || entry.idle < oldest!!.idle) oldest = Some(entry)
183 }
184 if (clientPresent(oldest)) oldest!!.draining = true