Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow to control the connection window size by setting local window s…
…ize (local endpoints's window size) to the given window_size

To increase window size, this function may submit WINDOW_UPDATE frame to transmission queue.
Pay attention, this function takes the absolute value of window size to set, rather than the delta, the delta is computed by the update operation.
  • Loading branch information
Yann Stephan authored and Yann Stephan committed Mar 28, 2019
commit 5f1192d2e28277bf5af64833081fa46dcb0a6e3a
13 changes: 13 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ Used to set a callback function that is called when there is no activity on
the `Http2Session` after `msecs` milliseconds. The given `callback` is
registered as a listener on the `'timeout'` event.

#### http2session.setConnectionWindowSize(windowSize)
<!-- YAML
added: v12.0.0
-->

* `windowSize` {number}
* Returns: 0
In case of allocation error, a new `ERR_OUT_OF_RANGE`
error will be thrown.

Used to set the local window size (local endpoints's window size).

#### http2session.socket
<!-- YAML
added: v8.4.0
Expand Down Expand Up @@ -2222,6 +2234,7 @@ added: v8.4.0
| `0x0b` | Enhance Your Calm | `http2.constants.NGHTTP2_ENHANCE_YOUR_CALM` |
| `0x0c` | Inadequate Security | `http2.constants.NGHTTP2_INADEQUATE_SECURITY` |
| `0x0d` | HTTP/1.1 Required | `http2.constants.NGHTTP2_HTTP_1_1_REQUIRED` |
| `0x0d` | HTTP/1.1 Required | `http2.constants.NGHTTP2_HTTP_1_1_REQUIRED` |

The `'timeout'` event is emitted when there is no activity on the Server for
a given number of milliseconds set using `http2server.setTimeout()`.
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const {
NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE,
NGHTTP2_ERR_INVALID_ARGUMENT,
NGHTTP2_ERR_STREAM_CLOSED,
NGHTTP2_ERR_NOMEM,

HTTP2_HEADER_AUTHORITY,
HTTP2_HEADER_DATE,
Expand Down Expand Up @@ -1036,6 +1037,23 @@ class Http2Session extends EventEmitter {
this[kHandle].setNextStreamID(id);
}

// Sets the local window size (local endpoints's window size)
// Returns 0 if sucess or throw an exception if NGHTTP2_ERR_NOMEM
// if the window allocation fails
setConnectionWindowSize(windowSize) {
if (this.destroyed)
throw new ERR_HTTP2_INVALID_SESSION();

validateNumber(windowSize, 'windowSize');
const ret = this[kHandle].setConnectionWindowSize(windowSize);

if (typeof ret === 'number' && ret === NGHTTP2_ERR_NOMEM) {
throw new ERR_OUT_OF_RANGE('windowSize', `> 0 and <= 2^31-1 bytes`, windowSize)
}

return ret;
}

// If ping is called while we are still connecting, or after close() has
// been called, the ping callback will be invoked immediately will a ping
// cancelled error and a duration of 0.0.
Expand Down
22 changes: 22 additions & 0 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,25 @@ void Http2Session::SetNextStreamID(const FunctionCallbackInfo<Value>& args) {
Debug(session, "set next stream id to %d", id);
}

// Set local window size (local endpoints's window size) to the given window_size for the stream denoted by 0.
// If successful, returns 0.
void Http2Session::SetConnectionWindowSize(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Http2Session* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());

int32_t window_size = args[0]->Int32Value(env->context()).ToChecked();

int result = nghttp2_session_set_local_window_size(
session->session(), NGHTTP2_FLAG_NONE, 0, window_size);

args.GetReturnValue().Set(result);

Debug(session, "set connection window size to %d", window_size);
}


// A TypedArray instance is shared between C++ and JS land to contain the
// SETTINGS (either remote or local). RefreshSettings updates the current
// values established for each of the settings so those can be read in JS land.
Expand Down Expand Up @@ -3032,6 +3051,8 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(session, "request", Http2Session::Request);
env->SetProtoMethod(session, "setNextStreamID",
Http2Session::SetNextStreamID);
env->SetProtoMethod(session, "setConnectionWindowSize",
Http2Session::SetConnectionWindowSize);
env->SetProtoMethod(session, "updateChunksSent",
Http2Session::UpdateChunksSent);
env->SetProtoMethod(session, "refreshState", Http2Session::RefreshState);
Expand Down Expand Up @@ -3092,6 +3113,7 @@ void Initialize(Local<Object> target,
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_INVALID_ARGUMENT);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_STREAM_CLOSED);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_NOMEM);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_ERR_FRAME_SIZE_ERROR);

NODE_DEFINE_HIDDEN_CONSTANT(constants, STREAM_OPTION_EMPTY_PAYLOAD);
Expand Down
1 change: 1 addition & 0 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ class Http2Session : public AsyncWrap, public StreamListener {
static void Settings(const FunctionCallbackInfo<Value>& args);
static void Request(const FunctionCallbackInfo<Value>& args);
static void SetNextStreamID(const FunctionCallbackInfo<Value>& args);
static void SetConnectionWindowSize(const FunctionCallbackInfo<Value>& args);
static void Goaway(const FunctionCallbackInfo<Value>& args);
static void UpdateChunksSent(const FunctionCallbackInfo<Value>& args);
static void RefreshState(const FunctionCallbackInfo<Value>& args);
Expand Down