http_driver: add 504 binding timeouts
This commit is contained in:
parent
6a4854110c
commit
6487ef65d0
|
@ -2,7 +2,7 @@
|
||||||
# SPDX-License-Identifier: Unlicense
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
import std/[httpcore, options, parseutils, sets, streams, strutils, tables, times, uri]
|
import std/[httpcore, options, parseutils, sets, streams, strutils, tables, times, uri]
|
||||||
import preserves, ../../syndicate, ../bags
|
import preserves, ../../syndicate, ../bags, ./timers
|
||||||
import ../protocols/http
|
import ../protocols/http
|
||||||
import taps
|
import taps
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ proc lenLine(chunk: Chunk): string =
|
||||||
type
|
type
|
||||||
Driver = ref object
|
Driver = ref object
|
||||||
facet: Facet
|
facet: Facet
|
||||||
ds: Cap
|
ds, timers: Cap
|
||||||
bindings: Bag[Value]
|
bindings: Bag[Value]
|
||||||
# cannot make a bag of HttpBinding, no `==` operator
|
# cannot make a bag of HttpBinding, no `==` operator
|
||||||
sequenceNumber: BiggestInt
|
sequenceNumber: BiggestInt
|
||||||
|
@ -128,6 +128,7 @@ type
|
||||||
req: HttpRequest
|
req: HttpRequest
|
||||||
stream: StringStream
|
stream: StringStream
|
||||||
mode: HttpResponseKind
|
mode: HttpResponseKind
|
||||||
|
active: bool
|
||||||
|
|
||||||
proc send[T: byte|char](ses: Session; data: openarray[T]) =
|
proc send[T: byte|char](ses: Session; data: openarray[T]) =
|
||||||
ses.conn.send(addr data[0], data.len, endOfMessage = false)
|
ses.conn.send(addr data[0], data.len, endOfMessage = false)
|
||||||
|
@ -187,6 +188,7 @@ method message(e: Exchange; turn: var Turn; a: AssertionRef) =
|
||||||
|
|
||||||
of HttpResponseKind.status:
|
of HttpResponseKind.status:
|
||||||
if e.mode == res.orKind:
|
if e.mode == res.orKind:
|
||||||
|
e.active = true
|
||||||
e.ses.conn.startBatch()
|
e.ses.conn.startBatch()
|
||||||
e.stream.write(
|
e.stream.write(
|
||||||
SupportedVersion, " ", res.status.code, " ", res.status.message, CRLF,
|
SupportedVersion, " ", res.status.code, " ", res.status.message, CRLF,
|
||||||
|
@ -239,10 +241,21 @@ proc service(turn: var Turn; exch: Exchange) =
|
||||||
if handler.isNone:
|
if handler.isNone:
|
||||||
stop(turn)
|
stop(turn)
|
||||||
else:
|
else:
|
||||||
publish(turn, handler.get, HttpContext(
|
let
|
||||||
|
cap = newCap(turn, exch)
|
||||||
|
ctx = publish(turn, handler.get, HttpContext(
|
||||||
req: exch.req,
|
req: exch.req,
|
||||||
res: embed newCap(turn, exch),
|
res: embed cap,
|
||||||
))
|
))
|
||||||
|
const timeout = initDuration(seconds = 4)
|
||||||
|
after(turn, exch.ses.driver.timers, timeout) do (turn: var Turn):
|
||||||
|
if not exch.active:
|
||||||
|
var res = HttpResponse(orKind: HttpResponseKind.status)
|
||||||
|
res.status.code = 504
|
||||||
|
res.status.message = "Binding timeout"
|
||||||
|
message(turn, cap, res)
|
||||||
|
res = HttpResponse(orKind: HttpResponseKind.done)
|
||||||
|
message(turn, cap, res)
|
||||||
|
|
||||||
proc service(ses: Session) =
|
proc service(ses: Session) =
|
||||||
## Service a connection to an HTTP client.
|
## Service a connection to an HTTP client.
|
||||||
|
@ -302,7 +315,8 @@ proc httpListen(turn: var Turn; driver: Driver; port: Port): Listener =
|
||||||
listener
|
listener
|
||||||
|
|
||||||
proc httpDriver(turn: var Turn; ds: Cap) =
|
proc httpDriver(turn: var Turn; ds: Cap) =
|
||||||
let driver = Driver(facet: turn.facet, ds: ds)
|
let driver = Driver(facet: turn.facet, ds: ds, timers: turn.newDataspace)
|
||||||
|
spawnTimerDriver(turn, driver.timers)
|
||||||
|
|
||||||
during(turn, ds, HttpBinding?:{
|
during(turn, ds, HttpBinding?:{
|
||||||
1: grab(),
|
1: grab(),
|
||||||
|
|
Loading…
Reference in New Issue