From 42cfaa99bd3490bb0849fd66aa56016e763a56a8 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Mon, 10 Apr 2017 15:43:09 -0700 Subject: Add native tag completion callbacks, dispose of server after tryShutdown succeeds --- src/node/ext/call.cc | 23 ++++++++++++++++++++--- src/node/ext/call.h | 1 + src/node/ext/server.cc | 37 +++++++++++++++++++++++++++++++++++++ src/node/ext/server.h | 2 ++ src/node/ext/server_uv.cc | 3 +++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index 8d5e2ced6a..bb11cfb83a 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -217,6 +217,8 @@ class SendMetadataOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { return "send_metadata"; @@ -260,6 +262,8 @@ class SendMessageOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { return "send_message"; @@ -280,6 +284,8 @@ class SendClientCloseOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { return "client_close"; @@ -349,6 +355,8 @@ class SendServerStatusOp : public Op { bool IsFinalOp() { return true; } + void OnComplete() { + } protected: std::string GetTypeString() const { return "send_status"; @@ -381,6 +389,8 @@ class GetMetadataOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { @@ -413,6 +423,8 @@ class ReadMessageOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { @@ -454,6 +466,8 @@ class ClientStatusOp : public Op { bool IsFinalOp() { return true; } + void OnComplete() { + } protected: std::string GetTypeString() const { return "status"; @@ -478,6 +492,8 @@ class ServerCloseResponseOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } protected: std::string GetTypeString() const { @@ -517,16 +533,17 @@ void CompleteTag(void *tag, const char *error_message) { callback->Call(1, argv); } bool is_final_op = false; - if (tag_struct->call == NULL) { - return; - } for (vector >::iterator it = tag_struct->ops->begin(); it != tag_struct->ops->end(); ++it) { Op *op_ptr = it->get(); + op_ptr->OnComplete(); if (op_ptr->IsFinalOp()) { is_final_op = true; } } + if (tag_struct->call == NULL) { + return; + } tag_struct->call->CompleteBatch(is_final_op); } diff --git a/src/node/ext/call.h b/src/node/ext/call.h index 4316e9ed88..b38f50060c 100644 --- a/src/node/ext/call.h +++ b/src/node/ext/call.h @@ -106,6 +106,7 @@ class Op { virtual ~Op(); v8::Local GetOpType() const; virtual bool IsFinalOp() = 0; + virtual void OnComplete() = 0; protected: virtual std::string GetTypeString() const = 0; diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index f0920c842a..22c89c5862 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -117,6 +117,8 @@ class NewCallOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } grpc_call *call; grpc_call_details details; @@ -126,6 +128,32 @@ class NewCallOp : public Op { std::string GetTypeString() const { return "new_call"; } }; +class TryShutdownOp: public Op { + public: + TryShutdownOp(Server *server, Local server_value) : server(server) { + server_persist.Reset(server_value); + } + Local GetNodeValue() const { + EscapableHandleScope scope; + return scope.Escape(Nan::New(server_persist)); + } + bool ParseOp(Local value, grpc_op *out) { + return true; + } + bool IsFinalOp() { + return false; + } + void OnComplete() { + server->DestroyWrappedServer(); + } + protected: + std::string GetTypeString() const { return "try_shutdown"; } + private: + Server *server; + Nan::Persistent> + server_persist; +}; + void Server::Init(Local exports) { HandleScope scope; Local tpl = Nan::New(New); @@ -147,6 +175,13 @@ bool Server::HasInstance(Local val) { return Nan::New(fun_tpl)->HasInstance(val); } +void Server::DestroyWrappedServer() { + if (this->wrapped_server != NULL) { + grpc_server_destroy(this->wrapped_server); + this->wrapped_server = NULL; + } +} + NAN_METHOD(Server::New) { /* If this is not a constructor call, make a constructor call and return the result */ @@ -242,7 +277,9 @@ NAN_METHOD(Server::TryShutdown) { return Nan::ThrowTypeError("tryShutdown can only be called on a Server"); } Server *server = ObjectWrap::Unwrap(info.This()); + TryShutdownOp *op = new TryShutdownOp(server, info.This()); unique_ptr ops(new OpVec()); + ops->push_back(unique_ptr(op)); grpc_server_shutdown_and_notify( server->wrapped_server, GetCompletionQueue(), new struct tag(new Nan::Callback(info[0].As()), ops.release(), diff --git a/src/node/ext/server.h b/src/node/ext/server.h index ab5fc210e8..c0f2e86554 100644 --- a/src/node/ext/server.h +++ b/src/node/ext/server.h @@ -53,6 +53,8 @@ class Server : public Nan::ObjectWrap { JavaScript constructor */ static bool HasInstance(v8::Local val); + void DestroyWrappedServer(); + private: explicit Server(grpc_server *server); ~Server(); diff --git a/src/node/ext/server_uv.cc b/src/node/ext/server_uv.cc index 82e7589fc8..bf402e1961 100644 --- a/src/node/ext/server_uv.cc +++ b/src/node/ext/server_uv.cc @@ -76,6 +76,8 @@ class ServerShutdownOp : public Op { bool IsFinalOp() { return false; } + void OnComplete() { + } grpc_server *server; @@ -104,6 +106,7 @@ NAN_METHOD(ServerShutdownCallback) { } void Server::ShutdownServer() { + Nan::HandleScope scope; if (this->wrapped_server != NULL) { if (shutdown_callback == NULL) { Localcallback_tpl = -- cgit v1.2.3