52 std::cerr <<
"ERROR: DesignCache is null, cannot start gRPC server" << std::endl;
58 std::string configPath;
59 const char* envConfig = std::getenv(
"GRPC_CONFIG_PATH");
60 if (envConfig !=
nullptr && envConfig[0] !=
'\0')
62 std::filesystem::path p(envConfig);
64 p = std::filesystem::weakly_canonical(p, ec);
66 std::filesystem::path exeDir = std::filesystem::weakly_canonical(
args().executableDirectory(), ec);
67 std::filesystem::path currentDir = std::filesystem::current_path(ec);
69 bool isValidPath =
false;
71 if (std::filesystem::proximate(p, exeDir, ec).
string().find(
"..") == std::string::npos ||
72 std::filesystem::proximate(p, currentDir, ec).
string().find(
"..") == std::string::npos)
77 if (!ec && p.extension() ==
".json" && isValidPath && std::filesystem::is_regular_file(p, ec))
79 configPath = p.string();
83 std::cerr <<
"WARNING: GRPC_CONFIG_PATH must be a valid path to an existing .json file within allowed directories. Falling back to default locations." << std::endl;
88 if (envConfig ==
nullptr || envConfig[0] ==
'\0')
92 std::filesystem::path configInExeDir = exeDir /
"config.json";
95 std::filesystem::path configInCwd =
"config.json";
98 if (std::filesystem::exists(configInExeDir))
100 configPath = configInExeDir.string();
102 else if (std::filesystem::exists(configInCwd))
104 configPath = configInCwd.string();
109 configPath = configInExeDir.string();
113 std::cout << loadResult.message << std::endl;
117 grpc::EnableDefaultHealthCheckService(
true);
118 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
120 grpc::ServerBuilder builder;
124 const int64_t maxReceiveBytes =
static_cast<int64_t
>(loadResult.config->max_receive_message_size_mb) * 1024 * 1024;
125 const int64_t maxSendBytes =
static_cast<int64_t
>(loadResult.config->max_send_message_size_mb) * 1024 * 1024;
129 constexpr int64_t maxIntBytes =
static_cast<int64_t
>(std::numeric_limits<int>::max());
130 if (maxReceiveBytes > maxIntBytes)
132 std::cerr <<
"WARNING: max_receive_message_size_mb exceeds " << maxIntBytes / (1024 * 1024)
133 <<
" MB; clamping gRPC receive limit to INT_MAX bytes" << std::endl;
135 if (maxSendBytes > maxIntBytes)
137 std::cerr <<
"WARNING: max_send_message_size_mb exceeds " << maxIntBytes / (1024 * 1024)
138 <<
" MB; clamping gRPC send limit to INT_MAX bytes" << std::endl;
140 builder.SetMaxReceiveMessageSize(
static_cast<int>(std::min(maxReceiveBytes, maxIntBytes)));
141 builder.SetMaxSendMessageSize(
static_cast<int>(std::min(maxSendBytes, maxIntBytes)));
143 std::cout <<
"gRPC max message sizes: receive=" << loadResult.config->max_receive_message_size_mb
144 <<
"MB (" << maxReceiveBytes <<
" bytes)"
145 <<
", send=" << loadResult.config->max_send_message_size_mb
146 <<
"MB (" << maxSendBytes <<
" bytes)" << std::endl;
148 if (loadResult.config->compression_level != GRPC_COMPRESS_LEVEL_NONE)
150 builder.SetDefaultCompressionLevel(loadResult.config->compression_level);
151 std::cout <<
"gRPC compression level: "
157 std::cout <<
"gRPC compression: disabled" << std::endl;
161 grpc::ResourceQuota quota;
162 quota.SetMaxThreads(loadResult.config->max_threads);
163 builder.SetResourceQuota(quota);
164 builder.SetSyncServerOption(grpc::ServerBuilder::SyncServerOption::MIN_POLLERS,
165 loadResult.config->min_pollers);
167 std::cout <<
"gRPC thread pool: max_threads=" << loadResult.config->max_threads
168 <<
", min_pollers=" << loadResult.config->min_pollers << std::endl;
171 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIME_MS,
172 loadResult.config->keepalive_time_s * 1000);
173 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIMEOUT_MS,
174 loadResult.config->keepalive_timeout_s * 1000);
175 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS,
176 loadResult.config->keepalive_permit_without_calls ? 1 : 0);
177 builder.AddChannelArgument(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS,
178 (loadResult.config->keepalive_time_s * 1000) / 2);
180 std::cout <<
"gRPC keepalive: time=" << loadResult.config->keepalive_time_s
181 <<
"s, timeout=" << loadResult.config->keepalive_timeout_s
182 <<
"s, permit_without_calls="
183 << (loadResult.config->keepalive_permit_without_calls ?
"true" :
"false")
186 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
187 builder.RegisterService(&service);
190 std::unique_ptr<grpc::Server> serverUnique = builder.BuildAndStart();
192 std::cerr <<
"ERROR: Failed to start gRPC server on " << server_address << std::endl;
195 std::cout <<
"gRPC server listening on " << server_address << std::endl;
198 std::shared_ptr<grpc::Server> serverShared(std::move(serverUnique));
202 serverShared->Wait();
203 std::cout <<
"gRPC server shut down." << std::endl;