Skip to content

Commit 6665881

Browse files
addaleaxMylesBorins
authored andcommitted
http2: use custom BaseObject smart pointers
Refs: nodejs/quic#141 Reviewed-By: James M Snell <[email protected]> PR-URL: #30374 Refs: nodejs/quic#149 Refs: nodejs/quic#165 Reviewed-By: David Carlier <[email protected]>
1 parent a2dbadc commit 6665881

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

src/base_object-inl.h

-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ BaseObject::~BaseObject() {
6363
}
6464
}
6565

66-
void BaseObject::RemoveCleanupHook() {
67-
env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
68-
}
69-
7066
void BaseObject::Detach() {
7167
CHECK_GT(pointer_data()->strong_ptr_count, 0);
7268
pointer_data()->is_detached = true;

src/base_object.h

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class BaseObject : public MemoryRetainer {
9797
inline void Detach();
9898

9999
protected:
100-
inline void RemoveCleanupHook(); // TODO(addaleax): Remove.
101100
virtual inline void OnGCCollect();
102101

103102
private:

src/node_http2.cc

+18-20
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ Http2Session::Http2Settings::Http2Settings(Environment* env,
239239
: AsyncWrap(env, obj, PROVIDER_HTTP2SETTINGS),
240240
session_(session),
241241
startTime_(start_time) {
242-
RemoveCleanupHook(); // This object is owned by the Http2Session.
243242
Init();
244243
}
245244

@@ -658,8 +657,6 @@ Http2Session::Http2Session(Environment* env,
658657
Http2Session::~Http2Session() {
659658
CHECK_EQ(flags_ & SESSION_STATE_HAS_SCOPE, 0);
660659
Debug(this, "freeing nghttp2 session");
661-
for (const auto& iter : streams_)
662-
iter.second->session_ = nullptr;
663660
nghttp2_session_del(session_);
664661
CHECK_EQ(current_nghttp2_memory_, 0);
665662
}
@@ -767,7 +764,7 @@ void Http2Session::Close(uint32_t code, bool socket_closed) {
767764
// If there are outstanding pings, those will need to be canceled, do
768765
// so on the next iteration of the event loop to avoid calling out into
769766
// javascript since this may be called during garbage collection.
770-
while (std::unique_ptr<Http2Ping> ping = PopPing()) {
767+
while (BaseObjectPtr<Http2Ping> ping = PopPing()) {
771768
ping->DetachFromSession();
772769
env()->SetImmediate(
773770
[ping = std::move(ping)](Environment* env) {
@@ -1483,7 +1480,7 @@ void Http2Session::HandlePingFrame(const nghttp2_frame* frame) {
14831480
Local<Value> arg;
14841481
bool ack = frame->hd.flags & NGHTTP2_FLAG_ACK;
14851482
if (ack) {
1486-
std::unique_ptr<Http2Ping> ping = PopPing();
1483+
BaseObjectPtr<Http2Ping> ping = PopPing();
14871484

14881485
if (!ping) {
14891486
// PING Ack is unsolicited. Treat as a connection error. The HTTP/2
@@ -1522,7 +1519,7 @@ void Http2Session::HandleSettingsFrame(const nghttp2_frame* frame) {
15221519

15231520
// If this is an acknowledgement, we should have an Http2Settings
15241521
// object for it.
1525-
std::unique_ptr<Http2Settings> settings = PopSettings();
1522+
BaseObjectPtr<Http2Settings> settings = PopSettings();
15261523
if (settings) {
15271524
settings->Done(true);
15281525
return;
@@ -1982,12 +1979,11 @@ Http2Stream::~Http2Stream() {
19821979
nghttp2_rcbuf_decref(header.value);
19831980
}
19841981

1985-
if (session_ == nullptr)
1982+
if (!session_)
19861983
return;
19871984
Debug(this, "tearing down stream");
19881985
session_->DecrementCurrentSessionMemory(current_headers_length_);
19891986
session_->RemoveStream(this);
1990-
session_ = nullptr;
19911987
}
19921988

19931989
std::string Http2Stream::diagnostic_name() const {
@@ -2189,8 +2185,10 @@ Http2Stream* Http2Stream::SubmitPushPromise(nghttp2_nv* nva,
21892185
id_, nva, len, nullptr);
21902186
CHECK_NE(*ret, NGHTTP2_ERR_NOMEM);
21912187
Http2Stream* stream = nullptr;
2192-
if (*ret > 0)
2193-
stream = Http2Stream::New(session_, *ret, NGHTTP2_HCAT_HEADERS, options);
2188+
if (*ret > 0) {
2189+
stream = Http2Stream::New(
2190+
session_.get(), *ret, NGHTTP2_HCAT_HEADERS, options);
2191+
}
21942192

21952193
return stream;
21962194
}
@@ -2855,7 +2853,8 @@ void Http2Session::Ping(const FunctionCallbackInfo<Value>& args) {
28552853
if (obj->Set(env->context(), env->ondone_string(), args[1]).IsNothing())
28562854
return;
28572855

2858-
Http2Ping* ping = session->AddPing(std::make_unique<Http2Ping>(session, obj));
2856+
Http2Ping* ping = session->AddPing(
2857+
MakeDetachedBaseObject<Http2Ping>(session, obj));
28592858
// To prevent abuse, we strictly limit the number of unacknowledged PING
28602859
// frames that may be sent at any given time. This is configurable in the
28612860
// Options when creating a Http2Session.
@@ -2884,16 +2883,16 @@ void Http2Session::Settings(const FunctionCallbackInfo<Value>& args) {
28842883
if (obj->Set(env->context(), env->ondone_string(), args[0]).IsNothing())
28852884
return;
28862885

2887-
Http2Session::Http2Settings* settings = session->AddSettings(
2888-
std::make_unique<Http2Settings>(session->env(), session, obj, 0));
2886+
Http2Settings* settings = session->AddSettings(
2887+
MakeDetachedBaseObject<Http2Settings>(session->env(), session, obj, 0));
28892888
if (settings == nullptr) return args.GetReturnValue().Set(false);
28902889

28912890
settings->Send();
28922891
args.GetReturnValue().Set(true);
28932892
}
28942893

2895-
std::unique_ptr<Http2Session::Http2Ping> Http2Session::PopPing() {
2896-
std::unique_ptr<Http2Ping> ping;
2894+
BaseObjectPtr<Http2Session::Http2Ping> Http2Session::PopPing() {
2895+
BaseObjectPtr<Http2Ping> ping;
28972896
if (!outstanding_pings_.empty()) {
28982897
ping = std::move(outstanding_pings_.front());
28992898
outstanding_pings_.pop();
@@ -2903,7 +2902,7 @@ std::unique_ptr<Http2Session::Http2Ping> Http2Session::PopPing() {
29032902
}
29042903

29052904
Http2Session::Http2Ping* Http2Session::AddPing(
2906-
std::unique_ptr<Http2Session::Http2Ping> ping) {
2905+
BaseObjectPtr<Http2Session::Http2Ping> ping) {
29072906
if (outstanding_pings_.size() == max_outstanding_pings_) {
29082907
ping->Done(false);
29092908
return nullptr;
@@ -2914,8 +2913,8 @@ Http2Session::Http2Ping* Http2Session::AddPing(
29142913
return ptr;
29152914
}
29162915

2917-
std::unique_ptr<Http2Session::Http2Settings> Http2Session::PopSettings() {
2918-
std::unique_ptr<Http2Settings> settings;
2916+
BaseObjectPtr<Http2Session::Http2Settings> Http2Session::PopSettings() {
2917+
BaseObjectPtr<Http2Settings> settings;
29192918
if (!outstanding_settings_.empty()) {
29202919
settings = std::move(outstanding_settings_.front());
29212920
outstanding_settings_.pop();
@@ -2925,7 +2924,7 @@ std::unique_ptr<Http2Session::Http2Settings> Http2Session::PopSettings() {
29252924
}
29262925

29272926
Http2Session::Http2Settings* Http2Session::AddSettings(
2928-
std::unique_ptr<Http2Session::Http2Settings> settings) {
2927+
BaseObjectPtr<Http2Session::Http2Settings> settings) {
29292928
if (outstanding_settings_.size() == max_outstanding_settings_) {
29302929
settings->Done(false);
29312930
return nullptr;
@@ -2940,7 +2939,6 @@ Http2Session::Http2Ping::Http2Ping(Http2Session* session, Local<Object> obj)
29402939
: AsyncWrap(session->env(), obj, AsyncWrap::PROVIDER_HTTP2PING),
29412940
session_(session),
29422941
startTime_(uv_hrtime()) {
2943-
RemoveCleanupHook(); // This object is owned by the Http2Session.
29442942
}
29452943

29462944
void Http2Session::Http2Ping::Send(const uint8_t* payload) {

src/node_http2.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ class Http2Stream : public AsyncWrap,
456456

457457
nghttp2_stream* operator*();
458458

459-
Http2Session* session() { return session_; }
460-
const Http2Session* session() const { return session_; }
459+
Http2Session* session() { return session_.get(); }
460+
const Http2Session* session() const { return session_.get(); }
461461

462462
void EmitStatistics();
463463

@@ -609,7 +609,7 @@ class Http2Stream : public AsyncWrap,
609609
nghttp2_headers_category category,
610610
int options);
611611

612-
Http2Session* session_ = nullptr; // The Parent HTTP/2 Session
612+
BaseObjectWeakPtr<Http2Session> session_; // The Parent HTTP/2 Session
613613
int32_t id_ = 0; // The Stream Identifier
614614
int32_t code_ = NGHTTP2_NO_ERROR; // The RST_STREAM code (if any)
615615
int flags_ = NGHTTP2_STREAM_FLAG_NONE; // Internal state flags
@@ -822,11 +822,11 @@ class Http2Session : public AsyncWrap, public StreamListener {
822822
return env()->event_loop();
823823
}
824824

825-
std::unique_ptr<Http2Ping> PopPing();
826-
Http2Ping* AddPing(std::unique_ptr<Http2Ping> ping);
825+
BaseObjectPtr<Http2Ping> PopPing();
826+
Http2Ping* AddPing(BaseObjectPtr<Http2Ping> ping);
827827

828-
std::unique_ptr<Http2Settings> PopSettings();
829-
Http2Settings* AddSettings(std::unique_ptr<Http2Settings> settings);
828+
BaseObjectPtr<Http2Settings> PopSettings();
829+
Http2Settings* AddSettings(BaseObjectPtr<Http2Settings> settings);
830830

831831
void IncrementCurrentSessionMemory(uint64_t amount) {
832832
current_session_memory_ += amount;
@@ -1001,10 +1001,10 @@ class Http2Session : public AsyncWrap, public StreamListener {
10011001
size_t stream_buf_offset_ = 0;
10021002

10031003
size_t max_outstanding_pings_ = DEFAULT_MAX_PINGS;
1004-
std::queue<std::unique_ptr<Http2Ping>> outstanding_pings_;
1004+
std::queue<BaseObjectPtr<Http2Ping>> outstanding_pings_;
10051005

10061006
size_t max_outstanding_settings_ = DEFAULT_MAX_SETTINGS;
1007-
std::queue<std::unique_ptr<Http2Settings>> outstanding_settings_;
1007+
std::queue<BaseObjectPtr<Http2Settings>> outstanding_settings_;
10081008

10091009
std::vector<nghttp2_stream_write> outgoing_buffers_;
10101010
std::vector<uint8_t> outgoing_storage_;

0 commit comments

Comments
 (0)