scoop:scoop.http.client:0.1.0
src/download/lifetime.scoop
Selected production source · src/download/lifetime.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.platform.fs.Path5
import scoop.time.Duration6
import scoop.platform.time.MonotonicTimeSource8
internal suspend fun runClientDownload(9
state: ClientState,10
request: HttpRequest,11
destination: Path,12
options: HttpDownloadOptions,13
resume: HttpResumeOptions? = None14
): 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? = None26
try {27
val ready =28
context(download) {29
coroutineScope<HttpDownloadResult> {30
val scope = this32
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
Unit39
}40
}41
val timer = startClientDownloadTimer(download, scope)42
try {43
download.requireActive()44
if (clientPresent(resume))45
prepareClientResume(download, request, destination, options, resume!!)46
else47
prepareClientDownload(download, request, destination, options)48
} catch (error: Throwable) {49
recordClientDownloadFailure(download, error);50
throw error51
}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 = true65
}66
nonCancellableScope<Unit> {67
state.loop.moveFile(download.output!!.path, destination, options.replaceExisting)68
download.committed = true69
}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!!89
}91
private 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 = true97
download.client.downloads.add(download)98
}99
}101
private fun unregisterClientDownload(download: ClientDownload) {102
val state = download.client103
state.lock.withLock {104
if (!download.registered)105
return@withLock106
var index = 0L107
while (index < state.downloads.size) {108
if (state.downloads[index] === download) {109
state.downloads.removeAt(index);110
break111
}112
index += 1L113
}114
download.registered = false115
download.job = None116
if (state.closing && clientPresent(download.cleanupFailure))117
state.closeFailure = appendClientCleanup(state.closeFailure, download.cleanupFailure!!)118
Unit119
}120
wakeClient(state)121
}123
private fun startClientDownloadTimer(download: ClientDownload, scope: CoroutineScope): Job? {124
val limit = download.sequence.timeouts.total ?: return None125
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
)139
}