@@ -129,6 +129,9 @@ pub struct Cache {
129129 yield_curves : AHashMap < String , YieldCurveData > ,
130130 external_order_claims : AHashMap < InstrumentId , StrategyId > ,
131131 client_accounts : AHashMap < ClientId , AccountId > ,
132+ client_routes : AHashMap < Venue , ClientId > ,
133+ default_client_id : Option < ClientId > ,
134+ external_clients : AHashSet < ClientId > ,
132135 accounts : AHashMap < AccountId , SharedCell < AccountAny > > ,
133136 orders : AHashMap < ClientOrderId , SharedCell < OrderAny > > ,
134137 order_lists : AHashMap < OrderListId , OrderList > ,
@@ -164,6 +167,9 @@ impl Debug for Cache {
164167 . field ( "yield_curves" , & self . yield_curves )
165168 . field ( "external_order_claims" , & self . external_order_claims )
166169 . field ( "client_accounts" , & self . client_accounts )
170+ . field ( "client_routes" , & self . client_routes )
171+ . field ( "default_client_id" , & self . default_client_id )
172+ . field ( "external_clients" , & self . external_clients )
167173 . field ( "accounts" , & self . accounts )
168174 . field ( "orders" , & self . orders )
169175 . field ( "order_lists" , & self . order_lists )
@@ -233,6 +239,9 @@ impl Cache {
233239 yield_curves : AHashMap :: new ( ) ,
234240 external_order_claims : AHashMap :: new ( ) ,
235241 client_accounts : AHashMap :: new ( ) ,
242+ client_routes : AHashMap :: new ( ) ,
243+ default_client_id : None ,
244+ external_clients : AHashSet :: new ( ) ,
236245 accounts : AHashMap :: new ( ) ,
237246 orders : AHashMap :: new ( ) ,
238247 order_lists : AHashMap :: new ( ) ,
@@ -377,6 +386,51 @@ impl Cache {
377386 self . client_accounts . remove ( client_id) ;
378387 }
379388
389+ /// Returns the execution client registered as the route for `venue`, else the default client.
390+ #[ must_use]
391+ pub fn client_id_for_venue ( & self , venue : & Venue ) -> Option < & ClientId > {
392+ self . client_routes
393+ . get ( venue)
394+ . or ( self . default_client_id . as_ref ( ) )
395+ }
396+
397+ /// Registers the execution client `client_id` as the route for commands on `venue`, replacing
398+ /// any previous route for the venue.
399+ ///
400+ /// Routes and the default client survive [`Self::clear_index`] and [`Self::reset`].
401+ pub fn add_client_route ( & mut self , client_id : ClientId , venue : Venue ) {
402+ self . client_routes . insert ( venue, client_id) ;
403+ }
404+
405+ /// Registers the execution client `client_id` as the default route for venues without a
406+ /// route, replacing any previous default.
407+ pub fn set_default_client ( & mut self , client_id : ClientId ) {
408+ self . default_client_id = Some ( client_id) ;
409+ }
410+
411+ /// Removes the venue routes and default route of the execution client `client_id`.
412+ pub fn remove_client_routes ( & mut self , client_id : & ClientId ) {
413+ self . client_routes . retain ( |_, routed| routed != client_id) ;
414+
415+ if self . default_client_id . as_ref ( ) == Some ( client_id) {
416+ self . default_client_id = None ;
417+ }
418+ }
419+
420+ /// Returns whether `client_id` is registered as an external execution client, whose commands
421+ /// are published for another process to execute.
422+ #[ must_use]
423+ pub fn is_external_client ( & self , client_id : & ClientId ) -> bool {
424+ self . external_clients . contains ( client_id)
425+ }
426+
427+ /// Registers `client_id` as an external execution client.
428+ ///
429+ /// Registrations survive [`Self::clear_index`] and [`Self::reset`].
430+ pub fn add_external_client ( & mut self , client_id : ClientId ) {
431+ self . external_clients . insert ( client_id) ;
432+ }
433+
380434 /// Sets the cache database adapter for persistence.
381435 ///
382436 /// This allows setting or replacing the database adapter after cache construction.
@@ -1980,8 +2034,8 @@ impl Cache {
19802034 /// All stateful fields are reset to their initial value. Instruments,
19812035 /// currencies, and synthetics are retained when `drop_instruments_on_reset`
19822036 /// is `false` so that repeated backtest runs can reuse the same dataset. External order claims
1983- /// and execution client account registrations are retained so registered strategy and client
1984- /// routing remain configured across resets.
2037+ /// and execution client account, route, and external client registrations are retained so
2038+ /// registered strategy and client routing remain configured across resets.
19852039 pub fn reset ( & mut self ) {
19862040 log:: debug!( "Resetting cache" ) ;
19872041
0 commit comments