Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: add dgram send queue info #44149

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,23 @@ added: v8.7.0

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

### `socket.getSendQueueSize()`
theanarkh marked this conversation as resolved.
Show resolved Hide resolved

<!-- 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();
}));
}));