Skip to content

Commit 277a76f

Browse files
committed
net: add writeQueueSize
1 parent 90c758c commit 277a76f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

doc/api/net.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ added: v0.5.3
825825

826826
The amount of bytes sent.
827827

828+
### `socket.writeQueueSize`
829+
830+
<!-- YAML
831+
added: REPLACEME
832+
-->
833+
834+
* {integer}
835+
836+
The amount of bytes in libuv write queue.
837+
828838
### `socket.connect()`
829839

830840
Initiate a connection on a given socket.

lib/net.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ protoGetter('bytesWritten', function bytesWritten() {
967967
return bytes;
968968
});
969969

970+
protoGetter('writeQueueSize', function writeQueueSize() {
971+
return this._handle ? this._handle.writeQueueSize : -1;
972+
});
970973

971974
function checkBindError(err, port, handle) {
972975
// EADDRINUSE may not be reported until we call listen() or connect().
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const tmpdir = require('../common/tmpdir');
6+
7+
tmpdir.refresh();
8+
9+
const socket = new net.Socket();
10+
assert.strictEqual(socket.writeQueueSize, -1);
11+
12+
const server = net.createServer()
13+
.listen(common.PIPE, common.mustCall(() => {
14+
// The pipe connection is a synchronous operation
15+
// `net.connect` will set `socket.connecting` to true
16+
const socket = net.connect(common.PIPE, common.mustCall(() => {
17+
socket.destroy();
18+
server.close();
19+
}));
20+
// Set connecting to false here to make `socket.write` can call into
21+
// libuv directly (see `_writeGeneric` in `net.js`).
22+
// because the socket is connecting currently in libuv, so libuv will
23+
// insert the write request into write queue, then we can get the
24+
// size of write queue by `socket.writeQueueSize`.
25+
socket.connecting = false;
26+
const data = 'hello';
27+
socket.write(data, 'utf-8', common.mustCall());
28+
assert.strictEqual(socket.writeQueueSize, common.isWindows ? 0 : data.length);
29+
// Restore it
30+
socket.connecting = true;
31+
}));

0 commit comments

Comments
 (0)