You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While troubleshooting #1533, it became evident that the code handling the InteractiveBrokersClient component lifecycle (_start, _stop, _reset, _degrade) could use some refactoring given how integrated it is with connectivity events.
Here are some observations:
In the client __init__, the _reset component lifecycle method is called. Starting an event loop or running an asyncio task from the constructor is generally considered an anti-pattern.
When the component is reseting to start up, it starts the watch dog task which checks for connectivity (which obviously isn't yet established) then initiates a reconnect. Once the reconnect establishes a successful first connection, the watchdog then calls the _start method, which indicates the client is ready. This sequence of events works, but it's not especially intuitive. It would make more sense in the _start method to (1) establish a connection using the _connect method, and then (2) start the three tasks: watchdog, TWS incoming message reader, and internal message queue processor.
The _reset method should call _stop then _start, which intuitively is what resetting does.
The _resume method should call the method to resubscribe to all subscriptions.
The watchdog should be simplified to simply monitor for connectivity issues and trigger reconnects. That said, in reality, connectivity issues tend to be triggered by the TWS incoming message reader since that runs in a continuous loop while the watchdog only executes every second.
In live / paper trading, you may want to indefinitely attempt to reconnect rather than stop after N attempts.
Nits: some variables and methods could be named more appropriately. E.g. _is_ib_ready could be renamed to _is_ib_connected since that's really what the event flag indciates. _watch_dog_task could be renamed to _connection_watchdog_task (watchdog generally doesn't have a space or underscore).
The text was updated successfully, but these errors were encountered:
@cjdsellers I believe these updates have been merged in #1532 and can now be closed. I will have the opportunity to fully test them and write related tests using the simulated mock that was added last week in a follow-up.
Feature Request
While troubleshooting #1533, it became evident that the code handling the InteractiveBrokersClient component lifecycle (
_start
,_stop
,_reset
,_degrade
) could use some refactoring given how integrated it is with connectivity events.Here are some observations:
__init__
, the_reset
component lifecycle method is called. Starting an event loop or running an asyncio task from the constructor is generally considered an anti-pattern._start
method, which indicates the client is ready. This sequence of events works, but it's not especially intuitive. It would make more sense in the_start
method to (1) establish a connection using the_connect
method, and then (2) start the three tasks: watchdog, TWS incoming message reader, and internal message queue processor._reset
method should call_stop
then_start
, which intuitively is what resetting does._resume
method should call the method to resubscribe to all subscriptions._is_ib_ready
could be renamed to_is_ib_connected
since that's really what the event flag indciates._watch_dog_task
could be renamed to_connection_watchdog_task
(watchdog generally doesn't have a space or underscore).The text was updated successfully, but these errors were encountered: