go.temporal.io/server@v1.23.0/common/quotas/delayed_request_rate_limiter.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 "errors" 29 "fmt" 30 "time" 31 32 "go.temporal.io/server/common/clock" 33 ) 34 35 // DelayedRequestRateLimiter is a rate limiter that allows all requests without any delay for a given duration. After 36 // the delay expires, it delegates to another rate limiter. This rate limiter is useful for cases where you want to 37 // allow all requests for a given duration, e.g. during something volatile like a deployment, and then switch to another 38 // rate limiter after the duration expires. 39 type DelayedRequestRateLimiter struct { 40 // RequestRateLimiter is the delegate that we switch to after the delay expires. 41 RequestRateLimiter 42 // timer triggers the rate limiter to delegate to the underlying rate limiter. We hold a reference to it in order to 43 // cancel it prematurely if needed. 44 timer clock.Timer 45 } 46 47 var ErrNegativeDelay = errors.New("delay cannot be negative") 48 49 // NewDelayedRequestRateLimiter returns a DelayedRequestRateLimiter that delegates to the given rate limiter after a 50 // delay. The timeSource is used to create the timer that triggers the switch. It returns an error if the given delay 51 // is negative. 52 func NewDelayedRequestRateLimiter( 53 rl RequestRateLimiter, 54 delay time.Duration, 55 timeSource clock.TimeSource, 56 ) (*DelayedRequestRateLimiter, error) { 57 if delay < 0 { 58 return nil, fmt.Errorf("%w: %v", ErrNegativeDelay, delay) 59 } 60 61 delegator := RequestRateLimiterDelegator{} 62 delegator.SetRateLimiter(NoopRequestRateLimiter) 63 64 timer := timeSource.AfterFunc(delay, func() { 65 delegator.SetRateLimiter(rl) 66 }) 67 68 return &DelayedRequestRateLimiter{ 69 RequestRateLimiter: &delegator, 70 timer: timer, 71 }, nil 72 } 73 74 // Cancel stops the timer that triggers the rate limiter to delegate to the underlying rate limiter. It returns true if 75 // the timer was stopped before it expired. 76 func (rl *DelayedRequestRateLimiter) Cancel() bool { 77 return rl.timer.Stop() 78 }