Skip to content

Commit 33707dc

Browse files
theanarkhrichardlau
authored andcommitted
dgram: add dgram send queue info
PR-URL: #44149 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
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

Lines changed: 17 additions & 0 deletions
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

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: 20 additions & 0 deletions
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

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: 27 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy