Skip to content
Snippets Groups Projects
Commit cd35c4c1 authored by murgatroid99's avatar murgatroid99
Browse files

Updated server to use new shutdown semantics

parent 10c763ac
No related branches found
No related tags found
No related merge requests found
...@@ -112,9 +112,17 @@ class NewCallOp : public Op { ...@@ -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) { void Server::Init(Handle<Object> exports) {
NanScope(); NanScope();
...@@ -148,6 +156,16 @@ bool Server::HasInstance(Handle<Value> val) { ...@@ -148,6 +156,16 @@ bool Server::HasInstance(Handle<Value> val) {
return NanHasInstance(fun_tpl, 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) { NAN_METHOD(Server::New) {
NanScope(); NanScope();
...@@ -207,6 +225,9 @@ NAN_METHOD(Server::RequestCall) { ...@@ -207,6 +225,9 @@ NAN_METHOD(Server::RequestCall) {
return NanThrowTypeError("requestCall can only be called on a Server"); return NanThrowTypeError("requestCall can only be called on a Server");
} }
Server *server = ObjectWrap::Unwrap<Server>(args.This()); 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(); NewCallOp *op = new NewCallOp();
unique_ptr<OpVec> ops(new OpVec()); unique_ptr<OpVec> ops(new OpVec());
ops->push_back(unique_ptr<Op>(op)); ops->push_back(unique_ptr<Op>(op));
...@@ -232,6 +253,9 @@ NAN_METHOD(Server::AddHttp2Port) { ...@@ -232,6 +253,9 @@ NAN_METHOD(Server::AddHttp2Port) {
return NanThrowTypeError("addHttp2Port's argument must be a String"); return NanThrowTypeError("addHttp2Port's argument must be a String");
} }
Server *server = ObjectWrap::Unwrap<Server>(args.This()); 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( NanReturnValue(NanNew<Number>(grpc_server_add_http2_port(
server->wrapped_server, *NanUtf8String(args[0])))); server->wrapped_server, *NanUtf8String(args[0]))));
} }
...@@ -251,6 +275,10 @@ NAN_METHOD(Server::AddSecureHttp2Port) { ...@@ -251,6 +275,10 @@ NAN_METHOD(Server::AddSecureHttp2Port) {
"addSecureHttp2Port's second argument must be ServerCredentials"); "addSecureHttp2Port's second argument must be ServerCredentials");
} }
Server *server = ObjectWrap::Unwrap<Server>(args.This()); 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>( ServerCredentials *creds = ObjectWrap::Unwrap<ServerCredentials>(
args[1]->ToObject()); args[1]->ToObject());
NanReturnValue(NanNew<Number>(grpc_server_add_secure_http2_port( NanReturnValue(NanNew<Number>(grpc_server_add_secure_http2_port(
...@@ -264,17 +292,24 @@ NAN_METHOD(Server::Start) { ...@@ -264,17 +292,24 @@ NAN_METHOD(Server::Start) {
return NanThrowTypeError("start can only be called on a Server"); return NanThrowTypeError("start can only be called on a Server");
} }
Server *server = ObjectWrap::Unwrap<Server>(args.This()); 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); grpc_server_start(server->wrapped_server);
NanReturnUndefined(); NanReturnUndefined();
} }
NAN_METHOD(ShutdownCallback) {
NanReturnUndefined();
}
NAN_METHOD(Server::Shutdown) { NAN_METHOD(Server::Shutdown) {
NanScope(); NanScope();
if (!HasInstance(args.This())) { if (!HasInstance(args.This())) {
return NanThrowTypeError("shutdown can only be called on a Server"); return NanThrowTypeError("shutdown can only be called on a Server");
} }
Server *server = ObjectWrap::Unwrap<Server>(args.This()); Server *server = ObjectWrap::Unwrap<Server>(args.This());
grpc_server_shutdown(server->wrapped_server); server->ShutdownServer();
NanReturnUndefined(); NanReturnUndefined();
} }
......
...@@ -61,6 +61,8 @@ class Server : public ::node::ObjectWrap { ...@@ -61,6 +61,8 @@ class Server : public ::node::ObjectWrap {
Server(const Server &); Server(const Server &);
Server &operator=(const Server &); Server &operator=(const Server &);
void ShutdownServer();
static NAN_METHOD(New); static NAN_METHOD(New);
static NAN_METHOD(RequestCall); static NAN_METHOD(RequestCall);
static NAN_METHOD(AddHttp2Port); static NAN_METHOD(AddHttp2Port);
...@@ -71,6 +73,7 @@ class Server : public ::node::ObjectWrap { ...@@ -71,6 +73,7 @@ class Server : public ::node::ObjectWrap {
static v8::Persistent<v8::FunctionTemplate> fun_tpl; static v8::Persistent<v8::FunctionTemplate> fun_tpl;
grpc_server *wrapped_server; grpc_server *wrapped_server;
grpc_completion_queue *shutdown_queue;
}; };
} // namespace node } // namespace node
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment