MCPcopy Index your code
hub / github.com/ccxt/ccxt / leakyBucketLoop

Method leakyBucketLoop

java/lib/src/main/java/io/github/ccxt/Throttler.java:128–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

126 }
127
128 private void leakyBucketLoop() {
129 long lastTimestamp = milliseconds();
130
131 while (true) {
132 List<CompletableFuture<Void>> toComplete = new ArrayList<>();
133 long sleepMs = 0;
134
135 lock.lock();
136 try {
137 if (queue.isEmpty()) {
138 running = false;
139 return;
140 }
141
142 // Refill tokens based on elapsed time
143 long now = milliseconds();
144 long elapsed = now - lastTimestamp;
145 lastTimestamp = now;
146 if (elapsed > 0) {
147 double newTokens = this.tokens + (this.refillRate * elapsed);
148 this.tokens = Math.min(newTokens, this.capacity);
149 }
150
151 // Batch: complete all affordable requests
152 while (!queue.isEmpty() && this.tokens >= 0) {
153 QueueElement el = queue.poll();
154 this.tokens -= el.cost;
155 toComplete.add(el.future);
156 }
157
158 // If queue still has items but no tokens, compute exact wait time
159 if (!queue.isEmpty() && this.tokens < 0) {
160 double deficit = -this.tokens;
161 sleepMs = (long) Math.ceil(deficit / this.refillRate);
162 if (sleepMs < 1) sleepMs = 1;
163 }
164 } finally {
165 lock.unlock();
166 }
167
168 // Complete futures outside the lock to reduce hold time
169 for (var f : toComplete) {
170 f.complete(null);
171 }
172
173 if (sleepMs > 0) {
174 try {
175 Thread.sleep(sleepMs);
176 } catch (InterruptedException e) {
177 Thread.currentThread().interrupt();
178 return;
179 }
180 }
181 }
182 }
183
184 private void rollingWindowLoop() {
185 while (true) {

Callers 1

loopMethod · 0.95

Calls 5

millisecondsMethod · 0.95
isEmptyMethod · 0.80
minMethod · 0.45
addMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected