-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
throttle the number of parallel client initial syncs in the tagger se…
…rver
- Loading branch information
Showing
6 changed files
with
153 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package server | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type token string | ||
|
||
// Throttler provides tokens with throttling logic that limits the number of active tokens at the same time | ||
// When a component is done with a token, it should release the token by calling the Release method | ||
type Throttler interface { | ||
// RequestToken returns a token | ||
RequestToken() token | ||
// ReleaseToken returns token back to the throttler | ||
// This method is idempotent (i.e. invoking it on the same token multiple times will have the same effect) | ||
Release(t token) | ||
} | ||
|
||
// limiter implements the Throttler interface | ||
type limiter struct { | ||
mutex sync.RWMutex | ||
tokensChan chan struct{} | ||
activeRequests map[token]struct{} | ||
} | ||
|
||
// NewSyncThrottler creates and returns a new Throttler | ||
func NewSyncThrottler(maxConcurrentSync uint32) Throttler { | ||
return &limiter{ | ||
mutex: sync.RWMutex{}, | ||
tokensChan: make(chan struct{}, maxConcurrentSync), | ||
activeRequests: make(map[token]struct{}), | ||
} | ||
} | ||
|
||
// RequestToken implements Throttler#RequestToken | ||
func (l *limiter) RequestToken() token { | ||
tk := token(uuid.New().String()) | ||
l.tokensChan <- struct{}{} | ||
|
||
l.mutex.Lock() | ||
defer l.mutex.Unlock() | ||
|
||
l.activeRequests[tk] = struct{}{} | ||
return tk | ||
} | ||
|
||
// Release implements Throttler#Release | ||
func (l *limiter) Release(t token) { | ||
l.mutex.Lock() | ||
defer l.mutex.Unlock() | ||
if _, found := l.activeRequests[t]; found { | ||
<-l.tokensChan | ||
delete(l.activeRequests, t) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package server | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSyncThrottler(t *testing.T) { | ||
throtler := NewSyncThrottler(3) | ||
|
||
t1 := throtler.RequestToken() | ||
t2 := throtler.RequestToken() | ||
t3 := throtler.RequestToken() | ||
|
||
go func() { | ||
time.Sleep(1 * time.Second) | ||
throtler.Release(t3) | ||
}() | ||
|
||
t4 := throtler.RequestToken() // this should block until token t3 is released | ||
throtler.Release(t4) | ||
|
||
throtler.Release(t4) // releasing a token that was already released should be ok (idempotent) | ||
|
||
throtler.Release(t1) | ||
throtler.Release(t2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters