|
| 1 | +// Copyright (c) 2022 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package ratelimit // import "go.uber.org/ratelimit" |
| 22 | + |
| 23 | +import ( |
| 24 | + "time" |
| 25 | + |
| 26 | + "sync/atomic" |
| 27 | +) |
| 28 | + |
| 29 | +type atomicInt64Limiter struct { |
| 30 | + //lint:ignore U1000 Padding is unused but it is crucial to maintain performance |
| 31 | + // of this rate limiter in case of collocation with other frequently accessed memory. |
| 32 | + prepadding [64]byte // cache line size = 64; created to avoid false sharing. |
| 33 | + state int64 // unix nanoseconds of the next permissions issue. |
| 34 | + //lint:ignore U1000 like prepadding. |
| 35 | + postpadding [56]byte // cache line size - state size = 64 - 8; created to avoid false sharing. |
| 36 | + |
| 37 | + perRequest time.Duration |
| 38 | + maxSlack time.Duration |
| 39 | + clock Clock |
| 40 | +} |
| 41 | + |
| 42 | +// newAtomicBased returns a new atomic based limiter. |
| 43 | +func newAtomicInt64Based(rate int, opts ...Option) *atomicInt64Limiter { |
| 44 | + // TODO consider moving config building to the implementation |
| 45 | + // independent code. |
| 46 | + config := buildConfig(opts) |
| 47 | + perRequest := config.per / time.Duration(rate) |
| 48 | + l := &atomicInt64Limiter{ |
| 49 | + perRequest: perRequest, |
| 50 | + maxSlack: time.Duration(config.slack) * perRequest, |
| 51 | + clock: config.clock, |
| 52 | + } |
| 53 | + atomic.StoreInt64(&l.state, 0) |
| 54 | + return l |
| 55 | +} |
| 56 | + |
| 57 | +// Take blocks to ensure that the time spent between multiple |
| 58 | +// Take calls is on average time.Second/rate. |
| 59 | +func (t *atomicInt64Limiter) Take() time.Time { |
| 60 | + var ( |
| 61 | + newTimeOfNextPermissionIssue int64 |
| 62 | + now int64 |
| 63 | + ) |
| 64 | + for { |
| 65 | + now = t.clock.Now().UnixNano() |
| 66 | + timeOfNextPermissionIssue := atomic.LoadInt64(&t.state) |
| 67 | + |
| 68 | + switch { |
| 69 | + case timeOfNextPermissionIssue == 0: |
| 70 | + // If this is our first request, then we allow it. |
| 71 | + newTimeOfNextPermissionIssue = now |
| 72 | + case now-timeOfNextPermissionIssue > int64(t.maxSlack): |
| 73 | + // a lot of nanoseconds passed since the last Take call |
| 74 | + // we will limit max accumulated time to maxSlack |
| 75 | + newTimeOfNextPermissionIssue = now - int64(t.maxSlack) |
| 76 | + default: |
| 77 | + // calculate the time at which our permission was issued |
| 78 | + newTimeOfNextPermissionIssue = timeOfNextPermissionIssue + int64(t.perRequest) |
| 79 | + } |
| 80 | + |
| 81 | + if atomic.CompareAndSwapInt64(&t.state, timeOfNextPermissionIssue, newTimeOfNextPermissionIssue) { |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + nanosToSleepUntilOurPermissionIsIssued := newTimeOfNextPermissionIssue - now |
| 86 | + if nanosToSleepUntilOurPermissionIsIssued > 0 { |
| 87 | + t.clock.Sleep(time.Duration(nanosToSleepUntilOurPermissionIsIssued)) |
| 88 | + } |
| 89 | + return time.Unix(0, newTimeOfNextPermissionIssue) |
| 90 | +} |
0 commit comments