scoop:scoop.http.client:0.1.0
src/lifetime/common.scoop
Selected production source · src/lifetime/common.scoop
1
package scoop.http.client3
import scoop.asyncio.*5
internal 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
}12
}14
internal fun <T> clientPresent(value: T?): Boolean = when (value) { case Some(_) -> true; case None -> false }16
internal 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 job22
}24
internal 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
*/30
public class HttpClientClosedException public constructor() : Exception(Some("HTTP client is closed"))32
internal fun clientBytesEqual(first: List<Byte>, second: List<Byte>): Boolean {33
if (first.size != second.size) return false34
var index = 0L35
while (index < first.size) {36
if (first[index] != second[index]) return false37
index += 1L38
}39
return true40
}