Skip to content

Commit

Permalink
src: add GetActiveRequests and GetActiveHandles to async_wrap.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
RaisinTen committed Feb 7, 2021
1 parent 4aecb7f commit 7dd70b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const { setCallbackTrampoline } = async_wrap;
const {
async_hook_fields,
async_id_fields,
execution_async_resources
execution_async_resources,
getActiveRequests,
getActiveHandles,
} = async_wrap;
// Store the pair executionAsyncId and triggerAsyncId in a AliasedFloat64Array
// in Environment::AsyncHooks::async_ids_stack_ which tracks the resource
Expand Down Expand Up @@ -540,15 +542,6 @@ function triggerAsyncId() {
}


function getActiveRequests() {
return process._getActiveRequests;
}

function getActiveHandles() {
return process._getActiveHandles;
}


module.exports = {
executionAsyncId,
triggerAsyncId,
Expand Down
35 changes: 35 additions & 0 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,36 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
}

static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

std::vector<Local<Value>> request_v;
for (ReqWrapBase* req_wrap : *env->req_wrap_queue()) {
AsyncWrap* w = req_wrap->GetAsyncWrap();
if (w->persistent().IsEmpty())
continue;
request_v.emplace_back(w->GetOwner());
}

args.GetReturnValue().Set(
Array::New(env->isolate(), request_v.data(), request_v.size()));
}

// Non-static, friend of HandleWrap. Could have been a HandleWrap method but
// implemented here for consistency with GetActiveRequests().
void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

std::vector<Local<Value>> handle_v;
for (auto w : *env->handle_wrap_queue()) {
if (!HandleWrap::HasRef(w))
continue;
handle_v.emplace_back(w->GetOwner());
}
args.GetReturnValue().Set(
Array::New(env->isolate(), handle_v.data(), handle_v.size()));
}

void AsyncWrap::GetAsyncId(const FunctionCallbackInfo<Value>& args) {
AsyncWrap* wrap;
args.GetReturnValue().Set(kInvalidAsyncId);
Expand Down Expand Up @@ -634,6 +664,9 @@ void AsyncWrap::Initialize(Local<Object> target,
env->SetMethod(target, "disablePromiseHook", DisablePromiseHook);
env->SetMethod(target, "registerDestroyHook", RegisterDestroyHook);

env->SetMethod(target, "getActiveRequests", GetActiveRequests);
env->SetMethod(target, "getActiveHandles", GetActiveHandles);

PropertyAttribute ReadOnlyDontDelete =
static_cast<PropertyAttribute>(ReadOnly | DontDelete);

Expand Down Expand Up @@ -732,6 +765,8 @@ void AsyncWrap::RegisterExternalReferences(
registry->Register(AsyncWrap::GetProviderType);
registry->Register(PromiseWrap::GetAsyncId);
registry->Register(PromiseWrap::GetTriggerAsyncId);
registry->Register(GetActiveRequests);
registry->Register(GetActiveHandles);
}

AsyncWrap::AsyncWrap(Environment* env,
Expand Down

0 comments on commit 7dd70b2

Please sign in to comment.