Skip to content

Commit 1021a36

Browse files
committed
Improve write (chunk if needed)
1 parent f682ca1 commit 1021a36

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/connection.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ function postgresSocket(options, {
181181
}) {
182182
let socket
183183
let closed = true
184+
let next = null
185+
let buffer
184186

185187
function onclose() {
186188
socket.removeListener('data', data)
@@ -227,7 +229,12 @@ function postgresSocket(options, {
227229

228230
const x = {
229231
ready: false,
230-
write: x => socket.write(x),
232+
write: x => {
233+
buffer = buffer ? Buffer.concat([buffer, x]) : Buffer.from(x)
234+
if (buffer.length >= 1024)
235+
return write()
236+
next === null && (next = setImmediate(write))
237+
},
231238
destroy: () => {
232239
socket && socket.destroy()
233240
return Promise.resolve()
@@ -238,6 +245,12 @@ function postgresSocket(options, {
238245
connect
239246
}
240247

248+
function write() {
249+
socket.write(buffer)
250+
next !== null && clearImmediate(next)
251+
buffer = next = null
252+
}
253+
241254
/* c8 ignore next */
242255
return x
243256
}

0 commit comments

Comments
 (0)