-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from libp2p/feat/accept-queue
keep accepting and negotiating connections until we have 16 ready and queued up
- Loading branch information
Showing
3 changed files
with
69 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package stream | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
func newThreshold(cutoff int) *threshold { | ||
t := &threshold{ | ||
threshold: cutoff, | ||
} | ||
t.cond.L = &t.mu | ||
return t | ||
} | ||
|
||
type threshold struct { | ||
mu sync.Mutex | ||
cond sync.Cond | ||
|
||
count int | ||
threshold int | ||
} | ||
|
||
// Acquire increments the counter. It will not block. | ||
func (t *threshold) Acquire() { | ||
t.mu.Lock() | ||
t.count++ | ||
t.mu.Unlock() | ||
} | ||
|
||
// Release decrements the counter. | ||
func (t *threshold) Release() { | ||
t.mu.Lock() | ||
if t.count == 0 { | ||
panic("negative count") | ||
} | ||
if t.threshold == t.count { | ||
t.cond.Broadcast() | ||
} | ||
t.count-- | ||
t.mu.Unlock() | ||
} | ||
|
||
// Wait waits for the counter to drop below the threshold | ||
func (t *threshold) Wait() { | ||
t.mu.Lock() | ||
for t.count > t.threshold { | ||
t.cond.Wait() | ||
} | ||
t.mu.Unlock() | ||
} |
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