Skip to content

Commit 983c585

Browse files
committed
dgram: add dgram send queue info
1 parent 90c758c commit 983c585

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

doc/api/dgram.md

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

+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,25 @@
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+
assert.strictEqual(client.getSendQueueSize(), data.length * 2);
21+
assert.strictEqual(client.getSendQueueCount(), 2);
22+
client.close();
23+
server.close();
24+
}));
25+
}));

0 commit comments

Comments
 (0)