Remove uWS.free, WorkerThreads graceful shutdown

This commit is contained in:
Alex Hultman 2020-10-15 18:43:32 +02:00
parent d6caec2240
commit fafd273753
2 changed files with 21 additions and 31 deletions

View File

@ -145,32 +145,6 @@ void uWS_cfg(const FunctionCallbackInfo<Value> &args) {
} }
} }
/* This has to be called in beforeExit, but exit also seems okay */
void uWS_free(const FunctionCallbackInfo<Value> &args) {
/* Holder is exports */
Local<Object> exports = args.Holder();
/* See if we even have free anymore */
if (exports->Get(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked() == Undefined(args.GetIsolate())) {
return;
}
/* Once we free uWS we remove the uWS.free function from exports */
exports->Set(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked(), Undefined(args.GetIsolate())).ToChecked();
/* We get the External holding perContextData */
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
/* Freeing apps here, it could be done earlier but not sooner */
perContextData->apps.clear();
perContextData->sslApps.clear();
/* Freeing the loop here means we give time for our timers to close, etc */
uWS::Loop::get()->free();
/* We can safely delete this since we no longer can call uWS.free */
delete perContextData;
}
/* todo: Put this function and all inits of it in its own header */ /* todo: Put this function and all inits of it in its own header */
void uWS_us_listen_socket_close(const FunctionCallbackInfo<Value> &args) { void uWS_us_listen_socket_close(const FunctionCallbackInfo<Value> &args) {
// this should take int ssl first // this should take int ssl first
@ -374,7 +348,7 @@ void uWS_unlock(const FunctionCallbackInfo<Value> &args) {
kvMutex.unlock(); kvMutex.unlock();
} }
void Main(Local<Object> exports) { PerContextData *Main(Local<Object> exports) {
/* We only care if it is defined, not what it says */ /* We only care if it is defined, not what it says */
experimental_fastcall = getenv("EXPERIMENTAL_FASTCALL") != nullptr; experimental_fastcall = getenv("EXPERIMENTAL_FASTCALL") != nullptr;
@ -403,7 +377,6 @@ void Main(Local<Object> exports) {
/* uWS namespace */ /* uWS namespace */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
/* Temporary KV store */ /* Temporary KV store */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
@ -441,6 +414,8 @@ void Main(Local<Object> exports) {
/* Listen options */ /* Listen options */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked();
return perContextData;
} }
/* This is required when building as a Node.js addon */ /* This is required when building as a Node.js addon */
@ -451,6 +426,22 @@ NODE_MODULE_INITIALIZER(Local<Object> exports, Local<Value> module, Local<Contex
/* Integrate uSockets with existing libuv loop */ /* Integrate uSockets with existing libuv loop */
uWS::Loop::get(node::GetCurrentEventLoop(context->GetIsolate())); uWS::Loop::get(node::GetCurrentEventLoop(context->GetIsolate()));
/* Register vanilla V8 addon */ /* Register vanilla V8 addon */
Main(exports); PerContextData *perContextData = Main(exports);
/* We cannot rely on process.exit or process.beforeExit when it comes to WorkerThreads */
node::AddEnvironmentCleanupHook(context->GetIsolate(), [](void *arg) {
PerContextData *perContextData = (PerContextData *) arg;
/* Freeing apps here, it could be done earlier but not sooner */
perContextData->apps.clear();
perContextData->sslApps.clear();
/* Freeing the loop here means we give time for our timers to close, etc */
uWS::Loop::get()->free();
/* We can safely delete this since we no longer can call uWS.free */
delete perContextData;
}, perContextData);
} }
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Authored by Alex Hultman, 2018-2019. * Authored by Alex Hultman, 2018-2020.
* Intellectual property of third-party. * Intellectual property of third-party.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -25,7 +25,6 @@ module.exports = (() => {
}); });
}; };
} }
process.on('exit', uWS.free);
return uWS; return uWS;
} catch (e) { } catch (e) {
throw new Error('This version of µWS is not compatible with your Node.js build:\n\n' + e.toString()); throw new Error('This version of µWS is not compatible with your Node.js build:\n\n' + e.toString());