Skip to content
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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions docs/pages/reference/configuration/environment-variables.mdx
Original file line number Diff line number Diff line change
@@ -1061,6 +1061,26 @@ Until v0.35, the default value was `schema`.
It can be also set using the [`schema_path` configuration
option](/reference/configuration/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 | 60000 | 60000 |

## `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 | 5000 | 5000 |

## `CUBEJS_SQL_USER`

A username required to access the [SQL API][ref-sql-api].
6 changes: 6 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
@@ -144,6 +144,12 @@ const variables: Record<string, (...args: any) => any> = {
webSockets: () => get('CUBEJS_WEB_SOCKETS')
.default('false')
.asBoolStrict(),
serverHeadersTimeout: () => get('CUBEJS_SERVER_HEADERS_TIMEOUT')
.default('60000')
.asInt(),
serverKeepAliveTimeout: () => get('CUBEJS_SERVER_KEEP_ALIVE_TIMEOUT')
.default('5000')
.asInt(),
rollupOnlyMode: () => get('CUBEJS_ROLLUP_ONLY')
.default('false')
.asBoolStrict(),
11 changes: 10 additions & 1 deletion packages/cubejs-server/src/server.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,11 @@ interface HttpOptions {

export interface CreateOptions extends CoreCreateOptions, WebSocketServerOptions, SQLServerOptions {
webSockets?: boolean;
sqlPort?: number;
pgSqlPort?: number;
gatewayPort?: number;
serverKeepAliveTimeout?: number;
serverHeadersTimeout?: number;
http?: HttpOptions;
gracefulShutdown?: number;
}
@@ -47,7 +52,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' | 'serverKeepAliveTimeout' | 'serverHeadersTimeout'>;

protected server: GracefulHttpServer | null = null;

@@ -64,6 +69,8 @@ export class CubejsServer {
sqlPort: config.sqlPort || getEnv('sqlPort'),
pgSqlPort: config.pgSqlPort || getEnv('pgSqlPort'),
gatewayPort: config.gatewayPort || getEnv('nativeApiGatewayPort'),
serverKeepAliveTimeout: config.serverKeepAliveTimeout || getEnv('serverKeepAliveTimeout'),
serverHeadersTimeout: config.serverHeadersTimeout || getEnv('serverHeadersTimeout'),
http: {
...config.http,
cors: {
@@ -103,6 +110,8 @@ export class CubejsServer {
}

this.server = gracefulHttp(http.createServer(options, app));
this.server.keepAliveTimeout = this.config.serverKeepAliveTimeout;
this.server.headersTimeout = this.config.serverKeepAliveTimeout;

if (this.config.webSockets) {
this.socketServer = new WebSocketServer(this.core, this.config);