Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

C++-side refactor #126

Closed
wants to merge 12 commits into from
12 changes: 5 additions & 7 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ function onSessionClose(code, family) {
// some basic information about the ALPN, SNI, and Ciphers that are
// being requested. It is only called if the 'clientHello' event is
// listened for.
function onSessionClientHello(alpn, servername, ciphers, callback) {
callback = callback.bind(this);
function onSessionClientHello(alpn, servername, ciphers) {
this[owner_symbol][kClientHello](
alpn,
servername,
Expand All @@ -274,7 +273,7 @@ function onSessionClientHello(alpn, servername, ciphers, callback) {
return;
}
try {
callback(...args);
this.onClientHelloDone(...args);
} catch (err) {
this[owner_symbol].destroy(err);
}
Expand All @@ -283,10 +282,9 @@ function onSessionClientHello(alpn, servername, ciphers, callback) {

// This callback is only ever invoked for QuicServerSession instances,
// and is used to trigger OCSP request processing when needed. The
// user callback must invoke the callback function in order for the
// user callback must invoke .onCertDone() in order for the
// TLS handshake to continue.
function onSessionCert(servername, callback) {
callback = callback.bind(this);
function onSessionCert(servername) {
this[owner_symbol][kCert](servername, (err, context, ocspResponse) => {
if (err) {
this[owner_symbol].destroy(err);
Expand All @@ -311,7 +309,7 @@ function onSessionCert(servername, callback) {
}
}
try {
callback(context ? context.context : undefined, ocspResponse);
this.onCertDone(context ? context.context : undefined, ocspResponse);
} catch (err) {
this[owner_symbol].destroy(err);
}
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@
'src/fs_event_wrap.cc',
'src/handle_wrap.cc',
'src/heap_utils.cc',
'src/histogram.cc',
'src/js_native_api.h',
'src/js_native_api_types.h',
'src/js_native_api_v8.cc',
Expand Down Expand Up @@ -621,6 +622,7 @@
'src/node_internals.h',
'src/node_main_instance.h',
'src/node_mem.h',
'src/node_mem-inl.h',
'src/node_messaging.h',
'src/node_metadata.h',
'src/node_mutex.h',
Expand Down
126 changes: 15 additions & 111 deletions src/histogram-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,46 @@

namespace node {

inline Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
CHECK_EQ(0, hdr_init(lowest, highest, figures, &histogram_));
}

inline Histogram::~Histogram() {
Histogram::~Histogram() {
hdr_close(histogram_);
}

inline void Histogram::Reset() {
void Histogram::Reset() {
hdr_reset(histogram_);
}

inline bool Histogram::Record(int64_t value) {
bool Histogram::Record(int64_t value) {
return hdr_record_value(histogram_, value);
}

inline int64_t Histogram::Min() {
int64_t Histogram::Min() {
return hdr_min(histogram_);
}

inline int64_t Histogram::Max() {
int64_t Histogram::Max() {
return hdr_max(histogram_);
}

inline double Histogram::Mean() {
double Histogram::Mean() {
return hdr_mean(histogram_);
}

inline double Histogram::Stddev() {
double Histogram::Stddev() {
return hdr_stddev(histogram_);
}

inline double Histogram::Percentile(double percentile) {
double Histogram::Percentile(double percentile) {
CHECK_GT(percentile, 0);
CHECK_LE(percentile, 100);
return static_cast<double>(hdr_value_at_percentile(histogram_, percentile));
}

inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
template <typename Iterator>
void Histogram::Percentiles(Iterator&& fn) {
hdr_iter iter;
hdr_iter_percentile_init(&iter, histogram_, 1);
while (hdr_iter_next(&iter)) {
Expand All @@ -57,7 +58,7 @@ inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
}
}

inline HistogramBase::HistogramBase(
HistogramBase::HistogramBase(
Environment* env,
v8::Local<v8::Object> wrap,
int64_t lowest,
Expand All @@ -66,7 +67,7 @@ inline HistogramBase::HistogramBase(
BaseObject(env, wrap),
Histogram(lowest, highest, figures) {}

inline bool HistogramBase::RecordDelta() {
bool HistogramBase::RecordDelta() {
uint64_t time = uv_hrtime();
bool ret = true;
if (prev_ > 0) {
Expand All @@ -85,110 +86,13 @@ inline bool HistogramBase::RecordDelta() {
return ret;
}

inline void HistogramBase::ResetState() {
void HistogramBase::ResetState() {
Reset();
exceeds_ = 0;
prev_ = 0;
}

inline void HistogramBase::HistogramMin(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Min());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramMax(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Max());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramMean(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Mean());
}

inline void HistogramBase::HistogramExceeds(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Exceeds());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramStddev(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Stddev());
}

inline void HistogramBase::HistogramPercentile(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsNumber());
double percentile = args[0].As<v8::Number>()->Value();
args.GetReturnValue().Set(histogram->Percentile(percentile));
}

inline void HistogramBase::HistogramPercentiles(
const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsMap());
v8::Local<v8::Map> map = args[0].As<v8::Map>();
histogram->Percentiles([&](double key, double value) {
map->Set(
env->context(),
v8::Number::New(env->isolate(), key),
v8::Number::New(env->isolate(), value)).IsEmpty();
});
}

inline void HistogramBase::HistogramReset(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
histogram->ResetState();
}

inline void HistogramBase::Initialize(Environment* env) {
// Guard against multiple initializations
if (!env->histogram_ctor_template().IsEmpty())
return;

v8::Local<v8::String> classname =
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");

v8::Local<v8::FunctionTemplate> histogram =
v8::FunctionTemplate::New(env->isolate());
histogram->SetClassName(classname);

v8::Local<v8::ObjectTemplate> histogramt =
histogram->InstanceTemplate();

histogramt->SetInternalFieldCount(1);
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
env->SetProtoMethod(histogram, "min", HistogramMin);
env->SetProtoMethod(histogram, "max", HistogramMax);
env->SetProtoMethod(histogram, "mean", HistogramMean);
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
env->SetProtoMethod(histogram, "reset", HistogramReset);

env->set_histogram_ctor_template(histogramt);
}

inline HistogramBase* HistogramBase::New(
HistogramBase* HistogramBase::New(
Environment* env,
int64_t lowest,
int64_t highest,
Expand Down
111 changes: 111 additions & 0 deletions src/histogram.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "histogram.h" // NOLINT(build/include_inline)
#include "histogram-inl.h"
addaleax marked this conversation as resolved.
Show resolved Hide resolved
#include "memory_tracker-inl.h"

namespace node {

using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Local;
using v8::Map;
using v8::Number;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;

void HistogramBase::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("histogram", GetMemorySize());
}

void HistogramBase::HistogramMin(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Min());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramMax(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Max());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramMean(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Mean());
}

void HistogramBase::HistogramExceeds(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Exceeds());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramStddev(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Stddev());
}

void HistogramBase::HistogramPercentile(
const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsNumber());
double percentile = args[0].As<Number>()->Value();
args.GetReturnValue().Set(histogram->Percentile(percentile));
}

void HistogramBase::HistogramPercentiles(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsMap());
Local<Map> map = args[0].As<Map>();
histogram->Percentiles([&](double key, double value) {
map->Set(
env->context(),
Number::New(env->isolate(), key),
Number::New(env->isolate(), value)).IsEmpty();
});
}

void HistogramBase::HistogramReset(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
histogram->ResetState();
}

void HistogramBase::Initialize(Environment* env) {
// Guard against multiple initializations
if (!env->histogram_ctor_template().IsEmpty())
return;

Local<String> classname =
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");

Local<FunctionTemplate> histogram =
FunctionTemplate::New(env->isolate());
histogram->SetClassName(classname);

Local<ObjectTemplate> histogramt =
histogram->InstanceTemplate();

histogramt->SetInternalFieldCount(1);
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
env->SetProtoMethod(histogram, "min", HistogramMin);
env->SetProtoMethod(histogram, "max", HistogramMax);
env->SetProtoMethod(histogram, "mean", HistogramMean);
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
env->SetProtoMethod(histogram, "reset", HistogramReset);

env->set_histogram_ctor_template(histogramt);
}

} // namespace node
Loading