OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
GrpcServiceConfig.cpp
Go to the documentation of this file.
1#include "GrpcServiceConfig.h"
2#include <algorithm>
3#include <cstdint>
4#include <fstream>
5#include <iostream>
6#include <limits>
7#include <sstream>
8#include <crow/json.h>
9#include <filesystem>
10
12{
13 namespace Config
14 {
15 namespace
16 {
17 // Map a config string to a gRPC compression level;
18 // unknown values fall back to high (matches previous gzip behavior)
19 grpc_compression_level ParseCompressionLevel(const std::string& value)
20 {
21 if (value == "none") return GRPC_COMPRESS_LEVEL_NONE;
22 if (value == "low") return GRPC_COMPRESS_LEVEL_LOW;
23 if (value == "medium") return GRPC_COMPRESS_LEVEL_MED;
24 if (value == "high") return GRPC_COMPRESS_LEVEL_HIGH;
25 std::cerr << "WARNING: unknown grpc compression level \"" << value
26 << "\", defaulting to \"high\"" << std::endl;
27 return GRPC_COMPRESS_LEVEL_HIGH;
28 }
29 }
30
32 {
33 LoadResult result;
34 result.config = std::make_shared<GrpcServiceConfig>();
35
36 // Check if file exists
37 if (!std::filesystem::exists(configPath))
38 {
39 result.loadedFromFile = false;
40 result.message = "Using default gRPC config (" + configPath + " not found)";
41 return result;
42 }
43
44 try
45 {
46 // Read file contents
47 std::ifstream file(configPath);
48 if (!file.is_open())
49 {
50 result.loadedFromFile = false;
51 result.message = "Using default gRPC config (could not open " + configPath + ")";
52 return result;
53 }
54
55 std::stringstream buffer;
56 buffer << file.rdbuf();
57 file.close();
58
59 std::string jsonContent = buffer.str();
60 if (jsonContent.empty())
61 {
62 result.loadedFromFile = false;
63 result.message = "Using default gRPC config (" + configPath + " is empty)";
64 return result;
65 }
66
67 // Parse JSON using Crow
68 auto json = crow::json::load(jsonContent);
69 if (!json)
70 {
71 result.loadedFromFile = false;
72 result.message = "Using default gRPC config (failed to parse JSON in " + configPath + ")";
73 return result;
74 }
75
76 // Extract grpc configuration
77 if (json.has("grpc"))
78 {
79 auto grpcSection = json["grpc"];
80
81 // Extract message size limits
82 if (grpcSection.has("max_receive_message_size_mb"))
83 {
84 int value = static_cast<int>(grpcSection["max_receive_message_size_mb"].i());
85 // Clamp to reasonable range: 1MB minimum, 1000MB maximum
86 result.config->max_receive_message_size_mb = std::max(1, std::min(1000, value));
87 }
88
89 if (grpcSection.has("max_send_message_size_mb"))
90 {
91 int value = static_cast<int>(grpcSection["max_send_message_size_mb"].i());
92 // Clamp to reasonable range: 1MB minimum, 1000MB maximum
93 result.config->max_send_message_size_mb = std::max(1, std::min(1000, value));
94 }
95
96 // Extract compression configuration
97 if (grpcSection.has("compression"))
98 {
99 auto compressionSection = grpcSection["compression"];
100
101 // Legacy boolean/algorithm keys. "enabled": false is honored as a
102 // one-time migration (-> level "none") so an explicit opt-out never
103 // silently re-enables compression after upgrade; "enabled": true and
104 // "algorithm" are superseded by level-based negotiation.
105 const bool hasLevel = compressionSection.has("level");
106 if (compressionSection.has("enabled"))
107 {
108 const bool enabled = compressionSection["enabled"].b();
109 if (!enabled && !hasLevel)
110 {
111 result.config->compression_level = GRPC_COMPRESS_LEVEL_NONE;
112 }
113 // The disposition must match the gating above: nothing is
114 // migrated when an explicit "level" is present.
115 const char* disposition;
116 if (hasLevel)
117 {
118 disposition = "ignored; the explicit \"level\" applies";
119 }
120 else if (enabled)
121 {
122 disposition = "ignored (the \"level\" default applies)";
123 }
124 else
125 {
126 disposition = "migrated to \"level\": \"none\"";
127 }
128 std::cerr << "WARNING: deprecated grpc.compression \"enabled\" key "
129 << disposition
130 << "; use \"level\": \"none|low|medium|high\" instead" << std::endl;
131 }
132 if (compressionSection.has("algorithm"))
133 {
134 std::cerr << "WARNING: deprecated grpc.compression \"algorithm\" key ignored; "
135 << "algorithm selection now follows \"level\"" << std::endl;
136 }
137
138 if (compressionSection.has("level"))
139 {
140 result.config->compression_level = ParseCompressionLevel(compressionSection["level"].s());
141 }
142 }
143
144 // Extract batch streaming configuration
145 if (grpcSection.has("batch_streaming"))
146 {
147 auto batchSection = grpcSection["batch_streaming"];
148
149 if (batchSection.has("enabled"))
150 {
151 result.config->enable_batch_streaming = batchSection["enabled"].b();
152 }
153
154 if (batchSection.has("batch_size"))
155 {
156 result.config->batch_size = static_cast<int>(batchSection["batch_size"].i());
157 result.config->ClampBatchSize(); // Ensure valid range
158 }
159 }
160
161 // Extract design streaming configuration (M6)
162 if (grpcSection.has("design_streaming"))
163 {
164 auto streamSection = grpcSection["design_streaming"];
165
166 if (streamSection.has("enabled"))
167 {
168 result.config->enable_design_streaming = streamSection["enabled"].b();
169 }
170
171 if (streamSection.has("feature_batch_size"))
172 {
173 int value = static_cast<int>(streamSection["feature_batch_size"].i());
174 result.config->design_stream_feature_batch_size = std::max(100, std::min(10000, value));
175 }
176
177 if (streamSection.has("spool_dir"))
178 {
179 result.config->stream_spool_dir = streamSection["spool_dir"].s();
180 }
181
182 if (streamSection.has("spool_max_bytes"))
183 {
184 // Clamp negatives to 0 first: Crow's .i() is signed,
185 // and a raw cast would wrap a negative JSON value to
186 // a huge uint64_t, silently disabling the cap.
187 const auto value = static_cast<std::uint64_t>(std::max<std::int64_t>(0, streamSection["spool_max_bytes"].i()));
188 result.config->stream_spool_max_bytes = (value == 0)
189 ? std::numeric_limits<std::uint64_t>::max()
190 : value;
191 }
192 }
193
194 // Extract thread pool configuration
195 if (grpcSection.has("thread_pool"))
196 {
197 auto threadSection = grpcSection["thread_pool"];
198
199 if (threadSection.has("max_threads"))
200 {
201 int value = static_cast<int>(threadSection["max_threads"].i());
202 result.config->max_threads = std::max(1, std::min(64, value));
203 }
204
205 if (threadSection.has("min_pollers"))
206 {
207 int value = static_cast<int>(threadSection["min_pollers"].i());
208 result.config->min_pollers = std::max(1, std::min(16, value));
209 }
210 }
211
212 // Extract keepalive configuration
213 if (grpcSection.has("keepalive"))
214 {
215 auto keepaliveSection = grpcSection["keepalive"];
216
217 if (keepaliveSection.has("time_s"))
218 {
219 int value = static_cast<int>(keepaliveSection["time_s"].i());
220 result.config->keepalive_time_s = std::max(1, std::min(3600, value));
221 }
222
223 if (keepaliveSection.has("timeout_s"))
224 {
225 int value = static_cast<int>(keepaliveSection["timeout_s"].i());
226 result.config->keepalive_timeout_s = std::max(1, std::min(300, value));
227 }
228
229 if (keepaliveSection.has("permit_without_calls"))
230 {
231 result.config->keepalive_permit_without_calls = keepaliveSection["permit_without_calls"].b();
232 }
233 }
234 }
235
236 result.loadedFromFile = true;
237 result.message = "Loaded gRPC config from: " + configPath;
238 }
239 catch (const std::exception& e)
240 {
241 // TODO: Refactor config error handling - preserve partially loaded config instead of silently falling back to defaults
242 // Issue: When JSON parsing fails, any successfully parsed values are lost
243 // Proposed: Store config values as parsed, only reset fields that failed, or fail explicitly
244 // Log exception and return default config
245 result.config = std::make_shared<GrpcServiceConfig>();
246 result.loadedFromFile = false;
247 result.message = "Using default gRPC config (exception loading " + configPath + ": " + e.what() + ")";
248 }
249 catch (...)
250 {
251 // TODO: Refactor config error handling - preserve partially loaded config instead of silently falling back to defaults
252 // Issue: When JSON parsing fails, any successfully parsed values are lost
253 // Proposed: Store config values as parsed, only reset fields that failed, or fail explicitly
254 // Log unknown exception and return default config
255 result.config = std::make_shared<GrpcServiceConfig>();
256 result.loadedFromFile = false;
257 result.message = "Using default gRPC config (unknown exception loading " + configPath + ")";
258 }
259
260 return result;
261 }
262 }
263}
static LoadResult LoadFromFile(const std::string &configPath)