aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node
diff options
context:
space:
mode:
authorGravatar murgatroid99 <mlumish@google.com>2017-04-10 15:43:09 -0700
committerGravatar murgatroid99 <mlumish@google.com>2017-04-10 15:43:09 -0700
commit42cfaa99bd3490bb0849fd66aa56016e763a56a8 (patch)
tree6882a327568a1d3b97c419f5f421c252d313eedb /src/node
parent79962f33c71e248276486122097e33b038e537df (diff)
Add native tag completion callbacks, dispose of server after tryShutdown succeeds
Diffstat (limited to 'src/node')
-rw-r--r--src/node/ext/call.cc23
-rw-r--r--src/node/ext/call.h1
-rw-r--r--src/node/ext/server.cc37
-rw-r--r--src/node/ext/server.h2
-rw-r--r--src/node/ext/server_uv.cc3
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<unique_ptr<Op> >::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<v8::Value> 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<Value> server_value) : server(server) {
+ server_persist.Reset(server_value);
+ }
+ Local<Value> GetNodeValue() const {
+ EscapableHandleScope scope;
+ return scope.Escape(Nan::New(server_persist));
+ }
+ bool ParseOp(Local<Value> 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<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
+ server_persist;
+};
+
void Server::Init(Local<Object> exports) {
HandleScope scope;
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -147,6 +175,13 @@ bool Server::HasInstance(Local<Value> 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<Server>(info.This());
+ TryShutdownOp *op = new TryShutdownOp(server, info.This());
unique_ptr<OpVec> ops(new OpVec());
+ ops->push_back(unique_ptr<Op>(op));
grpc_server_shutdown_and_notify(
server->wrapped_server, GetCompletionQueue(),
new struct tag(new Nan::Callback(info[0].As<Function>()), 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<v8::Value> 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) {
Local<FunctionTemplate>callback_tpl =