scoop:scoop.http.client:0.1.0
src/retry/failure.scoop
Selected production source · src/retry/failure.scoop
1
package scoop.http.client3
import scoop.asyncio.IoClosedException4
import scoop.asyncio.TcpConnectException5
import scoop.http.*6
import scoop.http.asyncio.Http2ConnectionDrainingException7
import scoop.platform.io.UnexpectedEofException8
import scoop.platform.system.PlatformErrorKind9
import scoop.platform.system.PlatformException11
internal enum ClientRetryReason { Connection, NotProcessed }12
internal class ClientIoFailure(val cause: Throwable, val reason: ClientRetryReason)14
// Call only at an actual transport boundary, before propagating its original error.15
internal 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 = true21
if (!clientPresent(entry!!.failure)) entry!!.failure = Some(error)22
Unit23
}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
Unit32
}33
}35
internal fun recordClientUploadIoFailure(call: ClientCall, error: Throwable, http2: Boolean) {36
call.lock.withLock { call.uploadIoFailure = Some(error) }37
recordClientIoFailure(call, error, http2)38
}40
internal 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 None48
return Some(ClientRetryReason.Connection)49
}50
return None51
}53
private fun retryableClientSocketFailure(error: Throwable): Boolean =54
error is PlatformException && (error.kind == PlatformErrorKind.BrokenPipe ||55
error.kind == PlatformErrorKind.ConnectionReset || error.kind == PlatformErrorKind.ConnectionRefused)