chore: use iterators for tracking map, const for default values

This commit is contained in:
AVVS 2024-03-04 18:25:23 -08:00
parent 62e8ea97e6
commit cf321a80b1
No known key found for this signature in database
GPG Key ID: 2B890ABACCC3E369
2 changed files with 35 additions and 28 deletions

View File

@ -133,6 +133,11 @@ interface TraceEvent {
*/ */
const TARGET_RETAINED_TRACES = 32; const TARGET_RETAINED_TRACES = 32;
/**
* Default number of sockets/servers/channels/subchannels to return
*/
const DEFAULT_MAX_RESULTS = 100;
export class ChannelzTraceStub { export class ChannelzTraceStub {
readonly events: TraceEvent[] = []; readonly events: TraceEvent[] = [];
readonly creationTimestamp: Date = new Date(); readonly creationTimestamp: Date = new Date();
@ -198,19 +203,15 @@ export class ChannelzTrace {
} }
} }
type RefOrderedMap = OrderedMap<
number,
{ ref: { id: number; kind: EntityTypes; name: string }; count: number }
>;
export class ChannelzChildrenTracker { export class ChannelzChildrenTracker {
private channelChildren = new OrderedMap< private channelChildren: RefOrderedMap = new OrderedMap();
number, private subchannelChildren: RefOrderedMap = new OrderedMap();
{ ref: ChannelRef; count: number } private socketChildren: RefOrderedMap = new OrderedMap();
>();
private subchannelChildren = new OrderedMap<
number,
{ ref: SubchannelRef; count: number }
>();
private socketChildren = new OrderedMap<
number,
{ ref: SocketRef; count: number }
>();
private trackerMap = { private trackerMap = {
[EntityTypes.channel]: this.channelChildren, [EntityTypes.channel]: this.channelChildren,
[EntityTypes.subchannel]: this.subchannelChildren, [EntityTypes.subchannel]: this.subchannelChildren,
@ -219,16 +220,19 @@ export class ChannelzChildrenTracker {
refChild(child: ChannelRef | SubchannelRef | SocketRef) { refChild(child: ChannelRef | SubchannelRef | SocketRef) {
const tracker = this.trackerMap[child.kind]; const tracker = this.trackerMap[child.kind];
const trackedChild = tracker.getElementByKey(child.id); const trackedChild = tracker.find(child.id);
if (trackedChild === undefined) { if (trackedChild.equals(tracker.end())) {
tracker.setElement(child.id, { tracker.setElement(
// @ts-expect-error union issues child.id,
ref: child, {
count: 1, ref: child,
}); count: 1,
},
trackedChild
);
} else { } else {
trackedChild.count += 1; trackedChild.pointer[1].count += 1;
} }
} }
@ -245,9 +249,9 @@ export class ChannelzChildrenTracker {
getChildLists(): ChannelzChildren { getChildLists(): ChannelzChildren {
return { return {
channels: this.channelChildren, channels: this.channelChildren as ChannelzChildren['channels'],
subchannels: this.subchannelChildren, subchannels: this.subchannelChildren as ChannelzChildren['subchannels'],
sockets: this.socketChildren, sockets: this.socketChildren as ChannelzChildren['sockets'],
}; };
} }
} }
@ -585,7 +589,8 @@ function GetTopChannels(
call: ServerUnaryCall<GetTopChannelsRequest__Output, GetTopChannelsResponse>, call: ServerUnaryCall<GetTopChannelsRequest__Output, GetTopChannelsResponse>,
callback: sendUnaryData<GetTopChannelsResponse> callback: sendUnaryData<GetTopChannelsResponse>
): void { ): void {
const maxResults = parseInt(call.request.max_results, 10) || 100; const maxResults =
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
const resultList: ChannelMessage[] = []; const resultList: ChannelMessage[] = [];
const startId = parseInt(call.request.start_channel_id, 10); const startId = parseInt(call.request.start_channel_id, 10);
const channelEntries = entityMaps[EntityTypes.channel]; const channelEntries = entityMaps[EntityTypes.channel];
@ -649,7 +654,8 @@ function GetServers(
call: ServerUnaryCall<GetServersRequest__Output, GetServersResponse>, call: ServerUnaryCall<GetServersRequest__Output, GetServersResponse>,
callback: sendUnaryData<GetServersResponse> callback: sendUnaryData<GetServersResponse>
): void { ): void {
const maxResults = parseInt(call.request.max_results, 10) || 100; const maxResults =
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
const startId = parseInt(call.request.start_server_id, 10); const startId = parseInt(call.request.start_server_id, 10);
const serverEntries = entityMaps[EntityTypes.server]; const serverEntries = entityMaps[EntityTypes.server];
const resultList: ServerMessage[] = []; const resultList: ServerMessage[] = [];
@ -820,7 +826,8 @@ function GetServerSockets(
} }
const startId = parseInt(call.request.start_socket_id, 10); const startId = parseInt(call.request.start_socket_id, 10);
const maxResults = parseInt(call.request.max_results, 10) || 100; const maxResults =
parseInt(call.request.max_results, 10) || DEFAULT_MAX_RESULTS;
const resolvedInfo = serverEntry.getInfo(); const resolvedInfo = serverEntry.getInfo();
// If we wanted to include listener sockets in the result, this line would // If we wanted to include listener sockets in the result, this line would
// instead say // instead say

View File

@ -368,7 +368,7 @@ export class Server {
}; };
} }
private getChannelzSessionInfoGetter( private getChannelzSessionInfo(
session: http2.ServerHttp2Session session: http2.ServerHttp2Session
): SocketInfo { ): SocketInfo {
const sessionInfo = this.sessions.get(session)!; const sessionInfo = this.sessions.get(session)!;
@ -1494,7 +1494,7 @@ export class Server {
return (session: http2.ServerHttp2Session) => { return (session: http2.ServerHttp2Session) => {
const channelzRef = registerChannelzSocket( const channelzRef = registerChannelzSocket(
session.socket?.remoteAddress ?? 'unknown', session.socket?.remoteAddress ?? 'unknown',
this.getChannelzSessionInfoGetter.bind(this, session), this.getChannelzSessionInfo.bind(this, session),
this.channelzEnabled this.channelzEnabled
); );