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: handful of http2 src cleanups #14825

Closed
wants to merge 4 commits into from
Closed
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
Prev Previous commit
[Squash] Don't use a macro
  • Loading branch information
jasnell committed Aug 16, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit cdaef97bfff7c50af322e2bd5e62996420bef81d
55 changes: 25 additions & 30 deletions src/node_http2_core-inl.h
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ inline int Nghttp2Session::OnNghttpError(nghttp2_session* session,
void* user_data) {
Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: Error '%.*s'\n",
SESSION_TYPE_NAME(handle), len, message);
handle->TypeName(), len, message);
return 0;
}
#endif
@@ -56,7 +56,7 @@ inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session,
frame->push_promise.promised_stream_id :
frame->hd.stream_id;
DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);

Nghttp2Stream* stream = handle->FindStream(id);
if (stream == nullptr) {
@@ -99,7 +99,7 @@ inline int Nghttp2Session::OnFrameReceive(nghttp2_session* session,
void* user_data) {
Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: complete frame received: type: %d\n",
SESSION_TYPE_NAME(handle), frame->hd.type);
handle->TypeName(), frame->hd.type);
bool ack;
switch (frame->hd.type) {
case NGHTTP2_DATA:
@@ -131,7 +131,7 @@ inline int Nghttp2Session::OnFrameNotSent(nghttp2_session *session,
void *user_data) {
Nghttp2Session *handle = static_cast<Nghttp2Session *>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: frame type %d was not sent, code: %d\n",
SESSION_TYPE_NAME(handle), frame->hd.type, error_code);
handle->TypeName(), frame->hd.type, error_code);
// Do not report if the frame was not sent due to the session closing
if (error_code != NGHTTP2_ERR_SESSION_CLOSING &&
error_code != NGHTTP2_ERR_STREAM_CLOSED &&
@@ -148,7 +148,7 @@ inline int Nghttp2Session::OnStreamClose(nghttp2_session *session,
void *user_data) {
Nghttp2Session *handle = static_cast<Nghttp2Session *>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: stream %d closed, code: %d\n",
SESSION_TYPE_NAME(handle), id, code);
handle->TypeName(), id, code);
Nghttp2Stream *stream = handle->FindStream(id);
// Intentionally ignore the callback if the stream does not exist
if (stream != nullptr)
@@ -168,7 +168,7 @@ inline ssize_t Nghttp2Session::OnStreamReadFD(nghttp2_session *session,
void *user_data) {
Nghttp2Session *handle = static_cast<Nghttp2Session *>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: reading outbound file data for stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);
Nghttp2Stream *stream = handle->FindStream(id);

