The Crystal Programming Language Forum

Timeout Http Server middleware?

I’m interested into something like https://expressjs.com/en/resources/middleware/timeout.html for kemal, but kind of assuming crystal http middlewares are generic for everything (including require "http/server", amber, lucky).

has someone written one? Maybe it should be a property of http/Server itself?

what does a semantically correct timeout middleware look like in crystal? Will it throw 408 request timeout? will the longer-than-expected request still keep running in the background or will it be terminated?

You would probably spawn a new fiber which takes over processing the remaining handler chain. If the fiber doesn’t terminate before a timeout, a request timeout should be returned. There needs to be some kind of mechanism to ensure that the work fiber doesn’t write the response simultaneously.

Appears the node one just lets it keep running in the background "While the library will emit a ‘timeout’ event when requests exceed the given timeout, node will continue processing the slow request until it terminates. " Which doesn’t seem optimal. Anyway if you want timeouts I’d suggest putting an nginx proxy on the front. Easy peasy :)