diff --git a/src/client/spark_api/utils/BTS.cs b/src/client/spark_api/utils/BTS.cs index e34f8b65a..856365559 100644 --- a/src/client/spark_api/utils/BTS.cs +++ b/src/client/spark_api/utils/BTS.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading; namespace bts { public static class BTS { - static bool _needs_init_spark_lib = true; + static int _needs_init_spark_lib = 0; public static void init() { var args = Environment.GetCommandLineArgs(); utils.BTSNativeMethods.provide_command_line(args, args.Length); @@ -17,8 +18,7 @@ namespace bts throw new ApplicationException(buffer.ToString()); } - if (_needs_init_spark_lib) { - _needs_init_spark_lib = false; + if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) { utils.BTSNativeMethods.init_spark_lib(); } diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 811c8acea..6c29be4b7 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -144,14 +144,14 @@ 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) { +void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t delay, void* extra) { bts_assert(pclient); - bool connected = pclient->rpc.wait_until_connected(0); //accumulated_delay ? 0 : 1); - if (!connected && accumulated_delay < rpc_connect_timeout_ms()) { + bool connected = pclient->rpc.wait_until_connected(0); + if (!connected && delay > 0) { // 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); + api_client_send_internal(pclient, std::move(request), cb, delay - 100, extra); }, 100); return; } @@ -178,7 +178,7 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len 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); + api_client_send_internal(pclient, std::move(request), cb, rpc_connect_timeout_ms(), extra); } CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) {