go.temporal.io/server@v1.23.0/common/quotas/multi_rate_limiter_impl.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package quotas 26 27 import ( 28 "context" 29 "fmt" 30 "time" 31 32 "golang.org/x/time/rate" 33 ) 34 35 const ( 36 InfDuration = rate.InfDuration 37 ) 38 39 type ( 40 // MultiRateLimiterImpl is a wrapper around the limiter interface 41 MultiRateLimiterImpl struct { 42 rateLimiters []RateLimiter 43 } 44 ) 45 46 var _ RateLimiter = (*MultiRateLimiterImpl)(nil) 47 48 // NewMultiRateLimiter returns a new rate limiter that have multiple stage 49 func NewMultiRateLimiter( 50 rateLimiters []RateLimiter, 51 ) *MultiRateLimiterImpl { 52 if len(rateLimiters) == 0 { 53 panic("expect at least one rate limiter") 54 } 55 return &MultiRateLimiterImpl{ 56 rateLimiters: rateLimiters, 57 } 58 } 59 60 // Allow immediately returns with true or false indicating if a rate limit 61 // token is available or not 62 func (rl *MultiRateLimiterImpl) Allow() bool { 63 return rl.AllowN(time.Now(), 1) 64 } 65 66 // AllowN immediately returns with true or false indicating if n rate limit 67 // token is available or not 68 func (rl *MultiRateLimiterImpl) AllowN(now time.Time, numToken int) bool { 69 length := len(rl.rateLimiters) 70 reservations := make([]Reservation, 0, length) 71 72 for _, rateLimiter := range rl.rateLimiters { 73 reservation := rateLimiter.ReserveN(now, numToken) 74 if !reservation.OK() || reservation.DelayFrom(now) > 0 { 75 if reservation.OK() { 76 reservation.CancelAt(now) 77 } 78 79 // cancel all existing reservation 80 for _, reservation := range reservations { 81 reservation.CancelAt(now) 82 } 83 return false 84 } 85 reservations = append(reservations, reservation) 86 } 87 88 return true 89 } 90 91 // Reserve returns a Reservation that indicates how long the caller 92 // must wait before event happen. 93 func (rl *MultiRateLimiterImpl) Reserve() Reservation { 94 return rl.ReserveN(time.Now(), 1) 95 } 96 97 // ReserveN returns a Reservation that indicates how long the caller 98 // must wait before event happen. 99 func (rl *MultiRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation { 100 length := len(rl.rateLimiters) 101 reservations := make([]Reservation, 0, length) 102 103 for _, rateLimiter := range rl.rateLimiters { 104 reservation := rateLimiter.ReserveN(now, numToken) 105 if !reservation.OK() { 106 // cancel all existing reservation 107 for _, reservation := range reservations { 108 reservation.CancelAt(now) 109 } 110 return NewMultiReservation(false, nil) 111 } 112 reservations = append(reservations, reservation) 113 } 114 115 return NewMultiReservation(true, reservations) 116 } 117 118 // Wait waits up till deadline for a rate limit token 119 func (rl *MultiRateLimiterImpl) Wait(ctx context.Context) error { 120 return rl.WaitN(ctx, 1) 121 } 122 123 // WaitN waits up till deadline for n rate limit token 124 func (rl *MultiRateLimiterImpl) WaitN(ctx context.Context, numToken int) error { 125 select { 126 case <-ctx.Done(): 127 return ctx.Err() 128 default: 129 } 130 131 now := time.Now().UTC() 132 reservation := rl.ReserveN(now, numToken) 133 if !reservation.OK() { 134 return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", numToken) 135 } 136 137 delay := reservation.DelayFrom(now) 138 if delay == 0 { 139 return nil 140 } 141 waitLimit := InfDuration 142 if deadline, ok := ctx.Deadline(); ok { 143 waitLimit = deadline.Sub(now) 144 } 145 if waitLimit < delay { 146 reservation.CancelAt(now) 147 return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", numToken) 148 } 149 150 t := time.NewTimer(delay) 151 defer t.Stop() 152 select { 153 case <-t.C: 154 return nil 155 156 case <-ctx.Done(): 157 reservation.CancelAt(time.Now()) 158 return ctx.Err() 159 } 160 } 161 162 // Rate returns the rate per second for this rate limiter 163 func (rl *MultiRateLimiterImpl) Rate() float64 { 164 result := rl.rateLimiters[0].Rate() 165 for _, rateLimiter := range rl.rateLimiters { 166 newRate := rateLimiter.Rate() 167 if result > newRate { 168 result = newRate 169 } 170 } 171 return result 172 } 173 174 // Burst returns the burst for this rate limiter 175 func (rl *MultiRateLimiterImpl) Burst() int { 176 result := rl.rateLimiters[0].Burst() 177 for _, rateLimiter := range rl.rateLimiters { 178 newBurst := rateLimiter.Burst() 179 if result > newBurst { 180 result = newBurst 181 } 182 } 183 return result 184 }