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 50e3318
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
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
22 changes: 21 additions & 1 deletion src/udp_wrap.cc
Expand Up @@ -181,7 +181,8 @@ void UDPWrap::Initialize(Local<Object> target,
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
SetProtoMethod(isolate, t, "setTTL", SetTTL);
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
SetProtoMethod(isolate, t, "getSendQueueSize", GetSendQueueSize);
SetProtoMethod(isolate, t, "getSendQueueCount", GetSendQueueCount);

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

Expand Down Expand Up @@ -783,6 +784,25 @@ 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
4 changes: 4 additions & 0 deletions src/udp_wrap.h
Expand Up @@ -150,6 +150,10 @@ 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
9 changes: 9 additions & 0 deletions test/parallel/test-dgram-send-queue-info.js
@@ -0,0 +1,9 @@
'use strict';
require('../common');
const assert = require('assert');
const dgram = require('dgram');

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

0 comments on commit 50e3318

Please sign in to comment.