Skip to content

Commit

Permalink
dgram: add dgram send queue info
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Aug 6, 2022
1 parent 90c758c commit 76f70b4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/api/dgram.md
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()`

<!-- 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
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
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
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
35 changes: 35 additions & 0 deletions test/parallel/test-dgram-send-queue-info.js
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const cp = require('child_process');

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

const CODE = `
const assert = require('assert');
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
const client = dgram.createSocket('udp4');
server.bind(0, () => {
client.connect(server.address().port, () => {
const data = "hello";
client.send(data);
client.send(data);
assert.strictEqual(client.getSendQueueSize(), data.length * 2);
assert.strictEqual(client.getSendQueueCount(), 2);
client.close();
server.close();
});
});
`;

const proc = cp.spawn(process.execPath,
[ '--test-udp-no-try-send',
'-e', CODE ]);

proc.once('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));

0 comments on commit 76f70b4

Please sign in to comment.