Scoop API

scoop:scoop.http.client:0.1.0

src/transport/h2c_upgrade.scoop

Selected production source · src/transport/h2c_upgrade.scoop

1package scoop.http.client
3import scoop.asyncio.IoClosedException
4import scoop.http.asyncio.*
6internal class ClientUpgrade(
7 val call: ClientCall,
8 val entry: ClientPoolEntry,
9 val source: AsyncHttp1Connection
12internal suspend fun awaitClientUpgrade(
13 call: ClientCall,
14 entry: ClientPoolEntry,
15 source: AsyncHttp1Connection
16): AsyncHttp2Connection =
17 suspendCoroutine<AsyncHttp2Connection> {
18 continuation ->
19 call.lock.withLock {
20 if (call.ending)
21 throw (call.failure ?: CancellationException())
22 call.upgradeWaiter = Some(continuation)
23 }
24 val queued =
25 call.client.lock.withLock {
26 if (call.client.closing)
27 false
28 else {
29 call.client.upgrades.addLast(ClientUpgrade(call, entry, source));
30 true
31 }
32 }
33 if (queued)
34 wakeClient(call.client)
35 else
36 cancelClientCall(call, HttpClientClosedException())
37 }
39internal fun startClientUpgrade(work: ClientUpgrade) {
40 val call = work.call
41 val scope = call.lock.withLock { call.scope }
42 if (!clientPresent(scope) || call.lock.withLock { call.ending }) {
43 finishClientH2cOffer(call.client, work.entry, true)
44 return
45 }
46 try {
47 launchClientTask(
48 scope!!,
49 {
50 error ->
51 cancelClientCall(call, error)
52 finishClientH2cOffer(call.client, work.entry, true)
53 }
54 ) {
55 try {
56 val connection =
57 context(scope!!.job) {
58 adoptClientUpgrade(work)
59 }
60 deliverClientUpgrade(work, connection)
61 } catch (error: Throwable) {
62 val waiter =
63 call.lock.withLock {
64 val saved = call.upgradeWaiter;
65 call.upgradeWaiter = None;
66 saved
67 }
68 finishClientH2cOffer(call.client, work.entry, true)
69 if (clientPresent(waiter))
70 waiter!!.resumeWithException(error)
71 else
72 if (!(error is CancellationException))
73 failClientCall(call, error)
74 }
75 }
76 } catch (error: Throwable) {
77 failClientCall(call, error)
78 finishClientH2cOffer(call.client, work.entry, true)
79 }
82private suspend fun adoptClientUpgrade(work: ClientUpgrade): AsyncHttp2Connection {
83 val call = work.call
84 val state = call.client
85 val owner =
86 state.lock.withLock {
87 if (state.closing)
88 throw HttpClientClosedException()
89 state.scope!!
90 }
91 requireClientCall(call)
92 val transport = work.source.takeTransport()
93 try {
94 val config = state.config
95 return AsyncHttp2Connection.takeOwnershipAsUpgradedClient(
96 transport,
97 owner,
98 call.request.methodBytes,
99 config.http2Settings,
100 config.http2Receive,
101 config.http2Limits,
102 ioTimeouts = clientIoTimeouts(call),
103 onEvent = { notice -> clientPoolNotice(state, work.entry, notice) },
104 captureTimings = clientPresent(config.observer)
105 )
106 } catch (error: Throwable) {
107 try {
108 transport.close()
109 } catch (cleanup: Throwable) {
110 throw ResourceUseException(error, cleanup)
111 }
112 throw error
113 }
116private suspend fun deliverClientUpgrade(work: ClientUpgrade, connection: AsyncHttp2Connection) {
117 val call = work.call
118 val waiter =
119 call.lock.withLock {
120 val saved = call.upgradeWaiter;
121 call.upgradeWaiter = None;
122 saved
123 }
124 val failure: Throwable? =
125 call.client.lock.withLock {
126 if (call.client.closing)
127 Some(HttpClientClosedException())
128 else
129 if (!clientPresent(waiter))
130 Some(IoClosedException())
131 else {
132 val error =
133 call.lock.withLock {
134 if (call.ending)
135 Some(call.failure ?: IoClosedException())
136 else
137 None
138 }
139 if (!clientPresent(error)) {
140 work.entry.connection = Some(ClientConnection.Http2(connection))
141 work.entry.h2cPending = false
142 }
143 error
144 }
145 }
146 if (clientPresent(failure)) {
147 var error = failure!!
148 try {
149 connection.close()
150 } catch (cleanup: Throwable) {
151 error = clientCombined(Some(error), cleanup)
152 }
153 finishClientH2cOffer(call.client, work.entry, true)
154 if (clientPresent(waiter))
155 waiter!!.resumeWithException(error)
156 else
157 if (error is ResourceUseException)
158 failClientCall(call, error)
159 } else {
160 waiter!!.resume(connection)
161 wakeClient(call.client)
162 }