Scoop API

scoop:scoop.http.asyncio:0.1.0

src/io_timeouts.scoop

Selected production source · src/io_timeouts.scoop

1package scoop.http.asyncio
3import scoop.http.HttpProtocol
4import scoop.time.Duration
5import scoop.time.TimeMark
7/**
8 * Optional read-idle and write-idle deadlines for protocol I/O progress.
9 */
10public struct HttpIoTimeoutOptions(val readIdle: Duration? = None, val writeIdle: Duration? = None)
12/**
13 * The I/O direction whose idle deadline expired.
14 */
15public enum HttpIoDirection {
16 /**
17 * Waiting for input progress.
18 */
19 Read,
20 /**
21 * Waiting for output progress.
22 */
23 Write }
25/**
26 * Reports an idle timeout with protocol, stream, direction, configured limit, and measured elapsed
27 * time.
28 */
29public class HttpIoTimeoutException public constructor(
30 /**
31 * The I/O direction that made insufficient progress.
32 */
33 public val direction: HttpIoDirection,
34 /**
35 * The affected HTTP protocol.
36 */
37 public val protocol: HttpProtocol,
38 /**
39 * The affected protocol stream identifier.
40 */
41 public val streamId: Int,
42 /**
43 * The configured idle timeout.
44 */
45 public val limit: Duration,
46 /**
47 * The measured idle duration at failure.
48 */
49 public val elapsed: Duration
50) : Exception(Some("HTTP I/O idle timeout"))
52internal fun httpIdleLimit(limit: Duration?) {
53 if (limit != None && limit!! < Duration.ZERO) throw IllegalArgumentException(Some("negative HTTP idle timeout"))
56internal fun requireHttpIoTimeouts(options: HttpIoTimeoutOptions) {
57 httpIdleLimit(options.readIdle); httpIdleLimit(options.writeIdle)
60internal class HttpIoClock(val direction: HttpIoDirection, val protocol: HttpProtocol, val streamId: Int,
61 val limit: Duration, var progress: TimeMark) {
62 fun remaining(now: TimeMark): Duration = limit - (now - progress)
63 fun expired(now: TimeMark): HttpIoTimeoutException? {
64 val elapsed = now - progress
65 return if (elapsed >= limit) Some(HttpIoTimeoutException(direction, protocol, streamId, limit, elapsed)) else None
66 }