34 result.
config = std::make_shared<GrpcServiceConfig>();
37 if (!std::filesystem::exists(configPath))
40 result.
message =
"Using default gRPC config (" + configPath +
" not found)";
47 std::ifstream file(configPath);
51 result.
message =
"Using default gRPC config (could not open " + configPath +
")";
55 std::stringstream buffer;
56 buffer << file.rdbuf();
59 std::string jsonContent = buffer.str();
60 if (jsonContent.empty())
63 result.
message =
"Using default gRPC config (" + configPath +
" is empty)";
68 auto json = crow::json::load(jsonContent);
72 result.
message =
"Using default gRPC config (failed to parse JSON in " + configPath +
")";
79 auto grpcSection = json[
"grpc"];
82 if (grpcSection.has(
"max_receive_message_size_mb"))
84 int value =
static_cast<int>(grpcSection[
"max_receive_message_size_mb"].i());
86 result.
config->max_receive_message_size_mb = std::max(1, std::min(1000, value));
89 if (grpcSection.has(
"max_send_message_size_mb"))
91 int value =
static_cast<int>(grpcSection[
"max_send_message_size_mb"].i());
93 result.
config->max_send_message_size_mb = std::max(1, std::min(1000, value));
97 if (grpcSection.has(
"compression"))
99 auto compressionSection = grpcSection[
"compression"];
105 const bool hasLevel = compressionSection.has(
"level");
106 if (compressionSection.has(
"enabled"))
108 const bool enabled = compressionSection[
"enabled"].b();
109 if (!enabled && !hasLevel)
111 result.
config->compression_level = GRPC_COMPRESS_LEVEL_NONE;
115 const char* disposition;
118 disposition =
"ignored; the explicit \"level\" applies";
122 disposition =
"ignored (the \"level\" default applies)";
126 disposition =
"migrated to \"level\": \"none\"";
128 std::cerr <<
"WARNING: deprecated grpc.compression \"enabled\" key "
130 <<
"; use \"level\": \"none|low|medium|high\" instead" << std::endl;
132 if (compressionSection.has(
"algorithm"))
134 std::cerr <<
"WARNING: deprecated grpc.compression \"algorithm\" key ignored; "
135 <<
"algorithm selection now follows \"level\"" << std::endl;
138 if (compressionSection.has(
"level"))
140 result.
config->compression_level = ParseCompressionLevel(compressionSection[
"level"].s());
145 if (grpcSection.has(
"batch_streaming"))
147 auto batchSection = grpcSection[
"batch_streaming"];
149 if (batchSection.has(
"enabled"))
151 result.
config->enable_batch_streaming = batchSection[
"enabled"].b();
154 if (batchSection.has(
"batch_size"))
156 result.
config->batch_size =
static_cast<int>(batchSection[
"batch_size"].i());
157 result.
config->ClampBatchSize();
162 if (grpcSection.has(
"design_streaming"))
164 auto streamSection = grpcSection[
"design_streaming"];
166 if (streamSection.has(
"enabled"))
168 result.
config->enable_design_streaming = streamSection[
"enabled"].b();
171 if (streamSection.has(
"feature_batch_size"))
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));
177 if (streamSection.has(
"spool_dir"))
179 result.
config->stream_spool_dir = streamSection[
"spool_dir"].s();
182 if (streamSection.has(
"spool_max_bytes"))
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()
195 if (grpcSection.has(
"thread_pool"))
197 auto threadSection = grpcSection[
"thread_pool"];
199 if (threadSection.has(
"max_threads"))
201 int value =
static_cast<int>(threadSection[
"max_threads"].i());
202 result.
config->max_threads = std::max(1, std::min(64, value));
205 if (threadSection.has(
"min_pollers"))
207 int value =
static_cast<int>(threadSection[
"min_pollers"].i());
208 result.
config->min_pollers = std::max(1, std::min(16, value));
213 if (grpcSection.has(
"keepalive"))
215 auto keepaliveSection = grpcSection[
"keepalive"];
217 if (keepaliveSection.has(
"time_s"))
219 int value =
static_cast<int>(keepaliveSection[
"time_s"].i());
220 result.
config->keepalive_time_s = std::max(1, std::min(3600, value));
223 if (keepaliveSection.has(
"timeout_s"))
225 int value =
static_cast<int>(keepaliveSection[
"timeout_s"].i());
226 result.
config->keepalive_timeout_s = std::max(1, std::min(300, value));
229 if (keepaliveSection.has(
"permit_without_calls"))
231 result.
config->keepalive_permit_without_calls = keepaliveSection[
"permit_without_calls"].b();
237 result.
message =
"Loaded gRPC config from: " + configPath;
239 catch (
const std::exception& e)
245 result.
config = std::make_shared<GrpcServiceConfig>();
247 result.
message =
"Using default gRPC config (exception loading " + configPath +
": " + e.what() +
")";
255 result.
config = std::make_shared<GrpcServiceConfig>();
257 result.
message =
"Using default gRPC config (unknown exception loading " + configPath +
")";