Skip to content

Commit

Permalink
add OnServerShutdown callback
Browse files Browse the repository at this point in the history
  • Loading branch information
flixr committed Dec 1, 2020
1 parent 1ef0e59 commit 3a4a0a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions async_grpc/rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ void Rpc::OnReadsDone() { handler_->OnReadsDone(); }

void Rpc::OnFinish() { handler_->OnFinish(); }

void Rpc::OnServerShutdown() {
if (handler_) {
handler_->OnServerShutdown();
}
}

void Rpc::RequestNextMethodInvocation() {
// Ask gRPC to notify us when the connection terminates.
SetRpcEventState(Event::DONE, true);
Expand Down Expand Up @@ -385,4 +391,11 @@ std::weak_ptr<Rpc> ActiveRpcs::GetWeakPtr(Rpc* rpc) {
return it->second;
}

void ActiveRpcs::Shutdown() {
common::MutexLocker locker(&lock_);
for (auto it : rpcs_) {
it.second->OnServerShutdown();
}
}

} // namespace async_grpc
2 changes: 2 additions & 0 deletions async_grpc/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Rpc {
void OnRequest();
void OnReadsDone();
void OnFinish();
void OnServerShutdown();
void RequestNextMethodInvocation();
void RequestStreamingReadIfNeeded();
void HandleSendQueue();
Expand Down Expand Up @@ -198,6 +199,7 @@ class ActiveRpcs {
std::shared_ptr<Rpc> Add(std::unique_ptr<Rpc> rpc) EXCLUDES(lock_);
bool Remove(Rpc* rpc) EXCLUDES(lock_);
Rpc::WeakPtrFactory GetWeakPtrFactory();
void Shutdown();

private:
std::weak_ptr<Rpc> GetWeakPtr(Rpc* rpc);
Expand Down
1 change: 1 addition & 0 deletions async_grpc/rpc_handler_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RpcHandlerInterface {
const ::google::protobuf::Message* request) = 0;
virtual void OnReadsDone(){};
virtual void OnFinish(){};
virtual void OnServerShutdown(){};
virtual Span* trace_span() = 0;
template <class RpcHandlerType>
static std::unique_ptr<RpcHandlerType> Instantiate() {
Expand Down
5 changes: 4 additions & 1 deletion async_grpc/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ void Service::StartServing(
}
}

void Service::StopServing() { shutting_down_ = true; }
void Service::StopServing() {
shutting_down_ = true;
active_rpcs_.Shutdown();
}

void Service::HandleEvent(Rpc::Event event, Rpc* rpc, bool ok) {
switch (event) {
Expand Down

0 comments on commit 3a4a0a6

Please sign in to comment.