forked from netbirdio/netbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[client] Add peer conn init limit (netbirdio#3001)
Limit the peer connection initialization to 200 peers at the same time
- Loading branch information
Showing
5 changed files
with
131 additions
and
7 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,48 @@ | ||
package semaphoregroup | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// SemaphoreGroup is a custom type that combines sync.WaitGroup and a semaphore. | ||
type SemaphoreGroup struct { | ||
waitGroup sync.WaitGroup | ||
semaphore chan struct{} | ||
} | ||
|
||
// NewSemaphoreGroup creates a new SemaphoreGroup with the specified semaphore limit. | ||
func NewSemaphoreGroup(limit int) *SemaphoreGroup { | ||
return &SemaphoreGroup{ | ||
semaphore: make(chan struct{}, limit), | ||
} | ||
} | ||
|
||
// Add increments the internal WaitGroup counter and acquires a semaphore slot. | ||
func (sg *SemaphoreGroup) Add(ctx context.Context) { | ||
sg.waitGroup.Add(1) | ||
|
||
// Acquire semaphore slot | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case sg.semaphore <- struct{}{}: | ||
} | ||
} | ||
|
||
// Done decrements the internal WaitGroup counter and releases a semaphore slot. | ||
func (sg *SemaphoreGroup) Done(ctx context.Context) { | ||
sg.waitGroup.Done() | ||
|
||
// Release semaphore slot | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-sg.semaphore: | ||
} | ||
} | ||
|
||
// Wait waits until the internal WaitGroup counter is zero. | ||
func (sg *SemaphoreGroup) Wait() { | ||
sg.waitGroup.Wait() | ||
} |
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,66 @@ | ||
package semaphoregroup | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSemaphoreGroup(t *testing.T) { | ||
semGroup := NewSemaphoreGroup(2) | ||
|
||
for i := 0; i < 5; i++ { | ||
semGroup.Add(context.Background()) | ||
go func(id int) { | ||
defer semGroup.Done(context.Background()) | ||
|
||
got := len(semGroup.semaphore) | ||
if got == 0 { | ||
t.Errorf("Expected semaphore length > 0 , got 0") | ||
} | ||
|
||
time.Sleep(time.Millisecond) | ||
t.Logf("Goroutine %d is running\n", id) | ||
}(i) | ||
} | ||
|
||
semGroup.Wait() | ||
|
||
want := 0 | ||
got := len(semGroup.semaphore) | ||
if got != want { | ||
t.Errorf("Expected semaphore length %d, got %d", want, got) | ||
} | ||
} | ||
|
||
func TestSemaphoreGroupContext(t *testing.T) { | ||
semGroup := NewSemaphoreGroup(1) | ||
semGroup.Add(context.Background()) | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
t.Cleanup(cancel) | ||
rChan := make(chan struct{}) | ||
|
||
go func() { | ||
semGroup.Add(ctx) | ||
rChan <- struct{}{} | ||
}() | ||
select { | ||
case <-rChan: | ||
case <-time.NewTimer(2 * time.Second).C: | ||
t.Error("Adding to semaphore group should not block when context is not done") | ||
} | ||
|
||
semGroup.Done(context.Background()) | ||
|
||
ctxDone, cancelDone := context.WithTimeout(context.Background(), 1*time.Second) | ||
t.Cleanup(cancelDone) | ||
go func() { | ||
semGroup.Done(ctxDone) | ||
rChan <- struct{}{} | ||
}() | ||
select { | ||
case <-rChan: | ||
case <-time.NewTimer(2 * time.Second).C: | ||
t.Error("Releasing from semaphore group should not block when context is not done") | ||
} | ||
} |