diff options
author | murgatroid99 <mlumish@google.com> | 2015-05-28 13:39:25 -0700 |
---|---|---|
committer | murgatroid99 <mlumish@google.com> | 2015-05-28 13:39:25 -0700 |
commit | cd35c4c15976b36bf97c3a5875e7a53ec168d733 (patch) | |
tree | 0f8a92519c484a5840693e5605206a20c93d426d /src/node | |
parent | 10c763ac6271b98f48af5743d826cd08f30308bc (diff) |
Updated server to use new shutdown semantics
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/ext/server.cc | 41 | ||||
-rw-r--r-- | src/node/ext/server.h | 3 |
2 files changed, 41 insertions, 3 deletions
diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index eb97f7348b..51c55ba965 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -112,9 +112,17 @@ class NewCallOp : public Op { } }; -Server::Server(grpc_server *server) : wrapped_server(server) {} +Server::Server(grpc_server *server) : wrapped_server(server) { + shutdown_queue = grpc_completion_queue_create(); + grpc_server_register_completion_queue(server, shutdown_queue); +} -Server::~Server() { grpc_server_destroy(wrapped_server); } +Server::~Server() { + this->ShutdownServer(); + grpc_completion_queue_shutdown(this->shutdown_queue); + grpc_server_destroy(wrapped_server); + grpc_completion_queue_destroy(this->shutdown_queue); +} void Server::Init(Handle<Object> exports) { NanScope(); @@ -148,6 +156,16 @@ bool Server::HasInstance(Handle<Value> val) { return NanHasInstance(fun_tpl, val); } +void Server::ShutdownServer() { + if (this->wrapped_server != NULL) { + grpc_server_shutdown_and_notify(this->wrapped_server, + this->shutdown_queue, + NULL); + grpc_completion_queue_pluck(this->shutdown_queue, NULL, gpr_inf_future); + this->wrapped_server = NULL; + } +} + NAN_METHOD(Server::New) { NanScope(); @@ -207,6 +225,9 @@ NAN_METHOD(Server::RequestCall) { return NanThrowTypeError("requestCall can only be called on a Server"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); + if (server->wrapped_server == NULL) { + return NanThrowError("requestCall cannot be called on a shut down Server"); + } NewCallOp *op = new NewCallOp(); unique_ptr<OpVec> ops(new OpVec()); ops->push_back(unique_ptr<Op>(op)); @@ -232,6 +253,9 @@ NAN_METHOD(Server::AddHttp2Port) { return NanThrowTypeError("addHttp2Port's argument must be a String"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); + if (server->wrapped_server == NULL) { + return NanThrowError("addHttp2Port cannot be called on a shut down Server"); + } NanReturnValue(NanNew<Number>(grpc_server_add_http2_port( server->wrapped_server, *NanUtf8String(args[0])))); } @@ -251,6 +275,10 @@ NAN_METHOD(Server::AddSecureHttp2Port) { "addSecureHttp2Port's second argument must be ServerCredentials"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); + if (server->wrapped_server == NULL) { + return NanThrowError( + "addSecureHttp2Port cannot be called on a shut down Server"); + } ServerCredentials *creds = ObjectWrap::Unwrap<ServerCredentials>( args[1]->ToObject()); NanReturnValue(NanNew<Number>(grpc_server_add_secure_http2_port( @@ -264,17 +292,24 @@ NAN_METHOD(Server::Start) { return NanThrowTypeError("start can only be called on a Server"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); + if (server->wrapped_server == NULL) { + return NanThrowError("start cannot be called on a shut down Server"); + } grpc_server_start(server->wrapped_server); NanReturnUndefined(); } +NAN_METHOD(ShutdownCallback) { + NanReturnUndefined(); +} + NAN_METHOD(Server::Shutdown) { NanScope(); if (!HasInstance(args.This())) { return NanThrowTypeError("shutdown can only be called on a Server"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); - grpc_server_shutdown(server->wrapped_server); + server->ShutdownServer(); NanReturnUndefined(); } diff --git a/src/node/ext/server.h b/src/node/ext/server.h index 641d5ccb3e..5b4b18a0e0 100644 --- a/src/node/ext/server.h +++ b/src/node/ext/server.h @@ -61,6 +61,8 @@ class Server : public ::node::ObjectWrap { Server(const Server &); Server &operator=(const Server &); + void ShutdownServer(); + static NAN_METHOD(New); static NAN_METHOD(RequestCall); static NAN_METHOD(AddHttp2Port); @@ -71,6 +73,7 @@ class Server : public ::node::ObjectWrap { static v8::Persistent<v8::FunctionTemplate> fun_tpl; grpc_server *wrapped_server; + grpc_completion_queue *shutdown_queue; }; } // namespace node |