Scoop API

scoop:scoop.http.client:0.1.0

src/timeouts/network.scoop

Selected production source · src/timeouts/network.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.time.Duration
6internal fun clientTcpConnectOptions(call: ClientCall): TcpConnectOptions {
7 val options = call.client.config.tcpConnect
8 val callback = options.onEvent
9 val onEvent: ((TcpConnectEvent) -> Unit)? =
10 if (!clientPresent(call.observation) && !clientPresent(callback))
11 None
12 else
13 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 error
24 }
25 Unit
26 }
27 )
28 return tcpConnectOptions {
29 this.resolutionDelay = options.resolutionDelay
30 this.attemptDelay = options.attemptDelay
31 this.maxConcurrentAttempts = options.maxConcurrentAttempts
32 this.resolveTimeout = call.timeouts.resolve
33 this.connectTimeout = call.timeouts.connect
34 this.onEvent = onEvent
35 }
38internal fun mapClientNetworkTimeout(call: ClientCall, error: Throwable): Throwable {
39 if (call.lock.withLock { call.userCallbackFailed })
40 return error
41 if (error is ResourceUseException) {
42 val mapped = mapClientNetworkTimeout(call, error.primaryFailure)
43 return if (mapped === error.primaryFailure)
44 error
45 else
46 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 error
55 )
56 if (error is TcpConnectException) {
57 for (failure in error.resolutionFailures) {
58 val cause = failure.cause
59 if (cause is TcpResolutionTimeoutException)
60 return clientNetworkTimeout(
61 call,
62 HttpTimeoutPhase.Resolve,
63 cause.limit,
64 cause.elapsed,
65 error
66 )
67 }
68 }
69 return error
72private fun clientNetworkTimeout(
73 call: ClientCall,
74 phase: HttpTimeoutPhase,
75 limit: Duration,
76 elapsed: Duration,
77 cause: Throwable
78): 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 }