Scoop API

scoop:scoop.http.client:0.1.0

src/retry/failure.scoop

Selected production source · src/retry/failure.scoop

1package scoop.http.client
3import scoop.asyncio.IoClosedException
4import scoop.asyncio.TcpConnectException
5import scoop.http.*
6import scoop.http.asyncio.Http2ConnectionDrainingException
7import scoop.platform.io.UnexpectedEofException
8import scoop.platform.system.PlatformErrorKind
9import scoop.platform.system.PlatformException
11internal enum ClientRetryReason { Connection, NotProcessed }
12internal class ClientIoFailure(val cause: Throwable, val reason: ClientRetryReason)
14// Call only at an actual transport boundary, before propagating its original error.
15internal fun recordClientIoFailure(call: ClientCall, error: Throwable, http2: Boolean = false) {
16 // A reconnect must not acquire a failed connection while its asynchronous close is pending.
17 if (http2 && error is HttpException && error.scope == HttpFailureScope.Connection) {
18 val entry = call.lock.withLock { call.lease }
19 if (clientPresent(entry)) call.client.lock.withLock {
20 entry!!.draining = true
21 if (!clientPresent(entry!!.failure)) entry!!.failure = Some(error)
22 Unit
23 }
24 }
25 val reason = clientRetryReason(error, http2)
26 call.lock.withLock {
27 if (!call.completed && (!clientPresent(call.failure) || call.failure!! === error)) {
28 call.sequence.lastTransportFailure = Some(error)
29 if (clientPresent(reason)) call.ioFailure = Some(ClientIoFailure(error, reason!!))
30 }
31 Unit
32 }
35internal fun recordClientUploadIoFailure(call: ClientCall, error: Throwable, http2: Boolean) {
36 call.lock.withLock { call.uploadIoFailure = Some(error) }
37 recordClientIoFailure(call, error, http2)
40internal fun clientRetryReason(error: Throwable, http2: Boolean): ClientRetryReason? {
41 if (error is Http2ConnectionDrainingException) return Some(ClientRetryReason.NotProcessed)
42 if (error is HttpException && error.protocol == HttpProtocol.Http2 &&
43 error.scope == HttpFailureScope.Stream && error.code == 7) return Some(ClientRetryReason.NotProcessed)
44 if (error is UnexpectedEofException || (http2 && error is IoClosedException)) return Some(ClientRetryReason.Connection)
45 if (retryableClientSocketFailure(error)) return Some(ClientRetryReason.Connection)
46 if (error is TcpConnectException && error.attempts.size != 0L) {
47 for (attempt in error.attempts) if (!retryableClientSocketFailure(attempt.cause)) return None
48 return Some(ClientRetryReason.Connection)
49 }
50 return None
53private fun retryableClientSocketFailure(error: Throwable): Boolean =
54 error is PlatformException && (error.kind == PlatformErrorKind.BrokenPipe ||
55 error.kind == PlatformErrorKind.ConnectionReset || error.kind == PlatformErrorKind.ConnectionRefused)