Scoop API

scoop:scoop.http.client:0.1.0

src/download/lifetime.scoop

Selected production source · src/download/lifetime.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.platform.fs.Path
5import scoop.time.Duration
6import scoop.platform.time.MonotonicTimeSource
8internal suspend fun runClientDownload(
9 state: ClientState,
10 request: HttpRequest,
11 destination: Path,
12 options: HttpDownloadOptions,
13 resume: HttpResumeOptions? = None
14): HttpDownloadResult {
15 if (!clientPresent(destination.fileName))
16 throw IllegalArgumentException(Some("download destination needs a file name"))
17 if (clientPresent(resume))
18 validateClientResumeRequest(request, destination, resume!!)
19 val download =
20 ClientDownload(
21 state,
22 clientJob(state.loop, true),
23 ClientRequestSequence(state.config, request)
24 )
25 var result: HttpDownloadResult? = None
26 try {
27 val ready =
28 context(download) {
29 coroutineScope<HttpDownloadResult> {
30 val scope = this
32 registerClientDownload(download, scope.job)
33 val listener =
34 scope.job.invokeOnCancellation {
35 reason -> state.lock.withLock {
36 if (!clientPresent(download.failure))
37 download.failure = Some(reason)
38 Unit
39 }
40 }
41 val timer = startClientDownloadTimer(download, scope)
42 try {
43 download.requireActive()
44 if (clientPresent(resume))
45 prepareClientResume(download, request, destination, options, resume!!)
46 else
47 prepareClientDownload(download, request, destination, options)
48 } catch (error: Throwable) {
49 recordClientDownloadFailure(download, error);
50 throw error
51 }
52 finally {
53 timer?.cancel();
54 listener.dispose()
55 }
56 }
57 }
58 download.requireActive()
59 state.lock.withLock {
60 if (state.closing)
61 throw HttpClientClosedException()
62 if (clientPresent(download.failure))
63 throw download.failure!!
64 download.committing = true
65 }
66 nonCancellableScope<Unit> {
67 state.loop.moveFile(download.output!!.path, destination, options.replaceExisting)
68 download.committed = true
69 }
70 result = Some(ready)
71 } catch (error: Throwable) {
72 recordClientDownloadFailure(download, error)
73 }
74 finally {
75 try {
76 cleanupClientDownload(download)
77 }
78 catch (error: Throwable) {
79 state.lock.withLock {
80 download.cleanupFailure = appendClientCleanup(download.cleanupFailure, error)
81 }
82 }
83 unregisterClientDownload(download)
84 }
85 val failure = clientDownloadFailure(download)
86 if (clientPresent(failure))
87 throw failure!!
88 return result!!
91private fun registerClientDownload(download: ClientDownload, job: Job) {
92 download.client.lock.withLock {
93 if (download.client.closing)
94 throw HttpClientClosedException()
95 download.job = Some(job)
96 download.registered = true
97 download.client.downloads.add(download)
98 }
101private fun unregisterClientDownload(download: ClientDownload) {
102 val state = download.client
103 state.lock.withLock {
104 if (!download.registered)
105 return@withLock
106 var index = 0L
107 while (index < state.downloads.size) {
108 if (state.downloads[index] === download) {
109 state.downloads.removeAt(index);
110 break
111 }
112 index += 1L
113 }
114 download.registered = false
115 download.job = None
116 if (state.closing && clientPresent(download.cleanupFailure))
117 state.closeFailure = appendClientCleanup(state.closeFailure, download.cleanupFailure!!)
118 Unit
119 }
120 wakeClient(state)
123private fun startClientDownloadTimer(download: ClientDownload, scope: CoroutineScope): Job? {
124 val limit = download.sequence.timeouts.total ?: return None
125 return Some(
126 scope.launch suspend {
127 try {
128 val remaining =
129 limit - (MonotonicTimeSource.markNow() - download.sequence.started!!)
130 if (remaining > Duration.ZERO)
131 download.client.loop.sleep(remaining)
132 stopClientDownload(download, download.timeout())
133 } catch (error: Throwable) {
134 if (!(error is CancellationException))
135 stopClientDownload(download, error)
136 }
137 }
138 )