response lifecycle timeout and route handling

This commit is contained in:
2021-03-08 12:34:31 -06:00
parent 9747d40659
commit 4ecada6be8
11 changed files with 92 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import {Singleton, Inject} from "@extollo/di"
import {Inject, Singleton} from "@extollo/di"
import {HTTPStatus, withTimeout} from "@extollo/util"
import {Unit} from "../lifecycle/Unit";
import {createServer, IncomingMessage, ServerResponse, Server} from "http";
import {createServer, IncomingMessage, Server, ServerResponse} from "http";
import {Logging} from "./Logging";
import {Request} from "../http/lifecycle/Request";
import {HTTPKernel} from "../http/kernel/HTTPKernel";
@@ -9,6 +10,7 @@ import {SetSessionCookieHTTPModule} from "../http/kernel/module/SetSessionCookie
import {InjectSessionHTTPModule} from "../http/kernel/module/InjectSessionHTTPModule";
import {PersistSessionHTTPModule} from "../http/kernel/module/PersistSessionHTTPModule";
import {MountActivatedRouteHTTPModule} from "../http/kernel/module/MountActivatedRouteHTTPModule";
import {ExecuteResolvedRouteHandlerHTTPModule} from "../http/kernel/module/ExecuteResolvedRouteHandlerHTTPModule";
@Singleton()
export class HTTPServer extends Unit {
@@ -29,6 +31,7 @@ export class HTTPServer extends Unit {
InjectSessionHTTPModule.register(this.kernel)
PersistSessionHTTPModule.register(this.kernel)
MountActivatedRouteHTTPModule.register(this.kernel)
ExecuteResolvedRouteHandlerHTTPModule.register(this.kernel)
await new Promise<void>((res, rej) => {
this.server = createServer(this.handler)
@@ -55,8 +58,26 @@ export class HTTPServer extends Unit {
public get handler() {
return async (request: IncomingMessage, response: ServerResponse) => {
const extolloReq = new Request(request, response)
// FIXME make timeout configurable
withTimeout(10000, extolloReq.response.sent$.toPromise())
.onTime(req => {
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
})
.late(req => {
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
})
.timeout(() => {
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
extolloReq.response.body = 'Sorry, your request timed out.'
extolloReq.response.send()
})
.run()
.catch(e => this.logging.error(e))
await this.kernel.handle(extolloReq)
await extolloReq.response.send('Hi, from Extollo!!')
await extolloReq.response.send()
}
}
}