mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** I added `quotas.DelayedRateLimiter`, which adds a delay before an input `quotas.RateLimiter` actually takes effect. I also added some utilities to make its implementation easier. The delayed rate limiter depends on the new rate limiter delegator and no-op rate limiter structs. <!-- Tell your future self why have you made these changes --> **Why?** In some cases in the history service, we want to delay rate limiters for a short period after startup. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** There is 100% test coverage for the new code. <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** None. These are just additions. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
87 lines
3.2 KiB
Go
87 lines
3.2 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package quotas_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.temporal.io/server/common/quotas"
|
|
)
|
|
|
|
// blockingRequestRateLimiter is a rate limiter that blocks in its Wait method until the context is canceled.
|
|
type blockingRequestRateLimiter struct {
|
|
// RateLimiter is an embedded field so that blockingRequestRateLimiter implements the RateLimiter interface. It doesn't
|
|
// actually delegate to this rate limiter, and this field should be left nil.
|
|
quotas.RequestRateLimiter
|
|
// waitStarted is a channel which is sent to as soon as Wait is called.
|
|
waitStarted chan struct{}
|
|
}
|
|
|
|
func (b *blockingRequestRateLimiter) Wait(ctx context.Context, _ quotas.Request) error {
|
|
b.waitStarted <- struct{}{}
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
// TestRateLimiterDelegator_Wait verifies that the RequestRateLimiterDelegator.Wait method can be called concurrently even if
|
|
// the rate limiter it delegates to is switched while the method is being called. The same condition should hold for
|
|
// all methods on RequestRateLimiterDelegator, but we only test Wait here for simplicity.
|
|
func TestRateLimiterDelegator_Wait(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
blockingRateLimiter := &blockingRequestRateLimiter{
|
|
waitStarted: make(chan struct{}),
|
|
}
|
|
delegator := "as.RequestRateLimiterDelegator{}
|
|
delegator.SetRateLimiter(blockingRateLimiter)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
waitErrs := make(chan error)
|
|
|
|
go func() {
|
|
waitErrs <- delegator.Wait(ctx, quotas.Request{})
|
|
}()
|
|
<-blockingRateLimiter.waitStarted
|
|
delegator.SetRateLimiter(quotas.NoopRequestRateLimiter)
|
|
assert.NoError(t, delegator.Wait(ctx, quotas.Request{}))
|
|
select {
|
|
case err := <-waitErrs:
|
|
t.Fatal("Wait returned before context was canceled:", err)
|
|
default:
|
|
}
|
|
cancel()
|
|
assert.ErrorIs(t, <-waitErrs, context.Canceled)
|
|
}
|
|
|
|
func TestRateLimiterDelegator_SetRateLimiter(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
delegator := "as.RequestRateLimiterDelegator{}
|
|
delegator.SetRateLimiter(quotas.NoopRequestRateLimiter)
|
|
testNoopRequestRateLimiterImpl(t, delegator)
|
|
}
|