Add support for request timeouts; timeout helper
This commit is contained in:
@@ -4,6 +4,10 @@ import Kernel from '../http/kernel/Kernel.ts'
|
||||
import {Logging} from '../service/logging/Logging.ts'
|
||||
import {serve} from '../external/http.ts'
|
||||
import {Request} from '../http/Request.ts'
|
||||
import {withTimeout} from '../support/timeout.ts'
|
||||
import {http} from '../http/response/helpers.ts'
|
||||
import {HTTPStatus} from '../const/http.ts'
|
||||
import Config from './Config.ts'
|
||||
|
||||
@Unit()
|
||||
export default class HttpServer extends LifecycleUnit {
|
||||
@@ -11,6 +15,7 @@ export default class HttpServer extends LifecycleUnit {
|
||||
|
||||
constructor(
|
||||
protected readonly kernel: Kernel,
|
||||
protected readonly config: Config,
|
||||
protected readonly logger: Logging,
|
||||
) {
|
||||
super()
|
||||
@@ -20,10 +25,24 @@ export default class HttpServer extends LifecycleUnit {
|
||||
this._server = serve({ port: 8000 })
|
||||
this.logger.success(`HTTP/S server listening on port 8000!`)
|
||||
|
||||
const request_timeout: number = this.config.get('server.request_timeout', 15000)
|
||||
|
||||
for await ( const native_request of this._server ) {
|
||||
let req: Request = this.make(Request, native_request)
|
||||
req = await this.kernel.handle(req)
|
||||
req.response.send()
|
||||
|
||||
await withTimeout(request_timeout, this.kernel.handle(req))
|
||||
.on_time(output_req => {
|
||||
output_req.response.send()
|
||||
})
|
||||
.late(output_req => {
|
||||
this.logger.error(`Request timed out. Response was already sent on path: ${req.path}. Consider increasing server.request_timeout.`)
|
||||
})
|
||||
.timeout(async () => {
|
||||
const factory = http(HTTPStatus.REQUEST_TIMEOUT)
|
||||
const output_req = await factory.write(req)
|
||||
output_req.response.send()
|
||||
})
|
||||
.run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user