Skip to content

Commit

Permalink
Fix flip grpc server thread terminate. (eBay#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanebay authored Jan 18, 2024
1 parent 00155a8 commit 24e1580
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class SISLConan(ConanFile):
name = "sisl"
version = "11.0.5"
version = "11.0.6"

homepage = "https://github.com/eBay/sisl"
description = "Library for fast data structures, utilities"
Expand Down
26 changes: 22 additions & 4 deletions include/sisl/flip/flip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#include <cstdlib>
#include <string>
#include <regex>
#include <grpc/grpc.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <grpcpp/security/server_credentials.h>

#include "proto/flip_spec.pb.h"
#include "flip_rpc_server.hpp"
Expand Down Expand Up @@ -431,21 +436,33 @@ static constexpr int DELAYED_RETURN = 3;
class Flip {
public:
Flip() : m_flip_enabled(false) {}
~Flip() {
if (m_flip_server) { stop_rpc_server(); }
}

static Flip& instance() {
static Flip s_instance;
return s_instance;
}

void start_rpc_server() {
if (m_flip_server) { stop_rpc_server(); }

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();
std::string server_address("0.0.0.0:50051");
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService((FlipRPCServer::Service*)m_flip_server.get());
m_grpc_server = builder.BuildAndStart();
LOGINFOMOD(flip, "Flip GRPC Server listening on {}", server_address);
m_flip_server_thread = std::unique_ptr< std::thread >(
new std::thread([grpc_server = m_grpc_server.get()]() { grpc_server->Wait(); }));
}

void stop_rpc_server() {
m_flip_server->shutdown();
if (m_grpc_server) { m_grpc_server->Shutdown(); }
m_flip_server_thread->join();
m_flip_server_thread.reset();
m_flip_server.reset();
}

Expand Down Expand Up @@ -675,6 +692,7 @@ class Flip {
std::unique_ptr< FlipTimerBase > m_timer;
std::unique_ptr< std::thread > m_flip_server_thread;
std::unique_ptr< FlipRPCServer > m_flip_server;
std::unique_ptr< grpc::Server > m_grpc_server;
};

} // namespace flip
Expand Down
7 changes: 0 additions & 7 deletions include/sisl/flip/flip_rpc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,5 @@ class FlipRPCServer final : public FlipServer::Service {
FlipListResponse* response) override;
grpc::Status RemoveFault(grpc::ServerContext*, const FlipRemoveRequest* request,
FlipRemoveResponse* response) override;
void rpc_thread();
void shutdown() {
if (m_server) { m_server->Shutdown(); }
}

private:
std::unique_ptr< grpc::Server > m_server;
};
} // namespace flip
12 changes: 0 additions & 12 deletions src/flip/lib/flip_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,4 @@ class FlipRPCServiceWrapper : public FlipRPCServer::Service {
}
};

void FlipRPCServer::rpc_thread() {
std::string server_address("0.0.0.0:50051");
FlipRPCServer service;

grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService((FlipRPCServer::Service*)&service);
m_server = builder.BuildAndStart();
LOGINFOMOD(flip, "Flip GRPC Server listening on {}", server_address);
m_server->Wait();
}

} // namespace flip

0 comments on commit 24e1580

Please sign in to comment.