Skip to content

Commit 40e4da2

Browse files
committed
dgram: add dgram send queue info
1 parent 90c758c commit 40e4da2

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

doc/api/dgram.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ added: v8.7.0
461461

462462
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
463463

464+
### `socket.getSendQueueSize()`
465+
466+
<!-- YAML
467+
added: REPLACEME
468+
-->
469+
470+
* Returns: {number} Number of bytes queued for sending.
471+
472+
### `socket.getSendQueueCount()`
473+
474+
<!-- YAML
475+
added: REPLACEME
476+
-->
477+
478+
* Returns: {number} Number of send requests currently in the queue awaiting
479+
to be processed.
480+
464481
### `socket.ref()`
465482

466483
<!-- YAML

lib/dgram.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,13 @@ Socket.prototype.getSendBufferSize = function() {
976976
return bufferSize(this, 0, SEND_BUFFER);
977977
};
978978

979+
Socket.prototype.getSendQueueSize = function() {
980+
return this[kStateSymbol].handle.getSendQueueSize();
981+
};
982+
983+
Socket.prototype.getSendQueueCount = function() {
984+
return this[kStateSymbol].handle.getSendQueueCount();
985+
};
979986

980987
// Deprecated private APIs.
981988
ObjectDefineProperty(Socket.prototype, '_handle', {

src/udp_wrap.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ void UDPWrap::Initialize(Local<Object> target,
181181
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
182182
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
183183
SetProtoMethod(isolate, t, "setTTL", SetTTL);
184-
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
184+
SetProtoMethod(isolate, t, "getSendQueueSize", GetSendQueueSize);
185+
SetProtoMethod(isolate, t, "getSendQueueCount", GetSendQueueCount);
185186

186187
t->Inherit(HandleWrap::GetConstructorTemplate(env));
187188

@@ -783,6 +784,23 @@ MaybeLocal<Object> UDPWrap::Instantiate(Environment* env,
783784
return env->udp_constructor_function()->NewInstance(env->context());
784785
}
785786

787+
void UDPWrap::GetSendQueueSize(const FunctionCallbackInfo<Value>& args) {
788+
UDPWrap* wrap;
789+
ASSIGN_OR_RETURN_UNWRAP(
790+
&wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF));
791+
792+
size_t size = uv_udp_get_send_queue_size(&wrap->handle_);
793+
args.GetReturnValue().Set(static_cast<double>(size));
794+
}
795+
796+
void UDPWrap::GetSendQueueCount(const FunctionCallbackInfo<Value>& args) {
797+
UDPWrap* wrap;
798+
ASSIGN_OR_RETURN_UNWRAP(
799+
&wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF));
800+
801+
size_t count = uv_udp_get_send_queue_count(&wrap->handle_);
802+
args.GetReturnValue().Set(static_cast<double>(count));
803+
}
786804

787805
} // namespace node
788806

src/udp_wrap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class UDPWrap final : public HandleWrap,
150150
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
151151
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
152152
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
153+
static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
154+
static void GetSendQueueCount(
155+
const v8::FunctionCallbackInfo<v8::Value>& args);
153156

154157
// UDPListener implementation
155158
uv_buf_t OnAlloc(size_t suggested_size) override;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
const cp = require('child_process');
6+
7+
const socket = dgram.createSocket('udp4');
8+
assert.strictEqual(socket.getSendQueueSize(), 0);
9+
assert.strictEqual(socket.getSendQueueCount(), 0);
10+
11+
const CODE = `
12+
const assert = require('assert');
13+
const dgram = require('dgram');
14+
const server = dgram.createSocket('udp4');
15+
const client = dgram.createSocket('udp4');
16+
server.bind(0, () => {
17+
client.connect(server.address().port, () => {
18+
const data = "hello";
19+
client.send(data);
20+
client.send(data);
21+
assert.strictEqual(client.getSendQueueSize(), data.length * 2);
22+
assert.strictEqual(client.getSendQueueCount(), 2);
23+
client.close();
24+
server.close();
25+
});
26+
});
27+
`;
28+
29+
const proc = cp.spawn(process.execPath,
30+
[ '--test-udp-no-try-send',
31+
'-e', CODE ]);
32+
33+
proc.once('exit', common.mustCall((code) => {
34+
assert(code === 0);
35+
}));

0 commit comments

Comments
 (0)