Skip to content

Commit 0c6042e

Browse files
Robert Baileypooneh-m
authored andcommitted
Use an environment variable to dynamically set the grpc port in (agones-dev#1093)
the nodejs sdk.
1 parent e633559 commit 0c6042e

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎sdks/nodejs/spec/agonesSDK.spec.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ describe('agones', () => {
2626
agonesSDK = new AgonesSDK();
2727
});
2828

29+
describe('port', () => {
30+
it('returns the default port if $AGONES_SDK_GRPC_PORT is not defined', async () => {
31+
let port = agonesSDK.port;
32+
expect(port).toEqual('59357');
33+
});
34+
35+
it('returns the default port if $AGONES_SDK_GRPC_PORT is not an integer', async () => {
36+
process.env.AGONES_SDK_GRPC_PORT = "random string";
37+
let port = agonesSDK.port;
38+
expect(port).toEqual('59357');
39+
});
40+
41+
it('returns the default port if $AGONES_SDK_GRPC_PORT is to large of an integer', async () => {
42+
process.env.AGONES_SDK_GRPC_PORT = '4455667788';
43+
let port = agonesSDK.port;
44+
expect(port).toEqual('59357');
45+
});
46+
47+
it('returns the default port if $AGONES_SDK_GRPC_PORT is to small of an integer', async () => {
48+
process.env.AGONES_SDK_GRPC_PORT = '0';
49+
let port = agonesSDK.port;
50+
expect(port).toEqual('59357');
51+
});
52+
53+
it('returns the port set in $AGONES_SDK_GRPC_PORT', async () => {
54+
process.env.AGONES_SDK_GRPC_PORT = '6789';
55+
let port = agonesSDK.port;
56+
expect(port).toEqual('6789');
57+
});
58+
});
59+
2960
describe('connect', () => {
3061
it('calls the server and handles success', async () => {
3162
spyOn(agonesSDK.client, 'waitForReady').and.callFake((deadline, callback) => {

‎sdks/nodejs/src/agonesSDK.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,30 @@ const services = require('../lib/sdk_grpc_pb');
1919

2020
class AgonesSDK {
2121
constructor() {
22-
this.client = new services.SDKClient('localhost:59357', grpc.credentials.createInsecure());
22+
this.client = new services.SDKClient('localhost:'+this.port, grpc.credentials.createInsecure());
2323
this.healthStream = undefined;
2424
this.emitters = [];
2525
}
2626

27+
get port() {
28+
const defaultPort = '59357';
29+
const port = process.env.AGONES_SDK_GRPC_PORT;
30+
if (port === undefined) {
31+
console.log(`Environment variable AGONES_SDK_GRPC_PORT not defined, using default port ${defaultPort}`);
32+
return defaultPort;
33+
}
34+
const portNum = parseInt(port, 10);
35+
if (isNaN(portNum)) {
36+
console.log(`Unable to parse '${port}' defined in AGONES_SDK_GRPC_PORT into an integer`);
37+
return defaultPort;
38+
}
39+
if (portNum < 1 || portNum > 65535) {
40+
console.log(`Invalid port ${portNum} defined in AGONES_SDK_GRPC_PORT. It must be between 1 and 65535`);
41+
return defaultPort;
42+
}
43+
return port;
44+
}
45+
2746
async connect() {
2847
return new Promise((resolve, reject) => {
2948
this.client.waitForReady(Date.now() + 30000, (error) => {
@@ -32,7 +51,7 @@ class AgonesSDK {
3251
} else {
3352
resolve();
3453
}
35-
})
54+
});
3655
});
3756
}
3857

0 commit comments

Comments
 (0)