1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Outbound message rate limiter (#950).
use ;
use ;
use lock;
/// Caps how fast a client sends messages to TWS / IB Gateway.
///
/// TWS limits the messages an API client may send (IBKR documents 50 per
/// second, rejecting the excess with error 100 and possibly disconnecting a
/// session that keeps exceeding it). The library enforces nothing unless a
/// limiter is installed with `ClientBuilder::rate_limiter`. With one installed,
/// a message over the budget is delayed, never rejected: the sync client
/// blocks the sending thread, the async client awaits without blocking the
/// runtime.
///
/// # What is counted
///
/// Every message the client sends after the connection handshake: requests,
/// orders and cancels. The handshake itself, including reconnects, is not.
///
/// Cancels (`Cancel*` messages, the global order cancel) count against the
/// budget but are sent at once, so unwanted data stops immediately and the
/// next request takes the delay. Cancels expressed as a request with an off
/// flag, such as account updates with `subscribe = false`, wait like requests.
///
/// # The limit
///
/// [`per_second(n)`](Self::per_second) sends at most `n` messages in any
/// one-second window: a small burst goes out at once, the rest at an even
/// pace. Sending cancels can briefly go above `n`; the requests after them
/// wait it out. [`Default`] is `per_second(50)`, IBKR's documented rate.
///
/// IBKR ties the rate to market-data lines (about lines / 2 per second), so
/// accounts with booster packs may allow more. The API does not report an
/// account's lines, so set a higher `n` yourself if yours allows it. A limit
/// that is too low only delays requests.
///
/// # Sharing
///
/// IBKR applies the limit to all clients of one TWS / IB Gateway together.
/// Clones of a limiter share one budget, so pass clones to every client
/// connected to the same gateway. Sync and async clients can share one.
///
/// An async send that is cancelled while waiting (dropped by a timeout, say)
/// still uses its slot.
///
/// Historical-data pacing (error 162) and market-data lines (error 101) are
/// separate limits this does not enforce.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(feature = "async")]
/// # async fn run() -> Result<(), ibapi::Error> {
/// use ibapi::{Client, RateLimiter};
///
/// // Two clients on one gateway, 50 messages per second between them.
/// let limiter = RateLimiter::default();
/// let trading = Client::builder()
/// .address("127.0.0.1:4002")
/// .client_id(100)
/// .rate_limiter(limiter.clone())
/// .connect()
/// .await?;
/// let research = Client::builder()
/// .address("127.0.0.1:4002")
/// .client_id(101)
/// .rate_limiter(limiter)
/// .connect()
/// .await?;
/// # drop((trading, research));
/// # Ok(())
/// # }
/// ```
/// Token bucket in GCRA form: one theoretical arrival time instead of a token
/// count. Each send moves `tat` one `interval` later; a send may go out once
/// `tat` is within `tolerance` of now, which admits `burst` sends at once.