scoop:scoop.http.client:0.1.0
src/sse/api.scoop
Selected production source · src/sse/api.scoop
1
package scoop.http.client3
import scoop.asyncio.AsyncCloseable4
import scoop.http.*5
import scoop.time.Duration6
import scoop.url.Url8
/**9
* SSE parser limits, buffer size, initial event ID, and optional reconnection policy. Reconnection10
* is disabled unless explicitly configured.11
*/12
public class HttpSseOptions internal constructor(13
/**14
* SSE line and event-data byte bounds.15
*/16
public val limits: SseLimits = SseLimits(),17
/**18
* The positive input-buffer size.19
*/20
public val bufferSize: Long = 8192L,21
/**22
* An optional starting Last-Event-ID value.23
*/24
public val initialLastEventId: String? = None,25
/**26
* Optional reconnection policy; None ends after the current stream.27
*/28
public val reconnect: HttpSseReconnectOptions? = None29
) {30
init {31
if (bufferSize <= 0L)32
throw IllegalArgumentException(Some("SSE buffer size must be positive"))33
}34
}36
/**37
* Cancellable SSE reconnection delays, optional attempt bound, and a callback before reconnecting.38
*/39
public class HttpSseReconnectOptions internal constructor(40
/**41
* The initial reconnect delay before a server retry hint.42
*/43
public val initialDelay: Duration = Duration.seconds(3L),44
/**45
* The maximum selected reconnect delay.46
*/47
public val maxDelay: Duration = Duration.seconds(30L),48
/**49
* Optional maximum reconnect count; None leaves the count unbounded.50
*/51
public val maxReconnects: Long? = None,52
/**53
* Suspend callback receiving the next reconnect decision.54
*/55
public val onReconnect: suspend (HttpSseReconnect) -> Unit = suspend { _ -> }56
) {57
init {58
if (initialDelay < Duration.ZERO || maxDelay < initialDelay || (maxReconnects ?: 0L) < 0L)59
throw IllegalArgumentException(Some("invalid SSE reconnect policy"))60
}61
}63
/**64
* A reconnect snapshot containing attempt number, selected delay, URL, current event ID, and65
* preceding failure.66
*/67
public class HttpSseReconnect internal constructor(68
/**69
* The reconnect attempt number.70
*/71
public val number: Long,72
/**73
* The selected wait before reconnecting.74
*/75
public val delay: Duration,76
/**77
* The URL selected for reconnect.78
*/79
public val url: Url,80
/**81
* The last accepted event ID forwarded to the next request.82
*/83
public val lastEventId: String,84
/**85
* The preceding failure, or None after ordinary EOF.86
*/87
public val cause: Throwable?88
)90
/**91
* The reason an HTTP response or request cannot be used as an SSE source.92
*/93
public enum HttpSseFailureReason {94
/**95
* SSE requires status 200, or 204 to stop reconnection.96
*/97
UnexpectedStatus,98
/**99
* The response is not text/event-stream.100
*/101
UnsupportedContentType,102
/**103
* A requested reconnect would reuse a one-shot request body.104
*/105
BodyNotReplayable106
}108
/**109
* An SSE setup or reconnection failure with the URL and response head when available.110
*/111
public class HttpSseException public constructor(112
/**113
* The SSE setup or replay failure category.114
*/115
public val reason: HttpSseFailureReason,116
/**117
* The URL involved.118
*/119
public val url: Url,120
/**121
* The response head when one was received.122
*/123
public val head: HttpResponseHead? = None124
): Exception(125
Some(126
when (reason) {127
case UnexpectedStatus -> "SSE requires status 200 or 204"128
case UnsupportedContentType -> "SSE requires text/event-stream"129
case BodyNotReplayable -> "SSE reconnect requires a replayable request body"130
}131
)132
)134
/**135
* An owned serial SSE event source. It tracks last-event ID and retry delay across explicitly136
* enabled reconnects; close cancels and awaits pending work.137
*/138
public class HttpEventSource internal constructor(139
private val state: ClientSseState,140
private val info: ClientSseInfo141
): AsyncCloseable {142
/**143
* The current stream's final URL, updated after reconnects.144
*/145
public val url: Url get() = state.client.lock.withLock { info.url }146
/**147
* The current stream's response head.148
*/149
public val head: HttpResponseHead get() = state.client.lock.withLock { info.head }150
/**151
* The persistent last-event ID.152
*/153
public val lastEventId: String get() = state.client.lock.withLock { state.lastEventId }154
/**155
* The currently selected reconnect delay.156
*/157
public val retryDelay: Duration get() = state.client.lock.withLock { state.delay }158
/**159
* The number of reconnects performed.160
*/161
public val reconnectCount: Long get() = state.client.lock.withLock { state.reconnects }162
/**163
* Whether event production has reached its terminal state.164
*/165
public val isFinished: Boolean get() = state.client.lock.withLock { state.finished }166
/**167
* Whether explicit close has completed.168
*/169
public val isClosed: Boolean get() = state.client.lock.withLock { state.closed }170
/**171
* The terminal stream or cleanup failure, if any.172
*/173
public val failure: Throwable? get() =174
state.client.lock.withLock {175
if (state.explicitClose)176
state.cleanupFailure177
else178
clientSseFailure(state)179
}180
/**181
* Returns the next event, suspending through enabled reconnects, or None when event production182
* ends. Only one reader may be active.183
*/184
public suspend fun next(): SseEvent? = readClientSse(state)185
/**186
* Cancels pending reads/reconnects and awaits stream and call cleanup.187
*/188
public override suspend fun close() {189
closeClientSse(state)190
}191
}