|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.util; |
| 10 | + |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | +import java.util.function.LongSupplier; |
| 14 | + |
| 15 | +/** |
| 16 | + * TokenBucket is used to limit the number of operations at a constant rate while allowing for short bursts. |
| 17 | + * |
| 18 | + * @opensearch.internal |
| 19 | + */ |
| 20 | +public class TokenBucket { |
| 21 | + /** |
| 22 | + * Defines a monotonically increasing counter. |
| 23 | + * |
| 24 | + * Usage examples: |
| 25 | + * 1. clock = System::nanoTime can be used to perform rate-limiting per unit time |
| 26 | + * 2. clock = AtomicLong::get can be used to perform rate-limiting per unit number of operations |
| 27 | + */ |
| 28 | + private final LongSupplier clock; |
| 29 | + |
| 30 | + /** |
| 31 | + * Defines the number of tokens added to the bucket per clock cycle. |
| 32 | + */ |
| 33 | + private final double rate; |
| 34 | + |
| 35 | + /** |
| 36 | + * Defines the capacity and the maximum number of operations that can be performed per clock cycle before |
| 37 | + * the bucket runs out of tokens. |
| 38 | + */ |
| 39 | + private final double burst; |
| 40 | + |
| 41 | + /** |
| 42 | + * Defines the current state of the token bucket. |
| 43 | + */ |
| 44 | + private final AtomicReference<State> state; |
| 45 | + |
| 46 | + public TokenBucket(LongSupplier clock, double rate, double burst) { |
| 47 | + this(clock, rate, burst, burst); |
| 48 | + } |
| 49 | + |
| 50 | + public TokenBucket(LongSupplier clock, double rate, double burst, double initialTokens) { |
| 51 | + if (rate <= 0.0) { |
| 52 | + throw new IllegalArgumentException("rate must be greater than zero"); |
| 53 | + } |
| 54 | + |
| 55 | + if (burst <= 0.0) { |
| 56 | + throw new IllegalArgumentException("burst must be greater than zero"); |
| 57 | + } |
| 58 | + |
| 59 | + this.clock = clock; |
| 60 | + this.rate = rate; |
| 61 | + this.burst = burst; |
| 62 | + this.state = new AtomicReference<>(new State(Math.min(initialTokens, burst), clock.getAsLong())); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * If there are enough tokens in the bucket, it requests/deducts 'n' tokens and returns true. |
| 67 | + * Otherwise, returns false and leaves the bucket untouched. |
| 68 | + */ |
| 69 | + public boolean request(double n) { |
| 70 | + if (n <= 0) { |
| 71 | + throw new IllegalArgumentException("requested tokens must be greater than zero"); |
| 72 | + } |
| 73 | + |
| 74 | + // Refill tokens |
| 75 | + State currentState, updatedState; |
| 76 | + do { |
| 77 | + currentState = state.get(); |
| 78 | + long now = clock.getAsLong(); |
| 79 | + double incr = (now - currentState.lastRefilledAt) * rate; |
| 80 | + updatedState = new State(Math.min(currentState.tokens + incr, burst), now); |
| 81 | + } while (state.compareAndSet(currentState, updatedState) == false); |
| 82 | + |
| 83 | + // Deduct tokens |
| 84 | + do { |
| 85 | + currentState = state.get(); |
| 86 | + if (currentState.tokens < n) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + updatedState = new State(currentState.tokens - n, currentState.lastRefilledAt); |
| 90 | + } while (state.compareAndSet(currentState, updatedState) == false); |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + public boolean request() { |
| 96 | + return request(1.0); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Represents an immutable token bucket state. |
| 101 | + */ |
| 102 | + private static class State { |
| 103 | + final double tokens; |
| 104 | + final long lastRefilledAt; |
| 105 | + |
| 106 | + public State(double tokens, long lastRefilledAt) { |
| 107 | + this.tokens = tokens; |
| 108 | + this.lastRefilledAt = lastRefilledAt; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean equals(Object o) { |
| 113 | + if (this == o) return true; |
| 114 | + if (o == null || getClass() != o.getClass()) return false; |
| 115 | + State state = (State) o; |
| 116 | + return Double.compare(state.tokens, tokens) == 0 && lastRefilledAt == state.lastRefilledAt; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public int hashCode() { |
| 121 | + return Objects.hash(tokens, lastRefilledAt); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments