Scoop API

scoop:scoop.http.client:0.1.0

src/pool/entry.scoop

Selected production source · src/pool/entry.scoop

1package scoop.http.client
3import scoop.http.asyncio.Http2Notice
4import scoop.http.HttpFailureScope
5import scoop.platform.time.MonotonicTimeSource
6import scoop.time.TimeMark
7import scoop.url.UrlOrigin
9internal class ClientPoolEntry(val origin: UrlOrigin, first: ClientCall, now: TimeMark) {
10 val route: ClientRoute = first.route
11 val webSocket: Boolean = clientPresent(first.sequence.webSocket)
12 var connection: ClientConnection? = None
13 var initial: ClientCall? = Some(first)
14 var connecting: Boolean = true
15 var h2cPending: Boolean = false
16 var closing: Boolean = false
17 var draining: Boolean = false
18 var active: Long = 0L
19 var peerStreams: Long = 4294967295L
20 var lastStreamId: Int? = None
21 var failure: Throwable? = None
22 var created: TimeMark = now
23 var idle: TimeMark = created
26internal enum ClientPoolAction {
27 Dial(ClientCall, ClientPoolEntry),
28 Upgrade(ClientUpgrade),
29 Deliver(ClientCall, ClientPoolEntry, Continuation<ClientConnectResult<ClientConnection>>),
30 Reject(ClientCall, Throwable),
31 Close(ClientPoolEntry)
34internal fun clientPoolNotice(state: ClientState, entry: ClientPoolEntry, notice: Http2Notice) {
35 when (notice) {
36 case StreamFailure(val id, val error) -> { failClientHttp2Stream(state, entry, id, error); return }
37 else -> {}
38 }
39 val changed = state.lock.withLock {
40 when (notice) {
41 case Settings(val values, false) -> {
42 for (value in values) if (value.id == 3.toUShort()) entry.peerStreams = value.value.toLong()
43 true
44 }
45 case GoAway(val last, _, _, val local) -> {
46 entry.draining = true
47 if (!local) entry.lastStreamId = Some(last)
48 true
49 }
50 case Failure(val error) -> {
51 if (error.scope != HttpFailureScope.Connection) false else {
52 entry.draining = true
53 if (!clientPresent(entry.failure)) entry.failure = Some(error)
54 true
55 }
56 }
57 else -> false
58 }
59 }
60 if (changed) wakeClient(state)
63internal fun removeClientPoolEntry(state: ClientState, entry: ClientPoolEntry) {
64 state.lock.withLock {
65 var index = 0L
66 while (index < state.connections.size) {
67 if (state.connections[index] === entry) { state.connections.removeAt(index); break }
68 index += 1L
69 }
70 }
71 wakeClient(state)
74internal fun releaseClientLease(call: ClientCall) {
75 val entry = call.lock.withLock { val saved = call.lease; call.lease = None; saved }
76 if (clientPresent(entry)) {
77 val now: TimeMark? = try { Some(MonotonicTimeSource.markNow()) }
78 catch (error: Throwable) {
79 call.lock.withLock { call.cleanupFailure = Some(clientCombined(call.cleanupFailure, error)) }
80 None
81 }
82 call.client.lock.withLock {
83 entry!!.active -= 1L
84 if (!clientPresent(now)) entry!!.draining = true
85 else if (entry!!.active == 0L) entry!!.idle = now!!
86 Unit
87 }
88 }