int fd = source->fd;
@@ -204,7 +204,7 @@ inline ssize_t Nghttp2Session::OnStreamReadFD(nghttp2_session *session,
// if numchars < length, assume that we are done.
if (static_cast<size_t>(numchars) < length || length <= 0) {
DEBUG_HTTP2("Nghttp2Session %s: no more data for stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);
*flags |= NGHTTP2_DATA_FLAG_EOF;
GetTrailers(session, handle, stream, flags);
}
@@ -224,7 +224,7 @@ inline ssize_t Nghttp2Session::OnStreamRead(nghttp2_session *session,
void *user_data) {
Nghttp2Session *handle = static_cast<Nghttp2Session *>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: reading outbound data for stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);
Nghttp2Stream *stream = handle->FindStream(id);
size_t remaining = length;
size_t offset = 0;
@@ -234,7 +234,7 @@ inline ssize_t Nghttp2Session::OnStreamRead(nghttp2_session *session,
// calls this callback.
while (stream->queue_head_ != nullptr) {
DEBUG_HTTP2("Nghttp2Session %s: processing outbound data chunk\n",
SESSION_TYPE_NAME(handle));
handle->TypeName());
nghttp2_stream_write_queue *head = stream->queue_head_;
while (stream->queue_head_index_ < head->nbufs) {
if (remaining == 0)
@@ -275,12 +275,12 @@ inline ssize_t Nghttp2Session::OnStreamRead(nghttp2_session *session,
int writable = stream->queue_head_ != nullptr || stream->IsWritable();
if (offset == 0 && writable && stream->queue_head_ == nullptr) {
DEBUG_HTTP2("Nghttp2Session %s: deferring stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);
return NGHTTP2_ERR_DEFERRED;
}
if (!writable) {
DEBUG_HTTP2("Nghttp2Session %s: no more data for stream %d\n",
SESSION_TYPE_NAME(handle), id);
handle->TypeName(), id);
*flags |= NGHTTP2_DATA_FLAG_EOF;

GetTrailers(session, handle, stream, flags);
@@ -299,7 +299,7 @@ inline ssize_t Nghttp2Session::OnSelectPadding(nghttp2_session *session,
assert(handle->HasGetPaddingCallback());
ssize_t padding = handle->GetPadding(frame->hd.length, maxPayloadLen);
DEBUG_HTTP2("Nghttp2Session %s: using padding, size: %d\n",
SESSION_TYPE_NAME(handle), padding);
handle->TypeName(), padding);
return padding;
}

@@ -312,7 +312,7 @@ inline int Nghttp2Session::OnDataChunkReceived(nghttp2_session *session,
void *user_data) {
Nghttp2Session *handle = static_cast<Nghttp2Session *>(user_data);
DEBUG_HTTP2("Nghttp2Session %s: buffering data chunk for stream %d, size: "
"%d, flags: %d\n", SESSION_TYPE_NAME(handle), id, len, flags);
"%d, flags: %d\n", handle->TypeName(), id, len, flags);
Nghttp2Stream *stream = handle->FindStream(id);
nghttp2_data_chunk_t *chunk = data_chunk_free_list.pop();
chunk->buf = uv_buf_init(new char[len], len);
@@ -346,7 +346,7 @@ inline void Nghttp2Session::SubmitTrailers::Submit(nghttp2_nv *trailers,
if (length == 0)
return;
DEBUG_HTTP2("Nghttp2Session %s: sending trailers for stream %d, "
"count: %d\n", SESSION_TYPE_NAME(handle_), stream_->id(), length);
"count: %d\n", handle_->TypeName(), stream_->id(), length);
*flags_ |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
nghttp2_submit_trailer(handle_->session_,
stream_->id(),
@@ -356,8 +356,7 @@ inline void Nghttp2Session::SubmitTrailers::Submit(nghttp2_nv *trailers,

// See: https://nghttp2.org/documentation/nghttp2_submit_shutdown_notice.html
inline void Nghttp2Session::SubmitShutdownNotice() {
DEBUG_HTTP2("Nghttp2Session %s: submitting shutdown notice\n",
SESSION_TYPE_NAME(this));
DEBUG_HTTP2("Nghttp2Session %s: submitting shutdown notice\n", TypeName());
nghttp2_submit_shutdown_notice(session_);
}

@@ -367,7 +366,7 @@ inline void Nghttp2Session::SubmitShutdownNotice() {
inline int Nghttp2Session::SubmitSettings(const nghttp2_settings_entry iv[],
size_t niv) {
DEBUG_HTTP2("Nghttp2Session %s: submitting settings, count: %d\n",
SESSION_TYPE_NAME(this), niv);
TypeName(), niv);
return nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv, niv);
}

@@ -376,11 +375,11 @@ inline Nghttp2Stream* Nghttp2Session::FindStream(int32_t id) {
auto s = streams_.find(id);
if (s != streams_.end()) {
DEBUG_HTTP2("Nghttp2Session %s: stream %d found\n",
SESSION_TYPE_NAME(this), id);
TypeName(), id);
return s->second;
} else {
DEBUG_HTTP2("Nghttp2Session %s: stream %d not found\n",
SESSION_TYPE_NAME(this), id);
TypeName(), id);
return nullptr;
}
}
@@ -405,7 +404,7 @@ inline void Nghttp2Stream::FlushDataChunks(bool done) {
inline void Nghttp2Session::HandleDataFrame(const nghttp2_frame* frame) {
int32_t id = frame->hd.stream_id;
DEBUG_HTTP2("Nghttp2Session %s: handling data frame for stream %d\n",
SESSION_TYPE_NAME(this), id);
TypeName(), id);
Nghttp2Stream* stream = this->FindStream(id);
// If the stream does not exist, something really bad happened
CHECK_NE(stream, nullptr);
@@ -421,7 +420,7 @@ inline void Nghttp2Session::HandleHeadersFrame(const nghttp2_frame* frame) {
int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ?
frame->push_promise.promised_stream_id : frame->hd.stream_id;
DEBUG_HTTP2("Nghttp2Session %s: handling headers frame for stream %d\n",
SESSION_TYPE_NAME(this), id);
TypeName(), id);
Nghttp2Stream* stream = FindStream(id);
// If the stream does not exist, something really bad happened
CHECK_NE(stream, nullptr);
@@ -437,7 +436,7 @@ inline void Nghttp2Session::HandlePriorityFrame(const nghttp2_frame* frame) {
nghttp2_priority priority_frame = frame->priority;
int32_t id = frame->hd.stream_id;
DEBUG_HTTP2("Nghttp2Session %s: handling priority frame for stream %d\n",
SESSION_TYPE_NAME(this), id);
TypeName(), id);
// Ignore the priority frame if stream ID is <= 0
// This actually should never happen because nghttp2 should treat this as
// an error condition that terminates the session.
@@ -450,8 +449,7 @@ inline void Nghttp2Session::HandlePriorityFrame(const nghttp2_frame* frame) {
// Notifies the JS layer that a GOAWAY frame has been received
inline void Nghttp2Session::HandleGoawayFrame(const nghttp2_frame* frame) {
nghttp2_goaway goaway_frame = frame->goaway;
DEBUG_HTTP2("Nghttp2Session %s: handling goaway frame\n",
SESSION_TYPE_NAME(this));
DEBUG_HTTP2("Nghttp2Session %s: handling goaway frame\n", TypeName());

OnGoAway(goaway_frame.last_stream_id,
goaway_frame.error_code,
@@ -461,8 +459,7 @@ inline void Nghttp2Session::HandleGoawayFrame(const nghttp2_frame* frame) {

// Prompts nghttp2 to flush the queue of pending data frames
inline void Nghttp2Session::SendPendingData() {
DEBUG_HTTP2("Nghttp2Session %s: Sending pending data\n",
SESSION_TYPE_NAME(this));
DEBUG_HTTP2("Nghttp2Session %s: Sending pending data\n", TypeName());
// Do not attempt to send data on the socket if the destroying flag has
// been set. That means everything is shutting down and the socket
// will not be usable.
@@ -551,8 +548,7 @@ inline void Nghttp2Session::MarkDestroying() {

inline int Nghttp2Session::Free() {
assert(session_ != nullptr);
DEBUG_HTTP2("Nghttp2Session %s: freeing session\n",
SESSION_TYPE_NAME(this));
DEBUG_HTTP2("Nghttp2Session %s: freeing session\n", TypeName());
// Stop the loop
uv_prepare_stop(&prep_);
auto PrepClose = [](uv_handle_t* handle) {
@@ -566,8 +562,7 @@ inline int Nghttp2Session::Free() {
nghttp2_session_del(session_);
session_ = nullptr;
loop_ = nullptr;
DEBUG_HTTP2("Nghttp2Session %s: session freed\n",
SESSION_TYPE_NAME(this));
DEBUG_HTTP2("Nghttp2Session %s: session freed\n", TypeName());
return 1;
}

22 changes: 10 additions & 12 deletions src/node_http2_core.h
Original file line number Diff line number Diff line change
@@ -50,18 +50,6 @@ enum nghttp2_session_type {
NGHTTP2_SESSION_CLIENT
};

static inline const char* SessionTypeName(nghttp2_session_type type) {
switch (type) {
case NGHTTP2_SESSION_SERVER: return "server";
case NGHTTP2_SESSION_CLIENT: return "client";
default:
// This should never happen
ABORT();
}
}

#define SESSION_TYPE_NAME(session) SessionTypeName(session->session_type_)

enum nghttp2_shutdown_flags {
NGHTTP2_SHUTDOWN_FLAG_GRACEFUL
};
@@ -117,6 +105,16 @@ class Nghttp2Session {
return destroying_;
}

inline const char* TypeName(nghttp2_session_type type) {
switch (type) {
case NGHTTP2_SESSION_SERVER: return "server";
case NGHTTP2_SESSION_CLIENT: return "client";
default:
// This should never happen
ABORT();
}
}

// Returns the pointer to the identified stream, or nullptr if
// the stream does not exist
inline Nghttp2Stream* FindStream(int32_t id);