Skip to content

Commit 8fd2c42

Browse files
feat(server): Add server config for headers and keep alive timeouts (#9309)
* feat: add new env vars * fix: add vars to interface * fix: add vars to schema options
1 parent 59210da commit 8fd2c42

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

docs/pages/product/configuration/reference/environment-variables.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,26 @@ Until v0.35, the default value was `schema`.
11361136
It can be also set using the [`schema_path` configuration
11371137
option](/product/configuration/reference/config#schema_path).
11381138

1139+
## `CUBEJS_SERVER_HEADERS_TIMEOUT`
1140+
1141+
The number of milliseconds to limit the amount of time the parser will wait
1142+
to receive the complete HTTP headers.
1143+
If the timeout expires, the server responds with status 408 without
1144+
forwarding the request to the request listener and then closes the connection.
1145+
1146+
| Possible Values | Default in Development | Default in Production |
1147+
| ----------------------------------------- | ------------------------ | ------------------------ |
1148+
| A valid number or string representing one | NodeJS's version default | NodeJS's version default |
1149+
1150+
## `CUBEJS_SERVER_KEEP_ALIVE_TIMEOUT`
1151+
1152+
The number of milliseconds of inactivity a server needs to wait for additional incoming data,
1153+
after it has finished writing the last response, before a socket will be destroyed.
1154+
1155+
| Possible Values | Default in Development | Default in Production |
1156+
| ----------------------------------------- | ------------------------ | ------------------------ |
1157+
| A valid number or string representing one | NodeJS's version default | NodeJS's version default |
1158+
11391159
## `CUBEJS_SQL_USER`
11401160

11411161
A username required to access the [SQL API][ref-sql-api].

packages/cubejs-backend-shared/src/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ const variables: Record<string, (...args: any) => any> = {
144144
webSockets: () => get('CUBEJS_WEB_SOCKETS')
145145
.default('false')
146146
.asBoolStrict(),
147+
serverHeadersTimeout: () => get('CUBEJS_SERVER_HEADERS_TIMEOUT')
148+
.asInt(),
149+
serverKeepAliveTimeout: () => get('CUBEJS_SERVER_KEEP_ALIVE_TIMEOUT')
150+
.asInt(),
147151
rollupOnlyMode: () => get('CUBEJS_ROLLUP_ONLY')
148152
.default('false')
149153
.asBoolStrict(),

packages/cubejs-server-core/src/core/optionsValidate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const schemaOptions = Joi.object().keys({
5252
cors: corsOptions,
5353
}),
5454
gracefulShutdown: Joi.number().min(0).integer(),
55+
serverHeadersTimeout: Joi.number(),
56+
serverKeepAliveTimeout: Joi.number(),
5557
// Additional from WebSocketServerOptions
5658
processSubscriptionsInterval: Joi.number(),
5759
webSocketsBasePath: Joi.string(),

packages/cubejs-server/src/server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export interface CreateOptions extends CoreCreateOptions, WebSocketServerOptions
3636
webSockets?: boolean;
3737
http?: HttpOptions;
3838
gracefulShutdown?: number;
39+
serverKeepAliveTimeout?: number;
40+
serverHeadersTimeout?: number;
3941
}
4042

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

50-
protected readonly config: RequireOne<CreateOptions, 'webSockets' | 'http' | 'sqlPort' | 'pgSqlPort'>;
52+
protected readonly config: RequireOne<CreateOptions, 'webSockets' | 'http' | 'sqlPort' | 'pgSqlPort' | 'serverHeadersTimeout' | 'serverKeepAliveTimeout'>;
5153

5254
protected server: GracefulHttpServer | null = null;
5355

@@ -64,6 +66,8 @@ export class CubejsServer {
6466
sqlPort: config.sqlPort || getEnv('sqlPort'),
6567
pgSqlPort: config.pgSqlPort || getEnv('pgSqlPort'),
6668
gatewayPort: config.gatewayPort || getEnv('nativeApiGatewayPort'),
69+
serverHeadersTimeout: config.serverHeadersTimeout ?? getEnv('serverHeadersTimeout'),
70+
serverKeepAliveTimeout: config.serverKeepAliveTimeout ?? getEnv('serverKeepAliveTimeout'),
6771
http: {
6872
...config.http,
6973
cors: {
@@ -114,6 +118,14 @@ export class CubejsServer {
114118
await this.sqlServer.init(this.config);
115119
}
116120

121+
if (this.config.serverKeepAliveTimeout) {
122+
this.server.keepAliveTimeout = this.config.serverKeepAliveTimeout;
123+
}
124+
125+
if (this.config.serverHeadersTimeout) {
126+
this.server.headersTimeout = this.config.serverHeadersTimeout;
127+
}
128+
117129
const PORT = getEnv('port');
118130
await this.server.listen(PORT);
119131

0 commit comments

Comments
 (0)