go.temporal.io/server@v1.23.0/common/quotas/request_rate_limiter_delegator.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 "sync/atomic" 30 "time" 31 ) 32 33 // RequestRateLimiterDelegator is a request rate limiter that delegates to another rate limiter. The delegate can be 34 // changed at runtime by calling SetRateLimiter. This rate limiter is useful for cases where you want to substitute one 35 // rate limiter implementation for another at runtime. All methods of this type are thread-safe. 36 type RequestRateLimiterDelegator struct { 37 // delegate is an atomic.Value so that it can be safely read from and written to concurrently. It stores the rate 38 // limiter that this rate limiter delegates to as a monomorphicRequestRateLimiter. 39 delegate atomic.Value 40 } 41 42 // monomorphicRequestRateLimiter is a workaround for the fact that the value stored in an atomic.Value must always be 43 // the same type, but we want to allow the rate limiter delegate to be any type that implements the RequestRateLimiter 44 // interface. 45 type monomorphicRequestRateLimiter struct { 46 RequestRateLimiter 47 } 48 49 // SetRateLimiter sets the rate limiter to delegate to. 50 func (d *RequestRateLimiterDelegator) SetRateLimiter(rl RequestRateLimiter) { 51 d.delegate.Store(monomorphicRequestRateLimiter{rl}) 52 } 53 54 // loadDelegate returns the rate limiter that this rate limiter delegates to. 55 func (d *RequestRateLimiterDelegator) loadDelegate() RequestRateLimiter { 56 return d.delegate.Load().(RequestRateLimiter) 57 } 58 59 // The following methods just delegate to the underlying rate limiter. 60 61 func (d *RequestRateLimiterDelegator) Allow(now time.Time, request Request) bool { 62 return d.loadDelegate().Allow(now, request) 63 } 64 65 func (d *RequestRateLimiterDelegator) Reserve(now time.Time, request Request) Reservation { 66 return d.loadDelegate().Reserve(now, request) 67 } 68 69 func (d *RequestRateLimiterDelegator) Wait(ctx context.Context, request Request) error { 70 return d.loadDelegate().Wait(ctx, request) 71 }