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

http2: window size connection control #26962

Closed
wants to merge 2 commits into from
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
28 changes: 28 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,33 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a getter and setter be better for an API?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point, I am not sure to be able to detect it. There is not acknoledge on a SETTINGS_INITIAL_WINDOW_SIZE
But, we can add a test for testing the limit of the buffer allocation for instance and insure that code results are covered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the getter

<!-- YAML
added: v12.0.0
jasnell marked this conversation as resolved.
Show resolved Hide resolved
-->

* `windowSize` {number}
* Returns: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return anything?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact it's not exaclty the case, at this stage it returns only zero or throws ERR_OUT_OF_RANGE, the function is mapped to nghttp2_session_set_local_window_size.
If the library changes its internal, the value will be returned.
If it does not fit the philosophy of node, we can return a generic errror that maps all errors at the native code level.

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).
The window_size is an absolute value of window size to set, rather
than the delta.

```js
clientSession.on('connect', (session) => sendSettings(session, (s) => cb(s)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps (s) => cb(s) would be more clear, as documentation, if it were written with meaningful variable names (settings?). No need to squeeze it on 80ch; just go multi-line.


function sendSettings(session, cb) {
session.setConnectionWindowSize(1024*1024);

const settings = http2.getDefaultSettings();
settings.initialWindowSize = WINDOW_SIZE;
settings.maxFrameSize = FRAME_SIZE;
}

```

#### http2session.socket
<!-- YAML
added: v8.4.0
Expand Down Expand Up @@ -2222,6 +2249,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` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate line?


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
23 changes: 23 additions & 0 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,26 @@ 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 +3052,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 +3114,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