TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_grpc.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-07 23:40:43 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----------------------------------------------------------------------*/
19
30#pragma once
31
32#include "tec/tec_def.hpp" // IWYU pragma: keep
33#include "tec/tec_utils.hpp"
34
35
36namespace tec {
37
38
39/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40*
41* gRPC common parameters
42*
43 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
44
46static constexpr const int kGrpcMaxMessageSize{4};
47
48
49/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50*
51* gRPC Server parameters
52*
53 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
54
57 void (*fptr)(bool);
58};
59
62 void (*fptr)(void);
63};
64
65
76 static constexpr const char kDefaultAddrUri[] = "0.0.0.0:50051";
77
82 static constexpr const MilliSec kStartTimeout{Seconds{5}};
83
88 static constexpr const MilliSec kShutdownTimeout{Seconds{10}};
89
90 std::string addr_uri;
93
94 // ServerBuilder parameters
100
105 , health_check_builder{nullptr}
106 , reflection_builder{nullptr}
107 , max_message_size{kGrpcMaxMessageSize}
110 {}
111};
112
113
114/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115*
116* gRPC Client parameters
117*
118 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
119
130 static constexpr const char kDefaultAddrUri[] = "127.0.0.1:50051";
131
136 static constexpr MilliSec kConnectTimeout{Seconds{5}};
137
142 static constexpr MilliSec kCloseTimeout{Seconds{10}};
143
144 std::string addr_uri;
147
148 // Channel arguments
151
156 , max_message_size{kGrpcMaxMessageSize}
158 {}
159};
160
161
162/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163 *
164 * gRPC metadata on the client side
165 *
166 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
167
172template <typename TClientContext>
173inline std::string get_server_metadata(const TClientContext& ctx, const std::string& key) {
174 auto meta = ctx.GetServerInitialMetadata();
175 auto data = meta.find(key);
176 if( data != meta.end() ) {
177 auto ref = data->second; // grpc::string_ref
178 auto len = ref.length();
179 // NOTE: grpc::string_ref is NOT null-terminated string!
180 if( len > 0 ) {
181 return{ref.data(), 0, len};
182 }
183 }
184 return{};
185}
186
192template <typename TClientContext>
193inline void add_client_metadata(TClientContext& ctx, const std::string& key, const std::string& data) {
194 ctx.AddMetadata(key, data);
195}
196
197
198/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199 *
200 * gRPC metadata on the server side
201 *
202 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
203
208template <typename TServerContext>
209inline std::string get_client_metadata(const TServerContext* pctx, const std::string& key) {
210 auto meta = pctx->client_metadata();
211 auto data = meta.find(key);
212 if( data != meta.end() ) {
213 auto ref = data->second; // grpc::string_ref
214 auto len = ref.length();
215 // NOTE: grpc::string_ref is NOT null-terminated string!
216 if( len > 0 ) {
217 return{ref.data(), 0, len};
218 }
219 }
220 return{};
221}
222
227template <typename TServerContext>
228inline void add_server_medadata(TServerContext* pctx, const std::string& key, const std::string& data) {
229 pctx->AddInitialMetadata(key, data);
230}
231
232
233} // ::tec
Configuration parameters for gRPC client instances.
Definition tec_grpc.hpp:125
int compression_algorithm
GRPC_COMPRESS_NONE = 0, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_ALGORITHMS_COUNT.
Definition tec_grpc.hpp:150
MilliSec connect_timeout
Timeout for client connection in milliseconds.
Definition tec_grpc.hpp:145
MilliSec close_timeout
Timeout for client closing in milliseconds.
Definition tec_grpc.hpp:146
std::string addr_uri
See kDefaultAddrUri.
Definition tec_grpc.hpp:144
static constexpr const char kDefaultAddrUri[]
Default client URI.
Definition tec_grpc.hpp:130
static constexpr MilliSec kConnectTimeout
Default timeout for client connection.
Definition tec_grpc.hpp:136
int max_message_size
See kGrpcMaxMessageSize.
Definition tec_grpc.hpp:149
static constexpr MilliSec kCloseTimeout
Default timeout for client closing.
Definition tec_grpc.hpp:142
Declare the gRPC health check service builder.
Definition tec_grpc.hpp:56
Declare the gRPC reflection service builder.
Definition tec_grpc.hpp:61
Configuration parameters for gRPC server instances.
Definition tec_grpc.hpp:71
static constexpr const char kDefaultAddrUri[]
Default server URI.
Definition tec_grpc.hpp:76
static constexpr const MilliSec kShutdownTimeout
Default timeout for gRPC shutdown.
Definition tec_grpc.hpp:88
int max_message_size
kGrpcMaxMessageSize, set to 0 to use gRPC's default (4Mb).
Definition tec_grpc.hpp:97
MilliSec shutdown_timeout
Timeout for server shutdown in milliseconds.
Definition tec_grpc.hpp:92
std::string addr_uri
See kDefaultAddrUri.
Definition tec_grpc.hpp:90
MilliSec start_timeout
Timeout for server startup in milliseconds.
Definition tec_grpc.hpp:91
int compression_algorithm
GRPC_COMPRESS_NONE = 0, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_ALGORITHMS_COUNT.
Definition tec_grpc.hpp:98
GrpcReflectionBuilder reflection_builder
e.g. {&grpc::reflection::InitProtoReflectionServerBuilderPlugin}.
Definition tec_grpc.hpp:96
int compression_level
GRPC_COMPRESS_LEVEL_NONE = 0, GRPC_COMPRESS_LEVEL_LOW, GRPC_COMPRESS_LEVEL_MED, GRPC_COMPRESS_LEVEL_H...
Definition tec_grpc.hpp:99
static constexpr const MilliSec kStartTimeout
Default timeout for gRPC startup.
Definition tec_grpc.hpp:82
GrpcHealthCheckBuilder health_check_builder
e.g. {&grpc::EnableDefaultHealthCheckService}.
Definition tec_grpc.hpp:95
Common definitions and utilities for the tec namespace.
void add_server_medadata(TServerContext *pctx, const std::string &key, const std::string &data)
Put server's metadata on the server side.
Definition tec_grpc.hpp:228
std::string get_server_metadata(const TClientContext &ctx, const std::string &key)
Get server's metadata on the client side.
Definition tec_grpc.hpp:173
std::string get_client_metadata(const TServerContext *pctx, const std::string &key)
Get client's metadata on the server side.
Definition tec_grpc.hpp:209
void add_client_metadata(TClientContext &ctx, const std::string &key, const std::string &data)
Put client's metadata on the client side.
Definition tec_grpc.hpp:193
Provides time-related utilities and system information functions for the tec namespace.
std::chrono::seconds Seconds
Type alias for seconds duration.
Definition tec_utils.hpp:52
std::chrono::milliseconds MilliSec
Type alias for milliseconds duration.
Definition tec_utils.hpp:62