Skip to content

Commit e9d4ff0

Browse files
committed
Update keepalive settings to agreed naming / behaviour
1 parent 7934736 commit e9d4ff0

File tree

4 files changed

+117
-26
lines changed

4 files changed

+117
-26
lines changed

src/Client/index.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ import { parseConnectionString } from "./parseConnectionString";
2222

2323
interface ClientOptions {
2424
/**
25-
* The optional amount of time to wait after which a keepalive ping is sent on the transport. (in ms)
25+
* The amount of time to wait after which a keepalive ping is sent on the transport. (in ms)
26+
* Use -1 to disable
2627
* @default 10_000
2728
*/
28-
keepAlive?: number | false;
29+
keepAliveInterval?: number;
30+
/**
31+
* The amount of time to wait after which a keepalive ping is sent on the transport. (in ms)
32+
* @default 10_000
33+
*/
34+
keepAliveTimeout?: number;
2935
/**
3036
* Whether or not to immediately throw an exception when an append fails.
3137
* @default true
@@ -82,7 +88,9 @@ export class Client {
8288
#connectionSettings: ConnectionTypeOptions;
8389
#channelCredentials: ChannelCredentials;
8490
#insecure: boolean;
85-
#keepAlive: number | false;
91+
#keepAliveInterval: number;
92+
#keepAliveTimeout: number;
93+
8694
#defaultCredentials?: Credentials;
8795

8896
#channel?: Promise<Channel>;
@@ -131,7 +139,8 @@ export class Client {
131139
gossipTimeout: options.gossipTimeout,
132140
maxDiscoverAttempts: options.maxDiscoverAttempts,
133141
throwOnAppendFailure: options.throwOnAppendFailure,
134-
keepAlive: options.keepAlive,
142+
keepAliveInterval: options.keepAliveInterval,
143+
keepAliveTimeout: options.keepAliveTimeout,
135144
},
136145
channelCredentials,
137146
options.defaultCredentials
@@ -147,7 +156,8 @@ export class Client {
147156
gossipTimeout: options.gossipTimeout,
148157
maxDiscoverAttempts: options.maxDiscoverAttempts,
149158
throwOnAppendFailure: options.throwOnAppendFailure,
150-
keepAlive: options.keepAlive,
159+
keepAliveInterval: options.keepAliveInterval,
160+
keepAliveTimeout: options.keepAliveTimeout,
151161
},
152162
channelCredentials,
153163
options.defaultCredentials
@@ -158,7 +168,8 @@ export class Client {
158168
{
159169
endpoint: options.hosts[0],
160170
throwOnAppendFailure: options.throwOnAppendFailure,
161-
keepAlive: options.keepAlive,
171+
keepAliveInterval: options.keepAliveInterval,
172+
keepAliveTimeout: options.keepAliveTimeout,
162173
},
163174
channelCredentials,
164175
options.defaultCredentials
@@ -183,14 +194,28 @@ export class Client {
183194
constructor(
184195
{
185196
throwOnAppendFailure = true,
186-
keepAlive = 10_000,
197+
keepAliveInterval = 10_000,
198+
keepAliveTimeout = 10_000,
187199
...connectionSettings
188200
}: ConnectionTypeOptions,
189201
channelCredentials: ChannelCredentialOptions = { insecure: false },
190202
defaultUserCredentials?: Credentials
191203
) {
204+
if (keepAliveInterval < -1) {
205+
throw new Error(
206+
`Invalid keepAliveInterval "${keepAliveInterval}". Please provide a positive integer, or -1 to disable.`
207+
);
208+
}
209+
210+
if (keepAliveTimeout < -1) {
211+
throw new Error(
212+
`Invalid keepAliveTimeout "${keepAliveTimeout}". Please provide a positive integer, or -1 to disable.`
213+
);
214+
}
215+
192216
this.#throwOnAppendFailure = throwOnAppendFailure;
193-
this.#keepAlive = keepAlive;
217+
this.#keepAliveInterval = keepAliveInterval;
218+
this.#keepAliveTimeout = keepAliveTimeout;
194219
this.#connectionSettings = connectionSettings;
195220
this.#insecure = !!channelCredentials.insecure;
196221
this.#defaultCredentials = defaultUserCredentials;
@@ -266,15 +291,14 @@ export class Client {
266291
uri
267292
);
268293

269-
return new Channel(
270-
uri,
271-
this.#channelCredentials,
272-
this.#keepAlive && this.#keepAlive > 0
273-
? {
274-
"grpc.keepalive_time_ms": this.#keepAlive,
275-
}
276-
: {}
277-
);
294+
return new Channel(uri, this.#channelCredentials, {
295+
"grpc.keepalive_time_ms":
296+
this.#keepAliveInterval < 0
297+
? Number.MAX_VALUE
298+
: this.#keepAliveInterval,
299+
"grpc.keepalive_timeout_ms":
300+
this.#keepAliveTimeout < 0 ? Number.MAX_VALUE : this.#keepAliveTimeout,
301+
});
278302
};
279303

280304
private resolveUri = async (): Promise<string> => {

src/Client/parseConnectionString.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const lowerToKey: Record<string, keyof ConnectionOptions> = {
1212
tls: "tls",
1313
tlsverifycert: "tlsVerifyCert",
1414
throwonappendfailure: "throwOnAppendFailure",
15-
keepalive: "keepAlive",
15+
keepaliveinterval: "keepAliveInterval",
16+
keepalivetimeout: "keepAliveTimeout",
1617
};
1718

1819
export interface ConnectionOptions {
@@ -24,7 +25,9 @@ export interface ConnectionOptions {
2425
tls: boolean;
2526
tlsVerifyCert: boolean;
2627
throwOnAppendFailure: boolean;
27-
keepAlive?: number;
28+
keepAliveInterval: number;
29+
keepAliveTimeout: number;
30+
2831
defaultCredentials?: Credentials;
2932
hosts: EndPoint[];
3033
}
@@ -37,6 +40,8 @@ const defaultConnectionOptions: ConnectionOptions = {
3740
nodePreference: "random",
3841
tls: true,
3942
tlsVerifyCert: true,
43+
keepAliveInterval: 10_000,
44+
keepAliveTimeout: 10_000,
4045
throwOnAppendFailure: true,
4146
hosts: [],
4247
};
@@ -255,7 +260,8 @@ const verifyKeyValuePair = (
255260
case "maxDiscoverAttempts":
256261
case "discoveryInterval":
257262
case "gossipTimeout":
258-
case "keepAlive": {
263+
case "keepAliveInterval":
264+
case "keepAliveTimeout": {
259265
const parsedValue = parseInt(value);
260266

261267
if (Number.isNaN(parsedValue)) {

src/__test__/connection/__snapshots__/parseConnectionString.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`connection string parser Should throw on invalid strings esdb://host1;h
88

99
exports[`connection string parser Should throw on invalid strings esdb://localhost/&tlsVerifyCert=false 1`] = `"Unexpected \\"&\\" at position 17, expected ?key=value."`;
1010

11-
exports[`connection string parser Should throw on invalid strings esdb://localhost?keepAlive=please 1`] = `"Unexpected \\"please\\" at position 27, expected Integer."`;
11+
exports[`connection string parser Should throw on invalid strings esdb://localhost?keepAliveTimeout=please 1`] = `"Unexpected \\"please\\" at position 34, expected Integer."`;
1212

1313
exports[`connection string parser Should throw on invalid strings esdb://localhost?throwOnAppendFailure=sometimes 1`] = `"Unexpected \\"sometimes\\" at position 38, expected true or false."`;
1414

0 commit comments

Comments
 (0)