scoop:scoop.http.client:0.1.0
src/pool/entry.scoop
Selected production source · src/pool/entry.scoop
1
package scoop.http.client3
import scoop.http.asyncio.Http2Notice4
import scoop.http.HttpFailureScope5
import scoop.platform.time.MonotonicTimeSource6
import scoop.time.TimeMark7
import scoop.url.UrlOrigin9
internal class ClientPoolEntry(val origin: UrlOrigin, first: ClientCall, now: TimeMark) {10
val route: ClientRoute = first.route11
val webSocket: Boolean = clientPresent(first.sequence.webSocket)12
var connection: ClientConnection? = None13
var initial: ClientCall? = Some(first)14
var connecting: Boolean = true15
var h2cPending: Boolean = false16
var closing: Boolean = false17
var draining: Boolean = false18
var active: Long = 0L19
var peerStreams: Long = 4294967295L20
var lastStreamId: Int? = None21
var failure: Throwable? = None22
var created: TimeMark = now23
var idle: TimeMark = created24
}26
internal enum ClientPoolAction {27
Dial(ClientCall, ClientPoolEntry),28
Upgrade(ClientUpgrade),29
Deliver(ClientCall, ClientPoolEntry, Continuation<ClientConnectResult<ClientConnection>>),30
Reject(ClientCall, Throwable),31
Close(ClientPoolEntry)32
}34
internal 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
true44
}45
case GoAway(val last, _, _, val local) -> {46
entry.draining = true47
if (!local) entry.lastStreamId = Some(last)48
true49
}50
case Failure(val error) -> {51
if (error.scope != HttpFailureScope.Connection) false else {52
entry.draining = true53
if (!clientPresent(entry.failure)) entry.failure = Some(error)54
true55
}56
}57
else -> false58
}59
}60
if (changed) wakeClient(state)61
}63
internal fun removeClientPoolEntry(state: ClientState, entry: ClientPoolEntry) {64
state.lock.withLock {65
var index = 0L66
while (index < state.connections.size) {67
if (state.connections[index] === entry) { state.connections.removeAt(index); break }68
index += 1L69
}70
}71
wakeClient(state)72
}74
internal 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
None81
}82
call.client.lock.withLock {83
entry!!.active -= 1L84
if (!clientPresent(now)) entry!!.draining = true85
else if (entry!!.active == 0L) entry!!.idle = now!!86
Unit87
}88
}89
}