grpc-lua
grpc-lua copied to clipboard
Server/service shutdown on remote client command
I have no idea how to implement the Subj. Is there any way to call service own methods over RPC?
greeter_service.lua --- Hello world greeter example server side service. -- greeter_service.lua
local M = {}
local grpc = require("grpc_lua.grpc_lua") grpc.import_proto_file("helloworld.proto")
--- Public functions. -- @section public
function M.SayHello(request, replier) assert("table" == type(request)) assert("table" == type(replier)) print("Got hello from "..request.name) -- replier:reply() can be called later after return. local response = { message = "Hello "..request.name } replier:reply(response); end -- SayHello()
function M.shutdown() --self:shutdown() !!! Something like this end
return M
What do you mean?
Service in your "Greeter" example will work until i kill this process with operating system command. I want to find the way to service shutdown by himself according to remote client command.
function M.SayHello(request, replier)
os.exit()
end -- SayHello()
Then the service will shutdown itself if the client requests rpc "SayHello".
Yes, os.exit() works, but I exected another solution. :-( I need the service gracefully finishes all started negotiations and passes control back to the main() routine next to the "svr:run()".
Sorry, no such function. Maybe you can try to export shutdown() from C++ to lua.
I've bind Shutdown function from <grpc_cb_core/server/server.h>. When I call it the service stop serving but dont return control. According to function desctiption there is an optional parameter Deadline wich can help us. Lua and C aren't my native languages, so I need your help in binding with parameters.
grpc-lua\src\cpp\server\BindServer.cpp ... void BindServer(const LuaRef& mod) { using namespace grpc_cb_core; LuaBinding(mod).beginClass<Server>("Server") .addConstructor(LUA_ARGS()) // Returns bound port number on success, 0 on failure. .addFunction("add_listening_port", static_cast<int(Server::*)(const std::string&)>( &Server::AddListeningPort)) .addFunction("register_service", &RegisterService) .addFunction("run", &Server::Run) .addFunction("shutdown", &Server::Shutdown) .endClass(); // Server } // BindServer() ...
grpc_lua\server\Server.lua ... function Server:shutdown() self.c_svr:shutdown() end ...
grpc_cb_core\include\grpc_cb_core\server\server.h ... /// Shutdown the server, blocking until all rpc processing finishes. /// Forcefully terminate pending calls after \a deadline expires. /// /// \param deadline How long to wait until pending rpcs are forcefully /// terminated. template <class T> void Shutdown(const T& deadline) { ShutdownInternal(TimePoint<T>(deadline).raw_time()); }
/// Shutdown the server, waiting for all rpc processing to finish. void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); }
/// Block waiting for all work to complete. /// /// \warning The server must be either shutting down or some other thread must /// call \a Shutdown for this function to ever return. void Run(); ...
How can I describe Shutdown(deadline) like this: .addFunction("add_listening_port", static_cast<int(Server::*)(const std::string&)>( &Server::AddListeningPort)) ?
Thank you in advance!
I hope this can compile:
#include <chrono>
...
namespace {
...
void ShutdownDeadline(grpc_cb_core::Server* pServer, const LuaRef& timeoutSec)
{
assert(pServer);
std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
double dSec = timeoutSec.toValue<double>();
t += std::chrono::seconds(dSec);
pServer->Shutdown(t);
}
} // namespace
namespace server {
void BindServer(const LuaRef& mod)
{
...
.addFunction("shutdown_deadline", &ShutdownDeadline)
...
} // BindServer()
} // namespace server
FAIL :-(
Shutdown with deadline gives the same result. Service stops without termination and loads processor up to 30%.
I'm stumped, so I just live compilable version here ...
grpc-lua\src\cpp\server\BindServer.cpp
#include <grpc/support/time.h> // for gpr_timespec
#include <grpc/impl/codegen/gpr_types.h> //for gpr_clock_type
...
namespace {
...
void ShutdownDeadline(grpc_cb_core::Server* pServer, const LuaRef& timeoutSec)
{
assert(pServer);
int32_t mSec = timeoutSec.toValue<int32_t>()*1000;
gpr_timespec t = gpr_time_from_millis(gpr_time_to_millis(gpr_now(GPR_CLOCK_MONOTONIC))+mSec,GPR_CLOCK_MONOTONIC);
pServer->Shutdown(t);
} //ShutdownDeadline()
} // namespace
namespace server {
void BindServer(const LuaRef& mod)
{
...
.addFunction("shutdown_deadline", &ShutdownDeadline)
...
} // BindServer()
} // namespace server