Skip to content

feat(server): Add server config for headers and keep alive timeouts #9309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,26 @@ Until v0.35, the default value was `schema`.
It can be also set using the [`schema_path` configuration
option](/product/configuration/reference/config#schema_path).

## `CUBEJS_SERVER_HEADERS_TIMEOUT`

The number of milliseconds to limit the amount of time the parser will wait
to receive the complete HTTP headers.
If the timeout expires, the server responds with status 408 without
forwarding the request to the request listener and then closes the connection.

| Possible Values | Default in Development | Default in Production |
| ----------------------------------------- | ------------------------ | ------------------------ |
| A valid number or string representing one | NodeJS's version default | NodeJS's version default |

## `CUBEJS_SERVER_KEEP_ALIVE_TIMEOUT`

The number of milliseconds of inactivity a server needs to wait for additional incoming data,
after it has finished writing the last response, before a socket will be destroyed.

| Possible Values | Default in Development | Default in Production |
| ----------------------------------------- | ------------------------ | ------------------------ |
| A valid number or string representing one | NodeJS's version default | NodeJS's version default |

## `CUBEJS_SQL_USER`

A username required to access the [SQL API][ref-sql-api].
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ const variables: Record<string, (...args: any) => any> = {
webSockets: () => get('CUBEJS_WEB_SOCKETS')
.default('false')
.asBoolStrict(),
serverHeadersTimeout: () => get('CUBEJS_SERVER_HEADERS_TIMEOUT')
.asInt(),
serverKeepAliveTimeout: () => get('CUBEJS_SERVER_KEEP_ALIVE_TIMEOUT')
.asInt(),
rollupOnlyMode: () => get('CUBEJS_ROLLUP_ONLY')
.default('false')
.asBoolStrict(),
Expand Down
2 changes: 2 additions & 0 deletions packages/cubejs-server-core/src/core/optionsValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const schemaOptions = Joi.object().keys({
cors: corsOptions,
}),
gracefulShutdown: Joi.number().min(0).integer(),
serverHeadersTimeout: Joi.number(),
serverKeepAliveTimeout: Joi.number(),
// Additional from WebSocketServerOptions
processSubscriptionsInterval: Joi.number(),
webSocketsBasePath: Joi.string(),
Expand Down
14 changes: 13 additions & 1 deletion packages/cubejs-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface CreateOptions extends CoreCreateOptions, WebSocketServerOptions
webSockets?: boolean;
http?: HttpOptions;
gracefulShutdown?: number;
serverKeepAliveTimeout?: number;
serverHeadersTimeout?: number;
}

type RequireOne<T, K extends keyof T> = {
Expand All @@ -47,7 +49,7 @@ type RequireOne<T, K extends keyof T> = {
export class CubejsServer {
protected readonly core: CubeCore;

protected readonly config: RequireOne<CreateOptions, 'webSockets' | 'http' | 'sqlPort' | 'pgSqlPort'>;
protected readonly config: RequireOne<CreateOptions, 'webSockets' | 'http' | 'sqlPort' | 'pgSqlPort' | 'serverHeadersTimeout' | 'serverKeepAliveTimeout'>;

protected server: GracefulHttpServer | null = null;

Expand All @@ -64,6 +66,8 @@ export class CubejsServer {
sqlPort: config.sqlPort || getEnv('sqlPort'),
pgSqlPort: config.pgSqlPort || getEnv('pgSqlPort'),
gatewayPort: config.gatewayPort || getEnv('nativeApiGatewayPort'),
serverHeadersTimeout: config.serverHeadersTimeout ?? getEnv('serverHeadersTimeout'),
serverKeepAliveTimeout: config.serverKeepAliveTimeout ?? getEnv('serverKeepAliveTimeout'),
http: {
...config.http,
cors: {
Expand Down Expand Up @@ -114,6 +118,14 @@ export class CubejsServer {
await this.sqlServer.init(this.config);
}

if (this.config.serverKeepAliveTimeout) {
this.server.keepAliveTimeout = this.config.serverKeepAliveTimeout;
}

if (this.config.serverHeadersTimeout) {
this.server.headersTimeout = this.config.serverHeadersTimeout;
}

const PORT = getEnv('port');
await this.server.listen(PORT);

Expand Down
Loading