Skip to content

Commit cf321a8

Browse files
committed
chore: use iterators for tracking map, const for default values
1 parent 62e8ea9 commit cf321a8

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

packages/grpc-js/src/channelz.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ interface TraceEvent {
133133
*/
134134
const TARGET_RETAINED_TRACES = 32;
135135

136+
/**
137+
* Default number of sockets/servers/channels/subchannels to return
138+
*/
139+
const DEFAULT_MAX_RESULTS = 100;
140+
136141
export class ChannelzTraceStub {
137142
readonly events: TraceEvent[] = [];
138143
readonly creationTimestamp: Date = new Date();
@@ -198,19 +203,15 @@ export class ChannelzTrace {
198203
}
199204
}
200205

206+
type RefOrderedMap = OrderedMap<
207+
number,
208+
{ ref: { id: number; kind: EntityTypes; name: string }; count: number }
209+
>;
210+
201211
export class ChannelzChildrenTracker {
202-
private channelChildren = new OrderedMap<
203-
number,
204-
{ ref: ChannelRef; count: number }
205-
>();
206-
private subchannelChildren = new OrderedMap<
207-
number,
208-
{ ref: SubchannelRef; count: number }
209-
>();
210-
private socketChildren = new OrderedMap<
211-
number,
212-
{ ref: SocketRef; count: number }
213-
>();
212+
private channelChildren: RefOrderedMap = new OrderedMap();
213+
private subchannelChildren: RefOrderedMap = new OrderedMap();
214+
private socketChildren: RefOrderedMap = new OrderedMap();
214215
private trackerMap = {
215216
[EntityTypes.channel]: this.channelChildren,
216217
[EntityTypes.subchannel]: this.subchannelChildren,
@@ -219,16 +220,19 @@ export class ChannelzChildrenTracker {
219220

220221
refChild(child: ChannelRef | SubchannelRef | SocketRef) {
221222
const tracker = this.trackerMap[child.kind];
222-
const trackedChild = tracker.getElementByKey(child.id);
223-
224-
if (trackedChild === undefined) {
225-
tracker.setElement(child.id, {
226-
// @ts-expect-error union issues
227-
ref: child,
228-
count: 1,
229-
});
223+
const trackedChild = tracker.find(child.id);
224+
225+
if (trackedChild.equals(tracker.end())) {
226+
tracker.setElement(
227+
child.id,
228+
{
229+
ref: child,
230+
count: 1,
231+
},
232+
trackedChild
233+
);
230234
} else {
231-
trackedChild.count += 1;
235+
trackedChild.pointer[1].count += 1;
232236
}
233237
}
234238

@@ -245,9 +249,9 @@ export class ChannelzChildrenTracker {
245249

246250
getChildLists(): ChannelzChildren {
247251
return {
248-
channels: this.channelChildren,
249-
subchannels: this.subchannelChildren,
250-
sockets: this.socketChildren,
252+
channels: this.channelChildren as ChannelzChildren['channels'],
253+
subchannels: this.subchannelChildren as ChannelzChildren['subchannels'],
254+
sockets: this.socketChildren as ChannelzChildren['sockets'],
251255
};
252256
}
253257
}
@@ -585,7 +589,8 @@ function GetTopChannels(
585589
call: ServerUnaryCall<GetTopChannelsRequest__Output, GetTopChannelsResponse>,
586590
callback: sendUnaryData<GetTopChannelsResponse>
587591
): void {
588-
const maxResults = parseInt(call.request.max_results, 10) || 100;
592+
const maxResults =
593+
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
589594
const resultList: ChannelMessage[] = [];
590595
const startId = parseInt(call.request.start_channel_id, 10);
591596
const channelEntries = entityMaps[EntityTypes.channel];
@@ -649,7 +654,8 @@ function GetServers(
649654
call: ServerUnaryCall<GetServersRequest__Output, GetServersResponse>,
650655
callback: sendUnaryData<GetServersResponse>
651656
): void {
652-
const maxResults = parseInt(call.request.max_results, 10) || 100;
657+
const maxResults =
658+
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
653659
const startId = parseInt(call.request.start_server_id, 10);
654660
const serverEntries = entityMaps[EntityTypes.server];
655661
const resultList: ServerMessage[] = [];
@@ -820,7 +826,8 @@ function GetServerSockets(
820826
}
821827

822828
const startId = parseInt(call.request.start_socket_id, 10);
823-
const maxResults = parseInt(call.request.max_results, 10) || 100;
829+
const maxResults =
830+
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
824831
const resolvedInfo = serverEntry.getInfo();
825832
// If we wanted to include listener sockets in the result, this line would
826833
// instead say

packages/grpc-js/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export class Server {
368368
};
369369
}
370370

371-
private getChannelzSessionInfoGetter(
371+
private getChannelzSessionInfo(
372372
session: http2.ServerHttp2Session
373373
): SocketInfo {
374374
const sessionInfo = this.sessions.get(session)!;
@@ -1494,7 +1494,7 @@ export class Server {
14941494
return (session: http2.ServerHttp2Session) => {
14951495
const channelzRef = registerChannelzSocket(
14961496
session.socket?.remoteAddress ?? 'unknown',
1497-
this.getChannelzSessionInfoGetter.bind(this, session),
1497+
this.getChannelzSessionInfo.bind(this, session),
14981498
this.channelzEnabled
14991499
);
15001500

0 commit comments

Comments
 (0)