scoop:scoop.http.client:0.1.0
src/timeouts/network.scoop
Selected production source · src/timeouts/network.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.time.Duration6
internal fun clientTcpConnectOptions(call: ClientCall): TcpConnectOptions {7
val options = call.client.config.tcpConnect8
val callback = options.onEvent9
val onEvent: ((TcpConnectEvent) -> Unit)? =10
if (!clientPresent(call.observation) && !clientPresent(callback))11
None12
else13
Some(14
{15
event: TcpConnectEvent ->16
observeClientTcp(call, event)17
try {18
if (clientPresent(callback))19
callback!!(event)20
}21
catch (error: Throwable) {22
failClientUserCallback(call, error);23
throw error24
}25
Unit26
}27
)28
return tcpConnectOptions {29
this.resolutionDelay = options.resolutionDelay30
this.attemptDelay = options.attemptDelay31
this.maxConcurrentAttempts = options.maxConcurrentAttempts32
this.resolveTimeout = call.timeouts.resolve33
this.connectTimeout = call.timeouts.connect34
this.onEvent = onEvent35
}36
}38
internal fun mapClientNetworkTimeout(call: ClientCall, error: Throwable): Throwable {39
if (call.lock.withLock { call.userCallbackFailed })40
return error41
if (error is ResourceUseException) {42
val mapped = mapClientNetworkTimeout(call, error.primaryFailure)43
return if (mapped === error.primaryFailure)44
error45
else46
ResourceUseException(mapped, error.cleanupFailure)47
}48
if (error is TcpConnectTimeoutException)49
return clientNetworkTimeout(50
call,51
HttpTimeoutPhase.Connect,52
error.limit,53
error.elapsed,54
error55
)56
if (error is TcpConnectException) {57
for (failure in error.resolutionFailures) {58
val cause = failure.cause59
if (cause is TcpResolutionTimeoutException)60
return clientNetworkTimeout(61
call,62
HttpTimeoutPhase.Resolve,63
cause.limit,64
cause.elapsed,65
error66
)67
}68
}69
return error70
}72
private fun clientNetworkTimeout(73
call: ClientCall,74
phase: HttpTimeoutPhase,75
limit: Duration,76
elapsed: Duration,77
cause: Throwable78
): HttpTimeoutException =79
call.lock.withLock {80
HttpTimeoutException(81
phase,82
limit,83
elapsed,84
call.requestBodyBytes,85
call.encodedResponseBodyBytes,86
call.responseBodyBytes,87
Some(cause)88
)89
}