Skip to content

Commit 33707dc

Browse files
theanarkhrichardlau
authored andcommitted
dgram: add dgram send queue info
PR-URL: #44149 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent c708d9b commit 33707dc

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

doc/api/dgram.md

+17
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,23 @@ added: v8.7.0
454454

455455
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
456456

457+
### `socket.getSendQueueSize()`
458+
459+
<!-- YAML
460+
added: REPLACEME
461+
-->
462+
463+
* Returns: {number} Number of bytes queued for sending.
464+
465+
### `socket.getSendQueueCount()`
466+
467+
<!-- YAML
468+
added: REPLACEME
469+
-->
470+
471+
* Returns: {number} Number of send requests currently in the queue awaiting
472+
to be processed.
473+
457474
### `socket.ref()`
458475

459476
<!-- YAML

lib/dgram.js

+7
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

+20
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ void UDPWrap::Initialize(Local<Object> target,
182182
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
183183
SetProtoMethod(isolate, t, "setTTL", SetTTL);
184184
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
185+
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
186+
SetProtoMethodNoSideEffect(
187+
isolate, t, "getSendQueueCount", GetSendQueueCount);
185188

186189
t->Inherit(HandleWrap::GetConstructorTemplate(env));
187190

@@ -783,6 +786,23 @@ MaybeLocal<Object> UDPWrap::Instantiate(Environment* env,
783786
return env->udp_constructor_function()->NewInstance(env->context());
784787
}
785788

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

787807
} // namespace node
788808

src/udp_wrap.h

+3
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Flags: --test-udp-no-try-send
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const dgram = require('dgram');
6+
7+
const socket = dgram.createSocket('udp4');
8+
assert.strictEqual(socket.getSendQueueSize(), 0);
9+
assert.strictEqual(socket.getSendQueueCount(), 0);
10+
socket.close();
11+
12+
const server = dgram.createSocket('udp4');
13+
const client = dgram.createSocket('udp4');
14+
15+
server.bind(0, common.mustCall(() => {
16+
client.connect(server.address().port, common.mustCall(() => {
17+
const data = 'hello';
18+
client.send(data);
19+
client.send(data);
20+
// See uv__send in win/udp.c
21+
assert.strictEqual(client.getSendQueueSize(),
22+
common.isWindows ? 0 : data.length * 2);
23+
assert.strictEqual(client.getSendQueueCount(), 2);
24+
client.close();
25+
server.close();
26+
}));
27+
}));

0 commit comments

Comments
 (0)