Skip to content

Commit

Permalink
scale the socket receive buffer on the fly #39062
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Jun 26, 2021
1 parent fdb097c commit 4bbfec7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ LibuvStreamWrap::LibuvStreamWrap(Environment* env,
reinterpret_cast<uv_handle_t*>(stream),
provider),
StreamBase(env),
stream_(stream) {
stream_(stream),
recvQSize(STREAM_WRAP_RECVQ_INITIAL) {
StreamBase::AttachToObject(object);
}

Expand Down Expand Up @@ -209,7 +210,7 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) {
HandleScope scope(env()->isolate());
Context::Scope context_scope(env()->context());

*buf = EmitAlloc(suggested_size);
*buf = EmitAlloc(recvQSize);
}

template <class WrapType>
Expand Down Expand Up @@ -252,6 +253,13 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
CHECK_EQ(persistent().IsEmpty(), false);

if (nread > 0) {
// Adjust the dynamic receive buffer
if (nread == recvQSize && recvQSize < STREAM_WRAP_RECVQ_MAX) {
recvQSize <<= 1;
} else if (nread < recvQSize / 4 && recvQSize > STREAM_WRAP_RECVQ_MIN) {
recvQSize >>= 1;
}

MaybeLocal<Object> pending_obj;

if (type == UV_TCP) {
Expand Down
5 changes: 5 additions & 0 deletions src/stream_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "handle_wrap.h"
#include "v8.h"

#define STREAM_WRAP_RECVQ_INITIAL 16*1024
#define STREAM_WRAP_RECVQ_MIN 16*1024
#define STREAM_WRAP_RECVQ_MAX 256*1024

namespace node {

class Environment;
Expand Down Expand Up @@ -111,6 +115,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase {
static void AfterUvShutdown(uv_shutdown_t* req, int status);

uv_stream_t* const stream_;
ssize_t recvQSize;

#ifdef _WIN32
// We don't always have an FD that we could look up on the stream_
Expand Down

0 comments on commit 4bbfec7

Please sign in to comment.