diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 487b2eabb..811c8acea 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -140,14 +140,23 @@ void reply_error(ApiError error, raw_response_callback cb, void* extra) { } -CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { +int32_t rpc_connect_timeout_ms() { + static int32_t t = config::get().rpc_connect_timeout_seconds() * 1000; + return t; +} +void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t accumulated_delay, void* extra) { bts_assert(pclient); - std::string strbuf(buffer, buffer_length); - qbus::RPCRequest request(move(strbuf)); - pclient->rpc.wait_until_connected(1); - LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + bool connected = pclient->rpc.wait_until_connected(0); //accumulated_delay ? 0 : 1); + if (!connected && accumulated_delay < rpc_connect_timeout_ms()) { + // This branch is the uncommon 'slow path' where connection is taking longer than we expect + LOG(Api,Diag,"rpc call delay waiting for connection {}",((int64_t)request.value().c_str())); + pclient->qbus.post([=, request{std::move(request)}]() mutable { + api_client_send_internal(pclient, std::move(request), cb, accumulated_delay + 100, extra); + }, 100); + return; + } pclient->rpc.send(std::move(request), [=](qbus::RPCError e, const qbus::RPCReplyPtr& reply) { - LOG(Api,Diag,"rpc reply {}",((int64_t)buffer)); + LOG(Api,Diag,"rpc reply {}",((int64_t)request.value().c_str())); switch (e) { case qbus::RPCError::RPC_OK: safe_call_fn([&]() { @@ -165,6 +174,12 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len } }); } +CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { + LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + std::string strbuf(buffer, buffer_length); + qbus::RPCRequest request(move(strbuf)); + api_client_send_internal(pclient, std::move(request), cb, 0, extra); +} CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) { return pclient->qbus.on_qb_thread(); diff --git a/src/lib/config/protos/quickbus_cfg.proto b/src/lib/config/protos/quickbus_cfg.proto index c779a2f77..bed2302ee 100644 --- a/src/lib/config/protos/quickbus_cfg.proto +++ b/src/lib/config/protos/quickbus_cfg.proto @@ -18,3 +18,6 @@ message QuickbusCfg { optional uint32 max_missed_hbs = 6 [default=3]; } +message RPCConfig { + optional uint32 rpc_connect_timeout_seconds = 1 [default = 10]; +} \ No newline at end of file