Skip to content

Commit e8c2917

Browse files
committed
src: simplify http2 perf tracking code
Use `unique_ptr`s and use the resulting simplification to reduce indentation in these functions. PR-URL: #19470 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 85a3d82 commit e8c2917

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

src/node_http2.cc

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -515,35 +515,35 @@ void Http2Stream::EmitStatistics() {
515515
Http2StreamPerformanceEntry* entry =
516516
new Http2StreamPerformanceEntry(env(), id_, statistics_);
517517
env()->SetImmediate([](Environment* env, void* data) {
518-
Http2StreamPerformanceEntry* entry =
519-
static_cast<Http2StreamPerformanceEntry*>(data);
520-
if (HasHttp2Observer(env)) {
521-
AliasedBuffer<double, v8::Float64Array>& buffer =
522-
env->http2_state()->stream_stats_buffer;
523-
buffer[IDX_STREAM_STATS_ID] = entry->id();
524-
if (entry->first_byte() != 0) {
525-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] =
526-
(entry->first_byte() - entry->startTimeNano()) / 1e6;
527-
} else {
528-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = 0;
529-
}
530-
if (entry->first_header() != 0) {
531-
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] =
532-
(entry->first_header() - entry->startTimeNano()) / 1e6;
533-
} else {
534-
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = 0;
535-
}
536-
if (entry->first_byte_sent() != 0) {
537-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] =
538-
(entry->first_byte_sent() - entry->startTimeNano()) / 1e6;
539-
} else {
540-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0;
541-
}
542-
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
543-
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
544-
entry->Notify(entry->ToObject());
518+
// This takes ownership, the entr is destroyed at the end of this scope.
519+
std::unique_ptr<Http2StreamPerformanceEntry> entry {
520+
static_cast<Http2StreamPerformanceEntry*>(data) };
521+
if (!HasHttp2Observer(env))
522+
return;
523+
AliasedBuffer<double, v8::Float64Array>& buffer =
524+
env->http2_state()->stream_stats_buffer;
525+
buffer[IDX_STREAM_STATS_ID] = entry->id();
526+
if (entry->first_byte() != 0) {
527+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] =
528+
(entry->first_byte() - entry->startTimeNano()) / 1e6;
529+
} else {
530+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = 0;
545531
}
546-
delete entry;
532+
if (entry->first_header() != 0) {
533+
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] =
534+
(entry->first_header() - entry->startTimeNano()) / 1e6;
535+
} else {
536+
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = 0;
537+
}
538+
if (entry->first_byte_sent() != 0) {
539+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] =
540+
(entry->first_byte_sent() - entry->startTimeNano()) / 1e6;
541+
} else {
542+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0;
543+
}
544+
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
545+
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
546+
entry->Notify(entry->ToObject());
547547
}, static_cast<void*>(entry));
548548
}
549549

@@ -553,25 +553,25 @@ void Http2Session::EmitStatistics() {
553553
Http2SessionPerformanceEntry* entry =
554554
new Http2SessionPerformanceEntry(env(), statistics_, session_type_);
555555
env()->SetImmediate([](Environment* env, void* data) {
556-
Http2SessionPerformanceEntry* entry =
557-
static_cast<Http2SessionPerformanceEntry*>(data);
558-
if (HasHttp2Observer(env)) {
559-
AliasedBuffer<double, v8::Float64Array>& buffer =
560-
env->http2_state()->session_stats_buffer;
561-
buffer[IDX_SESSION_STATS_TYPE] = entry->type();
562-
buffer[IDX_SESSION_STATS_PINGRTT] = entry->ping_rtt() / 1e6;
563-
buffer[IDX_SESSION_STATS_FRAMESRECEIVED] = entry->frame_count();
564-
buffer[IDX_SESSION_STATS_FRAMESSENT] = entry->frame_sent();
565-
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count();
566-
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] =
567-
entry->stream_average_duration();
568-
buffer[IDX_SESSION_STATS_DATA_SENT] = entry->data_sent();
569-
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
570-
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
571-
entry->max_concurrent_streams();
572-
entry->Notify(entry->ToObject());
573-
}
574-
delete entry;
556+
// This takes ownership, the entr is destroyed at the end of this scope.
557+
std::unique_ptr<Http2SessionPerformanceEntry> entry {
558+
static_cast<Http2SessionPerformanceEntry*>(data) };
559+
if (!HasHttp2Observer(env))
560+
return;
561+
AliasedBuffer<double, v8::Float64Array>& buffer =
562+
env->http2_state()->session_stats_buffer;
563+
buffer[IDX_SESSION_STATS_TYPE] = entry->type();
564+
buffer[IDX_SESSION_STATS_PINGRTT] = entry->ping_rtt() / 1e6;
565+
buffer[IDX_SESSION_STATS_FRAMESRECEIVED] = entry->frame_count();
566+
buffer[IDX_SESSION_STATS_FRAMESSENT] = entry->frame_sent();
567+
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count();
568+
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] =
569+
entry->stream_average_duration();
570+
buffer[IDX_SESSION_STATS_DATA_SENT] = entry->data_sent();
571+
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
572+
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
573+
entry->max_concurrent_streams();
574+
entry->Notify(entry->ToObject());
575575
}, static_cast<void*>(entry));
576576
}
577577

@@ -1379,6 +1379,7 @@ void Http2Session::MaybeScheduleWrite() {
13791379

13801380
// Sending data may call arbitrary JS code, so keep track of
13811381
// async context.
1382+
HandleScope handle_scope(env->isolate());
13821383
InternalCallbackScope callback_scope(session);
13831384
session->SendPendingData();
13841385
}, static_cast<void*>(this), object());

0 commit comments

Comments
 (0)