Scoop API

scoop:scoop.http.client:0.1.0

src/lifetime/common.scoop

Selected production source · src/lifetime/common.scoop

1package scoop.http.client
3import scoop.asyncio.*
5internal class ClientLock {
6 private val held: AtomicBoolean = AtomicBoolean(false)
7 fun <T> withLock(block: () -> T): T {
8 while (!held.compareAndSet(false, true, MemoryOrder.Acquire, MemoryOrder.Relaxed)) {}
9 try { return block() }
10 finally { held.store(false, MemoryOrder.Release) }
11 }
14internal fun <T> clientPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }
16internal fun clientJob(loop: EventLoop, active: Boolean): Job {
17 if (currentEventLoop() !== loop) throw IllegalStateException(Some("HTTP client belongs to another event loop"))
18 val job = currentJobOrNone() ?: throw IllegalStateException(Some("HTTP client operation requires a Job"))
19 if (job.isCompleted) throw IllegalStateException(Some("HTTP client operation requires an active Job"))
20 if (active) job.ensureActive()
21 return job
24internal fun clientCombined(primary: Throwable?, cleanup: Throwable): Throwable =
25 when (primary) { case Some(val error) -> ResourceUseException(error, cleanup); case None -> cleanup }
27/**
28 * An operation attempted to use a closing or closed client.
29 */
30public class HttpClientClosedException public constructor() : Exception(Some("HTTP client is closed"))
32internal fun clientBytesEqual(first: List<Byte>, second: List<Byte>): Boolean {
33 if (first.size != second.size) return false
34 var index = 0L
35 while (index < first.size) {
36 if (first[index] != second[index]) return false
37 index += 1L
38 }
39 return true