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:
@@ -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) => {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user