fix(broker_gateway): use blocking write for set_broker_client

try_write() is non-blocking and silently drops the CTraderClient when
the lock is contended. This means a successfully connected broker
client could be lost without any error. Changed to write().await which
guarantees the client is stored. Made function async and updated the
call site in main.rs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-02-23 23:08:43 +01:00
parent 2f6b76b8fe
commit 4e5a269353
2 changed files with 5 additions and 7 deletions

View File

@@ -93,7 +93,7 @@ async fn main() -> Result<()> {
match ctrader_openapi::CTraderClient::connect(ct_config).await {
Ok(ct_client) => {
service.set_broker_client(ct_client);
service.set_broker_client(ct_client).await;
info!("✓ cTrader broker connected");
}
Err(e) => {

View File

@@ -41,12 +41,10 @@ impl BrokerGatewayService {
/// Set the cTrader broker client (call after connecting).
#[cfg(feature = "icmarkets")]
pub fn set_broker_client(&self, client: CTraderClient) {
// Use try_write to avoid blocking — if locked, log and skip
match self.broker_client.try_write() {
Ok(mut guard) => *guard = Some(client),
Err(_) => warn!("could not set broker client — lock contended"),
}
pub async fn set_broker_client(&self, client: CTraderClient) {
let mut guard = self.broker_client.write().await;
*guard = Some(client);
info!("cTrader broker client set successfully");
}
/// Generate client order ID (UUID v4)