Skip to content

Commit

Permalink
Enable TCP_NODELAY
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
rgrove committed Aug 23, 2023
1 parent c8b8621 commit 0832889
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/memcache-client/src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ export class MemcacheClient extends EventEmitter {
): Promise<Response> {
return this.send(
(socket) => {
socket?.write(data);
if (options && options.noreply) {
socket?.write(" noreply\r\n");
} else {
socket?.write("\r\n");
let line = data;
if (options?.noreply) {
line += " noreply";
}
socket?.write(`${line}\r\n`);
},
options,
callback
Expand Down Expand Up @@ -318,10 +317,11 @@ export class MemcacheClient extends EventEmitter {
(options as Partial<CasCommandOptions>)?.compress === true
);
const bytes = Buffer.byteLength(packed.data);
const msg = `${cmd} ${key} ${packed.flag} ${lifetime} ${bytes}${casUniq}${noreply}\r\n`;
socket?.write(msg);
socket?.write(packed.data);
socket?.write("\r\n");
socket?.write(Buffer.concat([
Buffer.from(`${cmd} ${key} ${packed.flag} ${lifetime} ${bytes}${casUniq}${noreply}\r\n`),
Buffer.isBuffer(packed.data) ? packed.data : Buffer.from(packed.data),
Buffer.from("\r\n"),
]));
};

return this._callbackSend(_data, options, callback) as unknown as Promise<string[]>;
Expand Down
2 changes: 2 additions & 0 deletions packages/memcache-client/src/lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ export class MemcacheConnection extends MemcacheParser {
}

_setupConnection(socket: Socket): void {
socket.setNoDelay(true);

socket.on("data", this.onData.bind(this));

socket.on("end", () => {
Expand Down

0 comments on commit 0832889

Please sign in to comment.