1212namespace ray {
1313namespace rpc {
1414
15- // / Base class that represents an abstract gRPC server.
15+ class GrpcService ;
16+
17+ // / Class that represents an gRPC server.
1618// /
1719// / A `GrpcServer` listens on a specific port. It owns
1820// / 1) a `ServerCompletionQueue` that is used for polling events from gRPC,
@@ -28,11 +30,7 @@ class GrpcServer {
2830 // / \param[in] name Name of this server, used for logging and debugging purpose.
2931 // / \param[in] port The port to bind this server to. If it's 0, a random available port
3032 // / will be chosen.
31- // / \param[in] main_service The main event loop, to which service handler functions
32- // / will be posted.
33- GrpcServer (const std::string &name, const uint32_t port,
34- boost::asio::io_service &main_service)
35- : name_(name), port_(port), main_service_(main_service) {}
33+ GrpcServer (const std::string &name, const uint32_t port) : name_(name), port_(port) {}
3634
3735 // / Destruct this gRPC server.
3836 ~GrpcServer () {
@@ -46,36 +44,25 @@ class GrpcServer {
4644 // / Get the port of this gRPC server.
4745 int GetPort () const { return port_; }
4846
49- protected:
50- // / Subclasses should implement this method and register one or multiple gRPC services
51- // / to the given `ServerBuilder `.
47+ // / Register a grpc service. Multiple services can be registered to the same server.
48+ // / Note that the `service` registered must remain valid for the lifetime of the
49+ // / `GrpcServer`, as it holds the underlying `grpc::Service `.
5250 // /
53- // / \param[in] builder The `ServerBuilder` instance to register services to.
54- virtual void RegisterServices (grpc::ServerBuilder &builder) = 0;
55-
56- // / Subclasses should implement this method to initialize the `ServerCallFactory`
57- // / instances, as well as specify maximum number of concurrent requests that gRPC
58- // / server can "accept" (not "handle"). Each factory will be used to create
59- // / `accept_concurrency` `ServerCall` objects, each of which will be used to accept and
60- // / handle an incoming request.
61- // /
62- // / \param[out] server_call_factories_and_concurrencies The `ServerCallFactory` objects,
63- // / and the maximum number of concurrent requests that gRPC server can accept.
64- virtual void InitServerCallFactories (
65- std::vector<std::pair<std::unique_ptr<ServerCallFactory>, int >>
66- *server_call_factories_and_concurrencies) = 0;
51+ // / \param[in] service A `GrpcService` to register to this server.
52+ void RegisterService (GrpcService &service);
6753
54+ protected:
6855 // / This function runs in a background thread. It keeps polling events from the
6956 // / `ServerCompletionQueue`, and dispaches the event to the `ServiceHandler` instances
7057 // / via the `ServerCall` objects.
7158 void PollEventsFromCompletionQueue ();
7259
73- // / The main event loop, to which the service handler functions will be posted.
74- boost::asio::io_service &main_service_;
7560 // / Name of this server, used for logging and debugging purpose.
7661 const std::string name_;
7762 // / Port of this server.
7863 int port_;
64+ // / The `grpc::Service` objects which should be registered to `ServerBuilder`.
65+ std::vector<std::reference_wrapper<grpc::Service>> services_;
7966 // / The `ServerCallFactory` objects, and the maximum number of concurrent requests that
8067 // / gRPC server can accept.
8168 std::vector<std::pair<std::unique_ptr<ServerCallFactory>, int >>
@@ -86,6 +73,46 @@ class GrpcServer {
8673 std::unique_ptr<grpc::Server> server_;
8774};
8875
76+ // / Base class that represents an abstract gRPC service.
77+ // /
78+ // / Subclass should implement `InitServerCallFactories` to decide
79+ // / which kinds of requests this service should accept.
80+ class GrpcService {
81+ public:
82+ // / Constructor.
83+ // /
84+ // / \param[in] main_service The main event loop, to which service handler functions
85+ // / will be posted.
86+ GrpcService (boost::asio::io_service &main_service) : main_service_(main_service) {}
87+
88+ // / Destruct this gRPC service.
89+ ~GrpcService () {}
90+
91+ protected:
92+ // / Return the underlying grpc::Service object for this class.
93+ // / This is passed to `GrpcServer` to be registered to grpc `ServerBuilder`.
94+ virtual grpc::Service &GetGrpcService () = 0;
95+
96+ // / Subclasses should implement this method to initialize the `ServerCallFactory`
97+ // / instances, as well as specify maximum number of concurrent requests that gRPC
98+ // / server can "accept" (not "handle"). Each factory will be used to create
99+ // / `accept_concurrency` `ServerCall` objects, each of which will be used to accept and
100+ // / handle an incoming request.
101+ // /
102+ // / \param[in] cq The grpc completion queue.
103+ // / \param[out] server_call_factories_and_concurrencies The `ServerCallFactory` objects,
104+ // / and the maximum number of concurrent requests that gRPC server can accept.
105+ virtual void InitServerCallFactories (
106+ const std::unique_ptr<grpc::ServerCompletionQueue> &cq,
107+ std::vector<std::pair<std::unique_ptr<ServerCallFactory>, int >>
108+ *server_call_factories_and_concurrencies) = 0;
109+
110+ // / The main event loop, to which the service handler functions will be posted.
111+ boost::asio::io_service &main_service_;
112+
113+ friend class GrpcServer ;
114+ };
115+
89116} // namespace rpc
90117} // namespace ray
91118
0 commit comments