scoop:scoop.http.client:0.1.0
src/download/state.scoop
Selected production source · src/download/state.scoop
1
package scoop.http.client3
import scoop.asyncio.AsyncFile4
import scoop.asyncio.AsyncCloseable5
import scoop.platform.fs.Path6
import scoop.platform.time.MonotonicTimeSource8
internal class ClientDownloadFile(9
val path: Path,10
val file: AsyncFile,11
val discardOnFailure: Boolean12
): AsyncCloseable {13
public override suspend fun close() {14
file.close()15
}16
}18
internal class ClientDownload(19
val client: ClientState,20
val caller: Job,21
initial: ClientRequestSequence22
) {23
var sequence: ClientRequestSequence = initial24
var job: Job? = None25
var registered: Boolean = false26
var committing: Boolean = false27
var committed: Boolean = false28
var failure: Throwable? = None29
var cleanupFailure: Throwable? = None30
var responseCall: ClientCall? = None31
var output: ClientDownloadFile? = None32
var resumeMetadata: Path? = None34
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.total41
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
}49
}51
internal fun stopClientDownload(download: ClientDownload, reason: Throwable) {52
val job: Job? =53
download.client.lock.withLock {54
if (download.committing)55
return@withLock None56
if (!clientPresent(download.failure))57
download.failure = Some(reason)58
download.job59
}60
job?.cancel()61
}63
internal 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"))72
}