From 0a4fb24b9fce694a85328060f2f4804a45398df4 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 1 Mar 2019 19:47:27 +0100 Subject: [PATCH] Majorly simplify process.nextTick --- src/addon.cpp | 52 +-------------------------------------------------- src/uws.js | 10 +--------- 2 files changed, 2 insertions(+), 60 deletions(-) diff --git a/src/addon.cpp b/src/addon.cpp index 499e68c..10db94e 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -24,13 +24,6 @@ using namespace v8; /* These two are definitely static */ - -/* Warning: having nextTickQueue items at loop fallthrough is not allowed. - * Process will crash/hang due to destruction of V8 resouces after V8 itself - * has been destroyed. Either enforce nextTick calls to keep the loop rolling - * via for instance setTimeout or setImmediate, or make sure to drain completely - * the queue at process.on('beforeExit'). */ -std::vector> nextTickQueue; Isolate *isolate; #include "Utilities.h" @@ -39,43 +32,12 @@ Isolate *isolate; #include "HttpRequestWrapper.h" #include "AppWrapper.h" -/* We are not compatible with Node.js nextTick for performance (and standalone) reasons */ -void nextTick(const FunctionCallbackInfo &args) { - nextTickQueue.emplace_back(UniquePersistent(isolate, Local::Cast(args[0]))); -} - /* Used for debugging */ void print(const FunctionCallbackInfo &args) { NativeString nativeString(isolate, args[0]); std::cout << nativeString.getString() << std::endl; } -/* Does not guarantee empty queue because of recursive nextTick calls. - * Should return int queueSize after calling queued items, so that - * proper while(processNextTickQueueImpl()) can be done */ -int processNextTickQueueImpl(Isolate *isolate) { - - /* Run async continuations, promises and other V8-queued tasks */ - isolate->RunMicrotasks(); - - if (nextTickQueue.size()) { - /* Swap queues for recursive calls */ - std::vector> currentNextTickQueue = std::move(nextTickQueue); - - HandleScope hs(isolate); - for (UniquePersistent &f : currentNextTickQueue) { - Local::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr); - } - } - - return nextTickQueue.size(); -} - -/* It is possible to call this at process.beforeExit until it returns 0. */ -void processNextTickQueue(const FunctionCallbackInfo &args) { - args.GetReturnValue().Set(Integer::New(isolate, processNextTickQueueImpl(isolate))); -} - /* todo: Put this function and all inits of it in its own header */ void uWS_us_listen_socket_close(const FunctionCallbackInfo &args) { us_listen_socket_close((struct us_listen_socket *) External::Cast(*args[0])->Value()); @@ -85,27 +47,15 @@ void Main(Local exports) { /* I guess we store this statically */ isolate = exports->GetIsolate(); - /* We want this */ + /* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */ isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto); - /* Register our own nextTick handlers */ - uWS::Loop::defaultLoop()->setPostHandler([](uWS::Loop *) { - processNextTickQueueImpl(isolate); - }); - - /* We also do need it on pre */ - uWS::Loop::defaultLoop()->setPreHandler([](uWS::Loop *) { - processNextTickQueueImpl(isolate); - }); - /* Hook up our timers */ us_loop_integrate((us_loop *) uWS::Loop::defaultLoop()); /* uWS namespace */ exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App)->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App)->GetFunction()); - exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction()); - exports->Set(String::NewFromUtf8(isolate, "processNextTickQueue"), FunctionTemplate::New(isolate, processNextTickQueue)->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "print"), FunctionTemplate::New(isolate, print)->GetFunction()); /* Expose some µSockets functions directly under uWS namespace */ diff --git a/src/uws.js b/src/uws.js index 2f418a7..2ad88fa 100644 --- a/src/uws.js +++ b/src/uws.js @@ -20,18 +20,10 @@ module.exports = (() => { const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`); /* We are not compatible with Node.js nextTick and/or domains */ process.nextTick = (f, ...args) => { - uWS.nextTick(() => { + Promise.resolve().then(() => { f(...args); }); }; - process.on('beforeExit', () => { - if (uWS.processNextTickQueue()) { - setImmediate(() => { - - }); - } - }); - /* process.nextTick = setImmediate; */ return uWS; } catch (e) { throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString());