Skip to content

Commit 67aa5ef

Browse files
addaleaxBridgeAR
authored andcommitted
src: make ELDHistogram a HandleWrap
This simplifies the implementation of ELDHistogram a bit, and more generally allows us to have weak JS references associated with `HandleWrap`s. PR-URL: #29317 Reviewed-By: James M Snell <[email protected]>
1 parent 7dd897f commit 67aa5ef

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

src/async_wrap.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace node {
3434
#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
3535
V(NONE) \
3636
V(DNSCHANNEL) \
37+
V(ELDHISTOGRAM) \
3738
V(FILEHANDLE) \
3839
V(FILEHANDLECLOSEREQ) \
3940
V(FSEVENTWRAP) \

src/handle_wrap.cc

+13-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ void HandleWrap::Close(Local<Value> close_callback) {
8484
}
8585

8686

87+
void HandleWrap::MakeWeak() {
88+
persistent().SetWeak(
89+
this,
90+
[](const v8::WeakCallbackInfo<HandleWrap>& data) {
91+
HandleWrap* handle_wrap = data.GetParameter();
92+
handle_wrap->persistent().Reset();
93+
handle_wrap->Close();
94+
}, v8::WeakCallbackType::kParameter);
95+
}
96+
97+
8798
void HandleWrap::MarkAsInitialized() {
8899
env()->handle_wrap_queue()->PushBack(this);
89100
state_ = kInitialized;
@@ -115,15 +126,14 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
115126
HandleScope scope(env->isolate());
116127
Context::Scope context_scope(env->context());
117128

118-
// The wrap object should still be there.
119-
CHECK_EQ(wrap->persistent().IsEmpty(), false);
120129
CHECK_EQ(wrap->state_, kClosing);
121130

122131
wrap->state_ = kClosed;
123132

124133
wrap->OnClose();
125134

126-
if (wrap->object()->Has(env->context(), env->handle_onclose_symbol())
135+
if (!wrap->persistent().IsEmpty() &&
136+
wrap->object()->Has(env->context(), env->handle_onclose_symbol())
127137
.FromMaybe(false)) {
128138
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
129139
}

src/handle_wrap.h

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class HandleWrap : public AsyncWrap {
7676
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
7777
Environment* env);
7878

79+
void MakeWeak(); // This hides BaseObject::MakeWeak()
80+
7981
protected:
8082
HandleWrap(Environment* env,
8183
v8::Local<v8::Object> object,

src/node_perf.cc

+13-26
Original file line numberDiff line numberDiff line change
@@ -477,31 +477,18 @@ static void ELDHistogramNew(const FunctionCallbackInfo<Value>& args) {
477477
ELDHistogram::ELDHistogram(
478478
Environment* env,
479479
Local<Object> wrap,
480-
int32_t resolution) : BaseObject(env, wrap),
480+
int32_t resolution) : HandleWrap(env,
481+
wrap,
482+
reinterpret_cast<uv_handle_t*>(&timer_),
483+
AsyncWrap::PROVIDER_ELDHISTOGRAM),
481484
Histogram(1, 3.6e12),
482485
resolution_(resolution) {
483486
MakeWeak();
484-
timer_ = new uv_timer_t();
485-
uv_timer_init(env->event_loop(), timer_);
486-
timer_->data = this;
487+
uv_timer_init(env->event_loop(), &timer_);
487488
}
488489

489-
void ELDHistogram::CloseTimer() {
490-
if (timer_ == nullptr)
491-
return;
492-
493-
env()->CloseHandle(timer_, [](uv_timer_t* handle) { delete handle; });
494-
timer_ = nullptr;
495-
}
496-
497-
ELDHistogram::~ELDHistogram() {
498-
Disable();
499-
CloseTimer();
500-
}
501-
502-
void ELDHistogramDelayInterval(uv_timer_t* req) {
503-
ELDHistogram* histogram =
504-
reinterpret_cast<ELDHistogram*>(req->data);
490+
void ELDHistogram::DelayIntervalCallback(uv_timer_t* req) {
491+
ELDHistogram* histogram = ContainerOf(&ELDHistogram::timer_, req);
505492
histogram->RecordDelta();
506493
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
507494
"min", histogram->Min());
@@ -537,21 +524,21 @@ bool ELDHistogram::RecordDelta() {
537524
}
538525

539526
bool ELDHistogram::Enable() {
540-
if (enabled_) return false;
527+
if (enabled_ || IsHandleClosing()) return false;
541528
enabled_ = true;
542529
prev_ = 0;
543-
uv_timer_start(timer_,
544-
ELDHistogramDelayInterval,
530+
uv_timer_start(&timer_,
531+
DelayIntervalCallback,
545532
resolution_,
546533
resolution_);
547-
uv_unref(reinterpret_cast<uv_handle_t*>(timer_));
534+
uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
548535
return true;
549536
}
550537

551538
bool ELDHistogram::Disable() {
552-
if (!enabled_) return false;
539+
if (!enabled_ || IsHandleClosing()) return false;
553540
enabled_ = false;
554-
uv_timer_stop(timer_);
541+
uv_timer_stop(&timer_);
555542
return true;
556543
}
557544

src/node_perf.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ class GCPerformanceEntry : public PerformanceEntry {
123123
PerformanceGCKind gckind_;
124124
};
125125

126-
class ELDHistogram : public BaseObject, public Histogram {
126+
class ELDHistogram : public HandleWrap, public Histogram {
127127
public:
128128
ELDHistogram(Environment* env,
129129
Local<Object> wrap,
130130
int32_t resolution);
131131

132-
~ELDHistogram() override;
133-
134132
bool RecordDelta();
135133
bool Enable();
136134
bool Disable();
@@ -149,13 +147,13 @@ class ELDHistogram : public BaseObject, public Histogram {
149147
SET_SELF_SIZE(ELDHistogram)
150148

151149
private:
152-
void CloseTimer();
150+
static void DelayIntervalCallback(uv_timer_t* req);
153151

154152
bool enabled_ = false;
155153
int32_t resolution_ = 0;
156154
int64_t exceeds_ = 0;
157155
uint64_t prev_ = 0;
158-
uv_timer_t* timer_;
156+
uv_timer_t timer_;
159157
};
160158

161159
} // namespace performance

test/sequential/test-async-wrap-getasyncid.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const { getSystemErrorName } = require('util');
5050
delete providers.KEYPAIRGENREQUEST;
5151
delete providers.HTTPCLIENTREQUEST;
5252
delete providers.HTTPINCOMINGMESSAGE;
53+
delete providers.ELDHISTOGRAM;
5354

5455
const objKeys = Object.keys(providers);
5556
if (objKeys.length > 0)

0 commit comments

Comments
 (0)