Skip to content

Commit

Permalink
dgram: add dgram send queue info
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#44149
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
theanarkh authored and guangwong committed Jan 3, 2023
1 parent ce35b11 commit 2149604
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,23 @@ added: v8.7.0

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

### `socket.getSendQueueSize()`

<!-- YAML
added: REPLACEME
-->

* Returns: {number} Number of bytes queued for sending.

### `socket.getSendQueueCount()`

<!-- YAML
added: REPLACEME
-->

* Returns: {number} Number of send requests currently in the queue awaiting
to be processed.

### `socket.ref()`

<!-- YAML
Expand Down
7 changes: 7 additions & 0 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,13 @@ Socket.prototype.getSendBufferSize = function() {
return bufferSize(this, 0, SEND_BUFFER);
};

Socket.prototype.getSendQueueSize = function() {
return this[kStateSymbol].handle.getSendQueueSize();
};

Socket.prototype.getSendQueueCount = function() {
return this[kStateSymbol].handle.getSendQueueCount();
};

// Deprecated private APIs.
ObjectDefineProperty(Socket.prototype, '_handle', {
Expand Down
20 changes: 20 additions & 0 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ void UDPWrap::Initialize(Local<Object> target,
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
SetProtoMethod(isolate, t, "setTTL", SetTTL);
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
SetProtoMethodNoSideEffect(
isolate, t, "getSendQueueCount", GetSendQueueCount);

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

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

void UDPWrap::GetSendQueueSize(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF));

size_t size = uv_udp_get_send_queue_size(&wrap->handle_);
args.GetReturnValue().Set(static_cast<double>(size));
}

void UDPWrap::GetSendQueueCount(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF));

size_t count = uv_udp_get_send_queue_count(&wrap->handle_);
args.GetReturnValue().Set(static_cast<double>(count));
}

} // namespace node

Expand Down
3 changes: 3 additions & 0 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class UDPWrap final : public HandleWrap,
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueCount(
const v8::FunctionCallbackInfo<v8::Value>& args);

// UDPListener implementation
uv_buf_t OnAlloc(size_t suggested_size) override;
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-dgram-send-queue-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Flags: --test-udp-no-try-send
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

const socket = dgram.createSocket('udp4');
assert.strictEqual(socket.getSendQueueSize(), 0);
assert.strictEqual(socket.getSendQueueCount(), 0);
socket.close();

const server = dgram.createSocket('udp4');
const client = dgram.createSocket('udp4');

server.bind(0, common.mustCall(() => {
client.connect(server.address().port, common.mustCall(() => {
const data = 'hello';
client.send(data);
client.send(data);
// See uv__send in win/udp.c
assert.strictEqual(client.getSendQueueSize(),
common.isWindows ? 0 : data.length * 2);
assert.strictEqual(client.getSendQueueCount(), 2);
client.close();
server.close();
}));
}));

0 comments on commit 2149604

Please sign in to comment.