TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_grpc_server.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:19:27 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
32#pragma once
33
34#include "tec/tec_def.hpp" // IWYU pragma: keep
35#include "tec/tec_signal.hpp"
36#include "tec/tec_utils.hpp" // IWYU pragma: keep
37#include "tec/tec_trace.hpp"
38#include "tec/tec_actor.hpp" // IWYU pragma: keep
39#include "tec/grpc/tec_grpc.hpp" // IWYU pragma: keep
40
41
42namespace tec {
43
44/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45*
46* gRPC Server traits
47*
48*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
49
65template <
66 typename TService,
67 typename TGrpcServer,
68 typename TGrpcServerBuilder,
69 typename TGrpcServerCredentials,
70 typename TGrpcCompressionAlgorithm,
71 typename TGrpcCompressionLevel
72 >
74 typedef TService Service;
75 typedef TGrpcServer RpcServer;
76 typedef TGrpcServerBuilder Builder;
77 typedef TGrpcServerCredentials Credentials;
78 typedef TGrpcCompressionAlgorithm CompressionAlgorithm;
79 typedef TGrpcCompressionLevel CompressionLevel;
80};
81
82/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83*
84* Generic gRPC Server
85*
86*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
87
105template <typename TParams, typename Traits>
106class GrpcServer : public Actor {
107public:
108 typedef TParams Params;
109 typedef typename Traits::Service Service;
110 typedef typename Traits::RpcServer RpcServer;
111 typedef typename Traits::Builder Builder;
112 typedef typename Traits::Credentials Credentials;
113 typedef typename Traits::CompressionAlgorithm CompressionAlgorithm;
114 typedef typename Traits::CompressionLevel CompressionLevel;
115
116protected:
117 // ── Configuration & state ───────────────────────────────────────────────
118
121
123 std::unique_ptr<RpcServer> server_;
124
126 std::shared_ptr<Credentials> credentials_;
127
128protected:
139 virtual void set_plugins() {
140 TEC_ENTER("GrpcServer::set_plugins");
141
142 if (params_.health_check_builder.fptr) {
143 params_.health_check_builder.fptr(true);
144 TEC_TRACE("Health checking enabled.");
145 }
146 if (params_.reflection_builder.fptr) {
147 params_.reflection_builder.fptr();
148 TEC_TRACE("Reflection enabled.");
149 }
150 }
151
164 virtual void set_builder_options(Builder& builder) {
165 TEC_ENTER("GrpcServer::set_builder_options");
166
167 // Set max message size (both send and receive)
168 if (params_.max_message_size > 0) {
169 const int max_size = params_.max_message_size * 1024 * 1024;
170 builder.SetMaxReceiveMessageSize(max_size);
171 builder.SetMaxSendMessageSize(max_size);
172 }
173 TEC_TRACE("MaxMessageSize is set to {} Mb.", params_.max_message_size);
174
175 // Set default compression algorithm
176 // Note: this overrides any compression level set by SetDefaultCompressionLevel
177 if (params_.compression_algorithm > 0) {
178 builder.SetDefaultCompressionAlgorithm(static_cast<CompressionAlgorithm>(params_.compression_algorithm));
179 }
180 TEC_TRACE("CompressionAlgorithm is set to {}.", params_.compression_algorithm);
181
182 // Set default compression level
183 if (params_.compression_level > 0) {
184 builder.SetDefaultCompressionLevel(static_cast<CompressionLevel>(params_.compression_level));
185 }
186 TEC_TRACE("CompressionLevel is set to {}.", params_.compression_level);
187 }
188
189public:
196 GrpcServer(const Params& params, const std::shared_ptr<Credentials>& credentials)
197 : Actor()
198 , params_{params}
199 , credentials_{credentials}
200 {
201 static_assert(
202 std::is_base_of<GrpcServerParams, Params>::value,
203 "Must derive from tec::GrpcServerParams class");
204 }
205
207 virtual ~GrpcServer() = default;
208
222
237 void start(Signal* sig_started, Status* status) override {
238 TEC_ENTER("GrpcServer::start");
239
240 // Create service instance (usually synchronous)
241 Service service;
242
243 // Enable optional plugins
244 set_plugins();
245
246 Builder builder;
247
248 // Bind to address with authentication
249 builder.AddListeningPort(params_.addr_uri, credentials_);
250
251 // Apply message size / compression settings
252 set_builder_options(builder);
253
254 // Register service implementation
255 builder.RegisterService(&service);
256
257 // Start the server
258 TEC_TRACE("starting gRPC server on {} ...", params_.addr_uri);
259 server_ = builder.BuildAndStart();
260 if (!server_) {
261 auto errmsg = format("gRPC Server cannot start on \"{}\"", params_.addr_uri);
262 TEC_TRACE("!!! Error: {}.", errmsg);
263 *status = {errmsg, Error::Kind::NetErr};
264 sig_started->set();
265 return;
266 }
267 TEC_TRACE("server listening on \"{}\".", params_.addr_uri);
268
269 // Notify caller that server is up
270 sig_started->set();
271
272 // Block until shutdown is requested from another thread
273 server_->Wait();
274 }
275
284 void shutdown(Signal* sig_stopped) override {
285 TEC_ENTER("GrpcServer::shutdown");
286 Signal::OnExit on_exit(sig_stopped);
287 if (server_) {
288 TEC_TRACE("terminating gRPC server ...");
289 server_->Shutdown();
290 }
291 }
292
293}; // GrpcServer
294
295} // ::tec
Abstract base class defining the actor lifecycle and request handling interface.
Definition tec_actor.hpp:63
Base class for actor-style gRPC servers.
Definition tec_grpc_server.hpp:106
void start(Signal *sig_started, Status *status) override
Starts the gRPC server and begins listening.
Definition tec_grpc_server.hpp:237
GrpcServer(const Params &params, const std::shared_ptr< Credentials > &credentials)
Constructs a GrpcServer instance.
Definition tec_grpc_server.hpp:196
virtual void set_builder_options(Builder &builder)
Configures server-wide options on the ServerBuilder.
Definition tec_grpc_server.hpp:164
Params params_
Runtime configuration parameters (listen address, message size, compression, plugins…)
Definition tec_grpc_server.hpp:120
virtual ~GrpcServer()=default
Virtual destructor (defaulted)
TParams Params
Configuration parameters type.
Definition tec_grpc_server.hpp:108
Traits::CompressionAlgorithm CompressionAlgorithm
compression algorithm enum
Definition tec_grpc_server.hpp:113
virtual void set_plugins()
Configures optional gRPC server plugins (reflection, health checking).
Definition tec_grpc_server.hpp:139
Traits::RpcServer RpcServer
running server instance type
Definition tec_grpc_server.hpp:110
void shutdown(Signal *sig_stopped) override
Initiates graceful shutdown of the server.
Definition tec_grpc_server.hpp:284
Traits::Credentials Credentials
server credentials type
Definition tec_grpc_server.hpp:112
std::shared_ptr< Credentials > credentials_
Server-side credentials (insecure / SSL / custom)
Definition tec_grpc_server.hpp:126
Status process_request(Request, Reply) override
Synchronous request processor (optional override).
Definition tec_grpc_server.hpp:219
Traits::Builder Builder
server builder type
Definition tec_grpc_server.hpp:111
std::unique_ptr< RpcServer > server_
Owned server instance — created and started during start()
Definition tec_grpc_server.hpp:123
Traits::Service Service
gRPC service type
Definition tec_grpc_server.hpp:109
Traits::CompressionLevel CompressionLevel
compression level enum
Definition tec_grpc_server.hpp:114
A thread-safe signal mechanism for inter-thread synchronization.
Definition tec_signal.hpp:44
void set()
Sets the signal to the signaled state and notifies all waiting threads.
Definition tec_signal.hpp:72
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
@ NetErr
Network-related error.
@ NotImplemented
Not implemented.
Helper struct to signal termination on exit.
Definition tec_signal.hpp:110
Type traits / type aliases collection for configuring a gRPC server.
Definition tec_grpc_server.hpp:73
TGrpcServerBuilder Builder
server builder type
Definition tec_grpc_server.hpp:76
TGrpcServerCredentials Credentials
server-side credentials type
Definition tec_grpc_server.hpp:77
TGrpcCompressionLevel CompressionLevel
compression level enum
Definition tec_grpc_server.hpp:79
TService Service
gRPC service implementation type
Definition tec_grpc_server.hpp:74
TGrpcServer RpcServer
running gRPC server instance type
Definition tec_grpc_server.hpp:75
TGrpcCompressionAlgorithm CompressionAlgorithm
supported compression algorithms enum
Definition tec_grpc_server.hpp:78
Core interface for TEC actors with lifecycle management and request processing.
Common definitions and utilities for the tec namespace.
Generic gRPC parameters.
std::any Reply
Type alias for a reply object that can hold any object.
Definition tec_message.hpp:55
std::any Request
Type alias for a request object that can hold any object.
Definition tec_message.hpp:49
std::string format(const T &arg)
Formats a single argument into a string.
Definition tec_print.hpp:171
Defines a thread-safe signal implementation using mutex and condition variable.
Provides a thread-safe tracing utility for debugging in the tec namespace.
Provides time-related utilities and system information functions for the tec namespace.