[cdc_stream] Implement stop-service command (#29)

Implements cdc_stream stop-service. Also fixes an issue in the
BackgroundService implementation where Exit() would deadlock since
server shutdown waits for all RPCs to exit.
This commit is contained in:
Lutz Justen
2022-12-02 19:39:13 +01:00
committed by GitHub
parent 1120dcbee0
commit 90717ce670
8 changed files with 149 additions and 4 deletions

View File

@@ -23,7 +23,12 @@ namespace cdc_ft {
BackgroundServiceImpl::BackgroundServiceImpl() {}
BackgroundServiceImpl::~BackgroundServiceImpl() = default;
BackgroundServiceImpl::~BackgroundServiceImpl() {
if (exit_thread_) {
exit_thread_->join();
exit_thread_.reset();
}
}
void BackgroundServiceImpl::SetExitCallback(ExitCallback exit_callback) {
exit_callback_ = std::move(exit_callback);
@@ -33,8 +38,11 @@ grpc::Status BackgroundServiceImpl::Exit(grpc::ServerContext* context,
const EmptyProto* request,
EmptyProto* response) {
LOG_INFO("RPC:Exit");
if (exit_callback_) {
return ToGrpcStatus(exit_callback_());
if (exit_callback_ && !exit_thread_) {
// Fire up a thread so call the callback, since shutting down a server
// won't finish until all RPCs are done.
exit_thread_ =
std::make_unique<std::thread>([cb = &exit_callback_]() { (*cb)(); });
}
return grpc::Status::OK;
}