Skip to content

Commit

Permalink
Add shutdown api for flip grpc server. (eBay#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanebay authored Jan 16, 2024
1 parent a1aa7fb commit 4015bb0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion include/sisl/flip/flip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,17 @@ class Flip {
}

void start_rpc_server() {
m_flip_server_thread = std::unique_ptr< std::thread >(new std::thread(FlipRPCServer::rpc_thread));
m_flip_server = std::make_unique< FlipRPCServer >();
m_flip_server_thread =
std::unique_ptr< std::thread >(new std::thread([this]() { m_flip_server->rpc_thread(); }));
m_flip_server_thread->detach();
}

void stop_rpc_server() {
m_flip_server->shutdown();
m_flip_server.reset();
}

bool add(const FlipSpec& fspec) {
m_flip_enabled = true;
auto inst = flip_instance(fspec);
Expand Down Expand Up @@ -667,6 +674,7 @@ class Flip {
bool m_flip_enabled;
std::unique_ptr< FlipTimerBase > m_timer;
std::unique_ptr< std::thread > m_flip_server_thread;
std::unique_ptr< FlipRPCServer > m_flip_server;
};

} // namespace flip
Expand Down
10 changes: 9 additions & 1 deletion include/sisl/flip/flip_rpc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@
*
*********************************************************************************/
#pragma once
#include <grpcpp/grpcpp.h>

#include "proto/flip_spec.pb.h"
#include "proto/flip_server.grpc.pb.h"

namespace flip {
class FlipRPCServer final : public FlipServer::Service {
public:
FlipRPCServer() = default;
grpc::Status InjectFault(grpc::ServerContext* context, const FlipSpec* request, FlipResponse* response) override;
grpc::Status GetFaults(grpc::ServerContext* context, const FlipNameRequest* request,
FlipListResponse* response) override;
grpc::Status RemoveFault(grpc::ServerContext*, const FlipRemoveRequest* request,
FlipRemoveResponse* response) override;
static void rpc_thread();
void rpc_thread();
void shutdown() {
if (m_server) { m_server->Shutdown(); }
}

private:
std::unique_ptr< grpc::Server > m_server;
};
} // namespace flip
4 changes: 2 additions & 2 deletions src/flip/lib/flip_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ void FlipRPCServer::rpc_thread() {
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService((FlipRPCServer::Service*)&service);
std::unique_ptr< grpc::Server > server(builder.BuildAndStart());
m_server = builder.BuildAndStart();
LOGINFOMOD(flip, "Flip GRPC Server listening on {}", server_address);
server->Wait();
m_server->Wait();
}

} // namespace flip

0 comments on commit 4015bb0

Please sign in to comment.