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

optimization change to send outbound messages quicker #566

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Changes from all 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
19 changes: 5 additions & 14 deletions nats-base-client/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
servers: Servers;
server!: ServerImpl;
features: Features;
flusher?: unknown;
connectPromise: Promise<void> | null;

constructor(options: ConnectionOptions, publisher: Publisher) {
Expand All @@ -421,7 +420,6 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
//@ts-ignore: options.pendingLimit is hidden
this.pendingLimit = options.pendingLimit || this.pendingLimit;
this.features = new Features({ major: 0, minor: 0, micro: 0 });
this.flusher = null;
this.connectPromise = null;

const servers = typeof options.servers === "string"
Expand Down Expand Up @@ -606,7 +604,7 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
throw lastErr;
}

async dialLoop(): Promise<void> {
dialLoop(): Promise<void> {
if (this.connectPromise === null) {
this.connectPromise = this.dodialLoop();
this.connectPromise
Expand Down Expand Up @@ -868,19 +866,11 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
this.outbound.fill(buf, ...payloads);

if (len === 0) {
//@ts-ignore: node types timer
this.flusher = setTimeout(() => {
queueMicrotask(() => {
this.flushPending();
});
} else if (this.outbound.size() >= this.pendingLimit) {
// if we have a flusher, clear it - otherwise in a bench
// type scenario where the main loop is dominated by a publisher
// we create many timers.
if (this.flusher) {
//@ts-ignore: node types timer
clearTimeout(this.flusher);
this.flusher = null;
}
// flush inline
this.flushPending();
}
}
Expand Down Expand Up @@ -1002,7 +992,8 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
p = deferred<void>();
}
this.pongs.push(p);
this.sendCommand(PING_CMD);
this.outbound.fill(PING_CMD);
this.flushPending();
return p;
}

Expand Down