Skip to content

Commit 89cd8f7

Browse files
committed
grpc-js: Idle timeout: format files
1 parent fcff72b commit 89cd8f7

File tree

4 files changed

+84
-29
lines changed

4 files changed

+84
-29
lines changed

packages/grpc-js/src/channel-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const recognizedOptions = {
9090
'grpc.max_connection_age_grace_ms': true,
9191
'grpc-node.max_session_memory': true,
9292
'grpc.service_config_disable_resolution': true,
93-
'grpc.client_idle_timeout_ms': true
93+
'grpc.client_idle_timeout_ms': true,
9494
};
9595

9696
export function channelOptionsEqual(

packages/grpc-js/src/internal-channel.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import { ChannelOptions } from './channel-options';
2020
import { ResolvingLoadBalancer } from './resolving-load-balancer';
2121
import { SubchannelPool, getSubchannelPool } from './subchannel-pool';
2222
import { ChannelControlHelper } from './load-balancer';
23-
import { UnavailablePicker, Picker, PickResultType, QueuePicker } from './picker';
23+
import {
24+
UnavailablePicker,
25+
Picker,
26+
PickResultType,
27+
QueuePicker,
28+
} from './picker';
2429
import { Metadata } from './metadata';
2530
import { Status, LogVerbosity, Propagate } from './constants';
2631
import { FilterStackFactory } from './filter-stack';
@@ -191,9 +196,10 @@ export class InternalChannel {
191196
private currentResolutionError: StatusObject | null = null;
192197
private readonly retryBufferTracker: MessageBufferTracker;
193198
private keepaliveTime: number;
194-
private readonly wrappedSubchannels: Set<ChannelSubchannelWrapper> = new Set();
199+
private readonly wrappedSubchannels: Set<ChannelSubchannelWrapper> =
200+
new Set();
195201

196-
private callCount: number = 0;
202+
private callCount = 0;
197203
private idleTimer: NodeJS.Timer | null = null;
198204
private readonly idleTimeoutMs: number;
199205

@@ -274,7 +280,10 @@ export class InternalChannel {
274280
DEFAULT_PER_RPC_RETRY_BUFFER_SIZE_BYTES
275281
);
276282
this.keepaliveTime = options['grpc.keepalive_time_ms'] ?? -1;
277-
this.idleTimeoutMs = Math.max(options['grpc.client_idle_timeout_ms'] ?? DEFAULT_IDLE_TIMEOUT_MS, MIN_IDLE_TIMEOUT_MS);
283+
this.idleTimeoutMs = Math.max(
284+
options['grpc.client_idle_timeout_ms'] ?? DEFAULT_IDLE_TIMEOUT_MS,
285+
MIN_IDLE_TIMEOUT_MS
286+
);
278287
const channelControlHelper: ChannelControlHelper = {
279288
createSubchannel: (
280289
subchannelAddress: SubchannelAddress,
@@ -567,7 +576,11 @@ export class InternalChannel {
567576
private maybeStartIdleTimer() {
568577
if (this.callCount === 0) {
569578
this.idleTimer = setTimeout(() => {
570-
this.trace('Idle timer triggered after ' + this.idleTimeoutMs + 'ms of inactivity');
579+
this.trace(
580+
'Idle timer triggered after ' +
581+
this.idleTimeoutMs +
582+
'ms of inactivity'
583+
);
571584
this.enterIdle();
572585
}, this.idleTimeoutMs);
573586
this.idleTimer.unref?.();

packages/grpc-js/test/common.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import * as assert2 from './assert2';
2020
import * as path from 'path';
2121
import * as grpc from '../src';
2222

23-
import { GrpcObject, ServiceClientConstructor, ServiceClient, loadPackageDefinition } from '../src/make-client';
23+
import {
24+
GrpcObject,
25+
ServiceClientConstructor,
26+
ServiceClient,
27+
loadPackageDefinition,
28+
} from '../src/make-client';
2429
import { readFileSync } from 'fs';
2530

2631
const protoLoaderOptions = {
@@ -67,7 +72,9 @@ export class TestServer {
6772
start(): Promise<void> {
6873
let credentials: grpc.ServerCredentials;
6974
if (this.useTls) {
70-
credentials = grpc.ServerCredentials.createSsl(null, [{private_key: key, cert_chain: cert}]);
75+
credentials = grpc.ServerCredentials.createSsl(null, [
76+
{ private_key: key, cert_chain: cert },
77+
]);
7178
} else {
7279
credentials = grpc.ServerCredentials.createInsecure();
7380
}

packages/grpc-js/test/test-idle-timer.ts

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import * as assert from 'assert';
1919
import * as grpc from '../src';
20-
import { TestClient, TestServer } from "./common";
20+
import { TestClient, TestServer } from './common';
2121

2222
describe('Channel idle timer', () => {
2323
let server: TestServer;
@@ -31,65 +31,100 @@ describe('Channel idle timer', () => {
3131
client.close();
3232
client = null;
3333
}
34-
})
34+
});
3535
after(() => {
3636
server.shutdown();
3737
});
38-
it('Should go idle after the specified time after a request ends', function(done) {
38+
it('Should go idle after the specified time after a request ends', function (done) {
3939
this.timeout(5000);
40-
client = TestClient.createFromServer(server, {'grpc.client_idle_timeout_ms': 1000});
40+
client = TestClient.createFromServer(server, {
41+
'grpc.client_idle_timeout_ms': 1000,
42+
});
4143
client.sendRequest(error => {
4244
assert.ifError(error);
43-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.READY);
45+
assert.strictEqual(
46+
client!.getChannelState(),
47+
grpc.connectivityState.READY
48+
);
4449
setTimeout(() => {
45-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.IDLE);
50+
assert.strictEqual(
51+
client!.getChannelState(),
52+
grpc.connectivityState.IDLE
53+
);
4654
done();
4755
}, 1100);
4856
});
4957
});
50-
it('Should be able to make a request after going idle', function(done) {
58+
it('Should be able to make a request after going idle', function (done) {
5159
this.timeout(5000);
52-
client = TestClient.createFromServer(server, {'grpc.client_idle_timeout_ms': 1000});
60+
client = TestClient.createFromServer(server, {
61+
'grpc.client_idle_timeout_ms': 1000,
62+
});
5363
client.sendRequest(error => {
5464
assert.ifError(error);
55-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.READY);
65+
assert.strictEqual(
66+
client!.getChannelState(),
67+
grpc.connectivityState.READY
68+
);
5669
setTimeout(() => {
57-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.IDLE);
70+
assert.strictEqual(
71+
client!.getChannelState(),
72+
grpc.connectivityState.IDLE
73+
);
5874
client!.sendRequest(error => {
5975
assert.ifError(error);
6076
done();
6177
});
6278
}, 1100);
6379
});
6480
});
65-
it('Should go idle after the specified time after waitForReady ends', function(done) {
81+
it('Should go idle after the specified time after waitForReady ends', function (done) {
6682
this.timeout(5000);
67-
client = TestClient.createFromServer(server, {'grpc.client_idle_timeout_ms': 1000});
83+
client = TestClient.createFromServer(server, {
84+
'grpc.client_idle_timeout_ms': 1000,
85+
});
6886
const deadline = new Date();
6987
deadline.setSeconds(deadline.getSeconds() + 3);
7088
client.waitForReady(deadline, error => {
7189
assert.ifError(error);
72-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.READY);
90+
assert.strictEqual(
91+
client!.getChannelState(),
92+
grpc.connectivityState.READY
93+
);
7394
setTimeout(() => {
74-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.IDLE);
95+
assert.strictEqual(
96+
client!.getChannelState(),
97+
grpc.connectivityState.IDLE
98+
);
7599
done();
76100
}, 1100);
77101
});
78102
});
79-
it('Should ensure that the timeout is at least 1 second', function(done) {
80-
client = TestClient.createFromServer(server, {'grpc.client_idle_timeout_ms': 50});
103+
it('Should ensure that the timeout is at least 1 second', function (done) {
104+
client = TestClient.createFromServer(server, {
105+
'grpc.client_idle_timeout_ms': 50,
106+
});
81107
client.sendRequest(error => {
82108
assert.ifError(error);
83-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.READY);
109+
assert.strictEqual(
110+
client!.getChannelState(),
111+
grpc.connectivityState.READY
112+
);
84113
setTimeout(() => {
85114
// Should still be ready after 100ms
86-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.READY);
115+
assert.strictEqual(
116+
client!.getChannelState(),
117+
grpc.connectivityState.READY
118+
);
87119
setTimeout(() => {
88120
// Should go IDLE after another second
89-
assert.strictEqual(client!.getChannelState(), grpc.connectivityState.IDLE);
121+
assert.strictEqual(
122+
client!.getChannelState(),
123+
grpc.connectivityState.IDLE
124+
);
90125
done();
91126
}, 1000);
92127
}, 100);
93128
});
94-
})
95-
});
129+
});
130+
});

0 commit comments

Comments
 (0)