Scoop API

scoop:scoop.http.client:0.1.0

src/download/state.scoop

Selected production source · src/download/state.scoop

1package scoop.http.client
3import scoop.asyncio.AsyncFile
4import scoop.asyncio.AsyncCloseable
5import scoop.platform.fs.Path
6import scoop.platform.time.MonotonicTimeSource
8internal class ClientDownloadFile(
9 val path: Path,
10 val file: AsyncFile,
11 val discardOnFailure: Boolean
12): AsyncCloseable {
13 public override suspend fun close() {
14 file.close()
15 }
18internal class ClientDownload(
19 val client: ClientState,
20 val caller: Job,
21 initial: ClientRequestSequence
22) {
23 var sequence: ClientRequestSequence = initial
24 var job: Job? = None
25 var registered: Boolean = false
26 var committing: Boolean = false
27 var committed: Boolean = false
28 var failure: Throwable? = None
29 var cleanupFailure: Throwable? = None
30 var responseCall: ClientCall? = None
31 var output: ClientDownloadFile? = None
32 var resumeMetadata: Path? = None
34 fun requireActive() {
35 val current = client.lock.withLock { failure }
36 if (clientPresent(current))
37 throw current!!
38 caller.ensureActive()
39 ensureActive()
40 val limit = sequence.timeouts.total
41 if (clientPresent(limit) && MonotonicTimeSource.markNow() - sequence.started!! >= limit!!)
42 throw timeout()
43 }
45 fun timeout(): HttpTimeoutException {
46 val(current, previous) = client.lock.withLock { (sequence, responseCall) }
47 return clientSequenceTimeout(client, current, previous)
48 }
51internal fun stopClientDownload(download: ClientDownload, reason: Throwable) {
52 val job: Job? =
53 download.client.lock.withLock {
54 if (download.committing)
55 return@withLock None
56 if (!clientPresent(download.failure))
57 download.failure = Some(reason)
58 download.job
59 }
60 job?.cancel()
63internal fun checkClientDownloadClose(state: ClientState) {
64 val current = contextOrNone<ClientDownload>()
65 if (
66 clientPresent(current) &&
67 current!!.client ===
68 state &&
69 state.lock.withLock { current!!.registered }
70 )
71 throw IllegalStateException(Some("cannot await client.close from an active download"))