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

Keep alives #135

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
53 changes: 52 additions & 1 deletion src/Client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ import { discoverEndpoint } from "./discovery";
import { parseConnectionString } from "./parseConnectionString";

interface ClientOptions {
/**
* The amount of time (in milliseconds) to wait after which a keepalive ping is sent on the transport.
* Use -1 to disable.
* @default 10_000
*/
keepAliveInterval?: number;
/**
* The amount of time (in milliseconds) the sender of the keepalive ping waits for an acknowledgement.
* If it does not receive an acknowledgment within this time, it will close the connection.
* @default 10_000
*/
keepAliveTimeout?: number;
/**
* Whether or not to immediately throw an exception when an append fails.
* @default true
*/
throwOnAppendFailure?: boolean;
}

Expand Down Expand Up @@ -73,6 +89,9 @@ export class Client {
#connectionSettings: ConnectionTypeOptions;
#channelCredentials: ChannelCredentials;
#insecure: boolean;
#keepAliveInterval: number;
#keepAliveTimeout: number;

#defaultCredentials?: Credentials;

#channel?: Promise<Channel>;
Expand Down Expand Up @@ -120,6 +139,9 @@ export class Client {
discoveryInterval: options.discoveryInterval,
gossipTimeout: options.gossipTimeout,
maxDiscoverAttempts: options.maxDiscoverAttempts,
throwOnAppendFailure: options.throwOnAppendFailure,
keepAliveInterval: options.keepAliveInterval,
keepAliveTimeout: options.keepAliveTimeout,
},
channelCredentials,
options.defaultCredentials
Expand All @@ -134,6 +156,9 @@ export class Client {
discoveryInterval: options.discoveryInterval,
gossipTimeout: options.gossipTimeout,
maxDiscoverAttempts: options.maxDiscoverAttempts,
throwOnAppendFailure: options.throwOnAppendFailure,
keepAliveInterval: options.keepAliveInterval,
keepAliveTimeout: options.keepAliveTimeout,
},
channelCredentials,
options.defaultCredentials
Expand All @@ -143,6 +168,9 @@ export class Client {
return new Client(
{
endpoint: options.hosts[0],
throwOnAppendFailure: options.throwOnAppendFailure,
keepAliveInterval: options.keepAliveInterval,
keepAliveTimeout: options.keepAliveTimeout,
},
channelCredentials,
options.defaultCredentials
Expand All @@ -167,12 +195,28 @@ export class Client {
constructor(
{
throwOnAppendFailure = true,
keepAliveInterval = 10_000,
keepAliveTimeout = 10_000,
...connectionSettings
}: ConnectionTypeOptions,
channelCredentials: ChannelCredentialOptions = { insecure: false },
defaultUserCredentials?: Credentials
) {
if (keepAliveInterval < -1) {
throw new Error(
`Invalid keepAliveInterval "${keepAliveInterval}". Please provide a positive integer, or -1 to disable.`
);
}

if (keepAliveTimeout < -1) {
throw new Error(
`Invalid keepAliveTimeout "${keepAliveTimeout}". Please provide a positive integer, or -1 to disable.`
);
}

this.#throwOnAppendFailure = throwOnAppendFailure;
this.#keepAliveInterval = keepAliveInterval;
this.#keepAliveTimeout = keepAliveTimeout;
this.#connectionSettings = connectionSettings;
this.#insecure = !!channelCredentials.insecure;
this.#defaultCredentials = defaultUserCredentials;
Expand Down Expand Up @@ -248,7 +292,14 @@ export class Client {
uri
);

return new Channel(uri, this.#channelCredentials, {});
return new Channel(uri, this.#channelCredentials, {
"grpc.keepalive_time_ms":
this.#keepAliveInterval < 0
? Number.MAX_VALUE
: this.#keepAliveInterval,
"grpc.keepalive_timeout_ms":
this.#keepAliveTimeout < 0 ? Number.MAX_VALUE : this.#keepAliveTimeout,
});
};

private resolveUri = async (): Promise<string> => {
Expand Down
11 changes: 10 additions & 1 deletion src/Client/parseConnectionString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const lowerToKey: Record<string, keyof ConnectionOptions> = {
tls: "tls",
tlsverifycert: "tlsVerifyCert",
throwonappendfailure: "throwOnAppendFailure",
keepaliveinterval: "keepAliveInterval",
keepalivetimeout: "keepAliveTimeout",
};

export interface ConnectionOptions {
Expand All @@ -23,6 +25,9 @@ export interface ConnectionOptions {
tls: boolean;
tlsVerifyCert: boolean;
throwOnAppendFailure: boolean;
keepAliveInterval: number;
keepAliveTimeout: number;

defaultCredentials?: Credentials;
hosts: EndPoint[];
}
Expand All @@ -35,6 +40,8 @@ const defaultConnectionOptions: ConnectionOptions = {
nodePreference: "random",
tls: true,
tlsVerifyCert: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
throwOnAppendFailure: true,
hosts: [],
};
Expand Down Expand Up @@ -252,7 +259,9 @@ const verifyKeyValuePair = (
}
case "maxDiscoverAttempts":
case "discoveryInterval":
case "gossipTimeout": {
case "gossipTimeout":
case "keepAliveInterval":
case "keepAliveTimeout": {
const parsedValue = parseInt(value);

if (Number.isNaN(parsedValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ exports[`connection string parser Should throw on invalid strings esdb://host1;h

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

exports[`connection string parser Should throw on invalid strings esdb://localhost?keepAliveTimeout=please 1`] = `"Unexpected \\"please\\" at position 34, expected Integer."`;
George-Payne marked this conversation as resolved.
Show resolved Hide resolved

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

exports[`connection string parser Should throw on invalid strings esdb://localhost?tlsVerifyCert=false&nodePreference=any 1`] = `"Unexpected \\"any\\" at position 52, expected follower or leader or random."`;
Expand Down
68 changes: 66 additions & 2 deletions src/__test__/connection/connectionStringMockups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "localhost",
Expand All @@ -33,6 +35,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "localhost",
Expand All @@ -52,6 +56,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -75,6 +81,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -98,6 +106,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -121,6 +131,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -144,6 +156,8 @@ export const valid: Array<
tls: false,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -167,6 +181,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host1",
Expand Down Expand Up @@ -194,6 +210,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host1",
Expand Down Expand Up @@ -221,6 +239,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "bubaqp2rh41uf5akmj0g-0.mesdb.eventstore.cloud",
Expand Down Expand Up @@ -248,6 +268,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand Down Expand Up @@ -279,6 +301,8 @@ export const valid: Array<
tls: false,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host1",
Expand Down Expand Up @@ -306,6 +330,8 @@ export const valid: Array<
tls: false,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "127.0.0.1",
Expand All @@ -325,6 +351,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host1",
Expand Down Expand Up @@ -352,6 +380,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -364,6 +394,27 @@ export const valid: Array<
],
},
],
[
"esdb://host?keepAliveInterval=-1&keepAliveTimeout=-1",
{
dnsDiscover: false,
maxDiscoverAttempts: 3,
discoveryInterval: 500,
gossipTimeout: 3000,
nodePreference: "random",
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: -1,
keepAliveTimeout: -1,
hosts: [
{
address: "host",
port: 2113,
},
],
},
],
[
"esdb://my%3Agreat%40username:UyeXx8%24%5EPsOo4jG88FlCauR1Coz25q@host?nodePreference=follower&tlsVerifyCert=false",
{
Expand All @@ -375,6 +426,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "my:great@username",
password: "UyeXx8$^PsOo4jG88FlCauR1Coz25q",
Expand All @@ -398,6 +451,8 @@ export const valid: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
defaultCredentials: {
username: "user",
password: "pass",
Expand All @@ -415,7 +470,7 @@ export const valid: Array<
},
],
[
"esdb://host?maxDiscoverAttempts=200&discoveryInterval=1000&gossipTimeout=1&nodePreference=leader&tls=false&tlsVerifyCert=false&throwOnAppendFailure=false",
"esdb://host?maxDiscoverAttempts=200&discoveryInterval=1000&gossipTimeout=1&nodePreference=leader&tls=false&tlsVerifyCert=false&throwOnAppendFailure=false&keepAliveInterval=10",
George-Payne marked this conversation as resolved.
Show resolved Hide resolved
{
dnsDiscover: false,
maxDiscoverAttempts: 200,
Expand All @@ -425,6 +480,8 @@ export const valid: Array<
tls: false,
tlsVerifyCert: false,
throwOnAppendFailure: false,
keepAliveInterval: 10,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host",
Expand All @@ -434,7 +491,7 @@ export const valid: Array<
},
],
[
"esdb://host?MaxDiscoverAttempts=200&discoveryinterval=1000&GOSSIPTIMEOUT=1&nOdEpReFeReNcE=leader&TLS=false&TlsVerifyCert=false&THROWOnAppendFailure=false",
"esdb://host?MaxDiscoverAttempts=200&discoveryinterval=1000&GOSSIPTIMEOUT=1&nOdEpReFeReNcE=leader&TLS=false&TlsVerifyCert=false&THROWOnAppendFailure=false&KEEPALIVEinterval=200",
{
dnsDiscover: false,
maxDiscoverAttempts: 200,
Expand All @@ -444,6 +501,8 @@ export const valid: Array<
tls: false,
tlsVerifyCert: false,
throwOnAppendFailure: false,
keepAliveInterval: 200,
keepAliveTimeout: 10_000,
hosts: [
{
address: "host",
Expand All @@ -467,6 +526,7 @@ export const invalid: string[] = [
"esdb://localhost?tlsVerifyCert=false&nodePreference=any",
"esdb://localhost?tlsVerifyCert=if you feel like it",
"esdb://localhost?throwOnAppendFailure=sometimes",
"esdb://localhost?keepAliveTimeout=please",
];

export const warning: Array<
Expand All @@ -483,6 +543,8 @@ export const warning: Array<
tls: true,
tlsVerifyCert: false,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "localhost",
Expand All @@ -502,6 +564,8 @@ export const warning: Array<
tls: true,
tlsVerifyCert: true,
throwOnAppendFailure: true,
keepAliveInterval: 10_000,
keepAliveTimeout: 10_000,
hosts: [
{
address: "localhost",
Expand Down