Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server/service shutdown on remote client command #11

Open
gonsalez-ru opened this issue Oct 21, 2021 · 8 comments
Open

Server/service shutdown on remote client command #11

gonsalez-ru opened this issue Oct 21, 2021 · 8 comments

Comments

@gonsalez-ru
Copy link

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

@jinq0123
Copy link
Owner

What do you mean?

@gonsalez-ru
Copy link
Author

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.

@jinq0123
Copy link
Owner

jinq0123 commented Oct 21, 2021

function M.SayHello(request, replier)
    os.exit()
end -- SayHello()

Then the service will shutdown itself if the client requests rpc "SayHello".

@gonsalez-ru
Copy link
Author

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()".

@jinq0123
Copy link
Owner

Sorry, no such function. Maybe you can try to export shutdown() from C++ to lua.

@gonsalez-ru
Copy link
Author

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")
.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
void Shutdown(const T& deadline) {
ShutdownInternal(TimePoint(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!

@jinq0123
Copy link
Owner

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

@gonsalez-ru
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants