Skip to content

Commit 4745ac4

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

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
@@ -71,10 +71,6 @@ BaseObject::~BaseObject() {
7171
}
7272
}
7373

74-
void BaseObject::RemoveCleanupHook() {
75-
env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
76-
}
77-
7874
void BaseObject::Detach() {
7975
CHECK_GT(pointer_data()->strong_ptr_count, 0);
8076
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
@@ -243,7 +243,6 @@ Http2Session::Http2Settings::Http2Settings(Environment* env,
243243
: AsyncWrap(env, obj, PROVIDER_HTTP2SETTINGS),
244244
session_(session),
245245
startTime_(start_time) {
246-
RemoveCleanupHook(); // This object is owned by the Http2Session.
247246
Init();
248247
}
249248

@@ -586,8 +585,6 @@ Http2Session::Http2Session(Environment* env,
586585
Http2Session::~Http2Session() {
587586
CHECK_EQ(flags_ & SESSION_STATE_HAS_SCOPE, 0);
588587
Debug(this, "freeing nghttp2 session");
589-
for (const auto& iter : streams_)
590-
iter.second->session_ = nullptr;
591588
nghttp2_session_del(session_);
592589
CHECK_EQ(current_nghttp2_memory_, 0);
593590
}
@@ -695,7 +692,7 @@ void Http2Session::Close(uint32_t code, bool socket_closed) {
695692
// If there are outstanding pings, those will need to be canceled, do
696693
// so on the next iteration of the event loop to avoid calling out into
697694
// javascript since this may be called during garbage collection.
698-
while (std::unique_ptr<Http2Ping> ping = PopPing()) {
695+
while (BaseObjectPtr<Http2Ping> ping = PopPing()) {
699696
ping->DetachFromSession();
700697
env()->SetImmediate(
701698
[ping = std::move(ping)](Environment* env) {
@@ -1451,7 +1448,7 @@ void Http2Session::HandlePingFrame(const nghttp2_frame* frame) {
14511448
Local<Value> arg;
14521449
bool ack = frame->hd.flags & NGHTTP2_FLAG_ACK;
14531450
if (ack) {
1454-
std::unique_ptr<Http2Ping> ping = PopPing();
1451+
BaseObjectPtr<Http2Ping> ping = PopPing();
14551452

14561453
if (!ping) {
14571454
// PING Ack is unsolicited. Treat as a connection error. The HTTP/2
@@ -1490,7 +1487,7 @@ void Http2Session::HandleSettingsFrame(const nghttp2_frame* frame) {
14901487

14911488
// If this is an acknowledgement, we should have an Http2Settings
14921489
// object for it.
1493-
std::unique_ptr<Http2Settings> settings = PopSettings();
1490+
BaseObjectPtr<Http2Settings> settings = PopSettings();
14941491
if (settings) {
14951492
settings->Done(true);
14961493
return;
@@ -1951,12 +1948,11 @@ Http2Stream::~Http2Stream() {
19511948
nghttp2_rcbuf_decref(header.value);
19521949
}
19531950

1954-
if (session_ == nullptr)
1951+
if (!session_)
19551952
return;
19561953
Debug(this, "tearing down stream");
19571954
session_->DecrementCurrentSessionMemory(current_headers_length_);
19581955
session_->RemoveStream(this);
1959-
session_ = nullptr;
19601956
}
19611957

19621958
std::string Http2Stream::diagnostic_name() const {
@@ -2164,8 +2160,10 @@ Http2Stream* Http2Stream::SubmitPushPromise(nghttp2_nv* nva,
21642160
id_, nva, len, nullptr);
21652161
CHECK_NE(*ret, NGHTTP2_ERR_NOMEM);
21662162
Http2Stream* stream = nullptr;
2167-
if (*ret > 0)
2168-
stream = Http2Stream::New(session_, *ret, NGHTTP2_HCAT_HEADERS, options);
2163+
if (*ret > 0) {
2164+
stream = Http2Stream::New(
2165+
session_.get(), *ret, NGHTTP2_HCAT_HEADERS, options);
2166+
}
21692167

21702168
return stream;
21712169
}
@@ -2832,7 +2830,8 @@ void Http2Session::Ping(const FunctionCallbackInfo<Value>& args) {
28322830
if (obj->Set(env->context(), env->ondone_string(), args[1]).IsNothing())
28332831
return;
28342832

2835-
Http2Ping* ping = session->AddPing(std::make_unique<Http2Ping>(session, obj));
2833+
Http2Ping* ping = session->AddPing(
2834+
MakeDetachedBaseObject<Http2Ping>(session, obj));
28362835
// To prevent abuse, we strictly limit the number of unacknowledged PING
28372836
// frames that may be sent at any given time. This is configurable in the
28382837
// Options when creating a Http2Session.
@@ -2861,16 +2860,16 @@ void Http2Session::Settings(const FunctionCallbackInfo<Value>& args) {
28612860
if (obj->Set(env->context(), env->ondone_string(), args[0]).IsNothing())
28622861
return;
28632862

2864-
Http2Session::Http2Settings* settings = session->AddSettings(
2865-
std::make_unique<Http2Settings>(session->env(), session, obj, 0));
2863+
Http2Settings* settings = session->AddSettings(
2864+
MakeDetachedBaseObject<Http2Settings>(session->env(), session, obj, 0));
28662865
if (settings == nullptr) return args.GetReturnValue().Set(false);
28672866

28682867
settings->Send();
28692868
args.GetReturnValue().Set(true);
28702869
}
28712870

2872-
std::unique_ptr<Http2Session::Http2Ping> Http2Session::PopPing() {
2873-
std::unique_ptr<Http2Ping> ping;
2871+
BaseObjectPtr<Http2Session::Http2Ping> Http2Session::PopPing() {
2872+
BaseObjectPtr<Http2Ping> ping;
28742873
if (!outstanding_pings_.empty()) {
28752874
ping = std::move(outstanding_pings_.front());
28762875
outstanding_pings_.pop();
@@ -2880,7 +2879,7 @@ std::unique_ptr<Http2Session::Http2Ping> Http2Session::PopPing() {
28802879
}
28812880

28822881
Http2Session::Http2Ping* Http2Session::AddPing(
2883-
std::unique_ptr<Http2Session::Http2Ping> ping) {
2882+
BaseObjectPtr<Http2Session::Http2Ping> ping) {
28842883
if (outstanding_pings_.size() == max_outstanding_pings_) {
28852884
ping->Done(false);
28862885
return nullptr;
@@ -2891,8 +2890,8 @@ Http2Session::Http2Ping* Http2Session::AddPing(
28912890
return ptr;
28922891
}
28932892

2894-
std::unique_ptr<Http2Session::Http2Settings> Http2Session::PopSettings() {
2895-
std::unique_ptr<Http2Settings> settings;
2893+
BaseObjectPtr<Http2Session::Http2Settings> Http2Session::PopSettings() {
2894+
BaseObjectPtr<Http2Settings> settings;
28962895
if (!outstanding_settings_.empty()) {
28972896
settings = std::move(outstanding_settings_.front());
28982897
outstanding_settings_.pop();
@@ -2902,7 +2901,7 @@ std::unique_ptr<Http2Session::Http2Settings> Http2Session::PopSettings() {
29022901
}
29032902

29042903
Http2Session::Http2Settings* Http2Session::AddSettings(
2905-
std::unique_ptr<Http2Session::Http2Settings> settings) {
2904+
BaseObjectPtr<Http2Session::Http2Settings> settings) {
29062905
if (outstanding_settings_.size() == max_outstanding_settings_) {
29072906
settings->Done(false);
29082907
return nullptr;
@@ -2917,7 +2916,6 @@ Http2Session::Http2Ping::Http2Ping(Http2Session* session, Local<Object> obj)
29172916
: AsyncWrap(session->env(), obj, AsyncWrap::PROVIDER_HTTP2PING),
29182917
session_(session),
29192918
startTime_(uv_hrtime()) {
2920-
RemoveCleanupHook(); // This object is owned by the Http2Session.
29212919
}
29222920

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

src/node_http2.h

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

461461
nghttp2_stream* operator*();
462462

463-
Http2Session* session() { return session_; }
464-
const Http2Session* session() const { return session_; }
463+
Http2Session* session() { return session_.get(); }
464+
const Http2Session* session() const { return session_.get(); }
465465

466466
void EmitStatistics();
467467

@@ -614,7 +614,7 @@ class Http2Stream : public AsyncWrap,
614614
nghttp2_headers_category category,
615615
int options);
616616

617-
Http2Session* session_ = nullptr; // The Parent HTTP/2 Session
617+
BaseObjectWeakPtr<Http2Session> session_; // The Parent HTTP/2 Session
618618
int32_t id_ = 0; // The Stream Identifier
619619
int32_t code_ = NGHTTP2_NO_ERROR; // The RST_STREAM code (if any)
620620
int flags_ = NGHTTP2_STREAM_FLAG_NONE; // Internal state flags
@@ -846,11 +846,11 @@ class Http2Session : public AsyncWrap,
846846
return env()->event_loop();
847847
}
848848

849-
std::unique_ptr<Http2Ping> PopPing();
850-
Http2Ping* AddPing(std::unique_ptr<Http2Ping> ping);
849+
BaseObjectPtr<Http2Ping> PopPing();
850+
Http2Ping* AddPing(BaseObjectPtr<Http2Ping> ping);
851851

852-
std::unique_ptr<Http2Settings> PopSettings();
853-
Http2Settings* AddSettings(std::unique_ptr<Http2Settings> settings);
852+
BaseObjectPtr<Http2Settings> PopSettings();
853+
Http2Settings* AddSettings(BaseObjectPtr<Http2Settings> settings);
854854

855855
void IncrementCurrentSessionMemory(uint64_t amount) {
856856
current_session_memory_ += amount;
@@ -1027,10 +1027,10 @@ class Http2Session : public AsyncWrap,
10271027
size_t stream_buf_offset_ = 0;
10281028

10291029
size_t max_outstanding_pings_ = DEFAULT_MAX_PINGS;
1030-
std::queue<std::unique_ptr<Http2Ping>> outstanding_pings_;
1030+
std::queue<BaseObjectPtr<Http2Ping>> outstanding_pings_;
10311031

10321032
size_t max_outstanding_settings_ = DEFAULT_MAX_SETTINGS;
1033-
std::queue<std::unique_ptr<Http2Settings>> outstanding_settings_;
1033+
std::queue<BaseObjectPtr<Http2Settings>> outstanding_settings_;
10341034

10351035
std::vector<nghttp2_stream_write> outgoing_buffers_;
10361036
std::vector<uint8_t> outgoing_storage_;

0 commit comments

Comments
 (0)