-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding an upgraded event to conn * set stopped flag asap * trigger upgradded event on conn * set concurrency limit for accepts * backporting semaphore from tcp-limits2 * export unittests module * make params explicit * tone down debug logs * adding semaphore tests * use semaphore to throttle concurent upgrades * add libp2p scope * trigger upgraded event before any other events * add event handler for connection upgrade * cleanup upgraded event on conn close * make upgrades slot release rebust * dont forget to release slot on nil connection * misc * make sure semaphore is always released * minor improvements and a nil check * removing unneeded comment * make upgradeMonitor a non-closure proc * make sure the `upgraded` event is initialized * handle exceptions in accepts when stopping * don't leak exceptions when stopping accept loops
- Loading branch information
Showing
10 changed files
with
276 additions
and
32 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
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,75 @@ | ||
## Nim-Libp2p | ||
## Copyright (c) 2020 Status Research & Development GmbH | ||
## Licensed under either of | ||
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) | ||
## * MIT license ([LICENSE-MIT](LICENSE-MIT)) | ||
## at your option. | ||
## This file may not be copied, modified, or distributed except according to | ||
## those terms. | ||
|
||
import sequtils | ||
import chronos, chronicles | ||
|
||
# TODO: this should probably go in chronos | ||
|
||
logScope: | ||
topics = "libp2p semaphore" | ||
|
||
type | ||
AsyncSemaphore* = ref object of RootObj | ||
size*: int | ||
count*: int | ||
queue*: seq[Future[void]] | ||
|
||
proc init*(T: type AsyncSemaphore, size: int): T = | ||
T(size: size, count: size) | ||
|
||
proc tryAcquire*(s: AsyncSemaphore): bool = | ||
## Attempts to acquire a resource, if successful | ||
## returns true, otherwise false | ||
## | ||
|
||
if s.count > 0 and s.queue.len == 0: | ||
s.count.dec | ||
trace "Acquired slot", available = s.count, queue = s.queue.len | ||
return true | ||
|
||
proc acquire*(s: AsyncSemaphore): Future[void] = | ||
## Acquire a resource and decrement the resource | ||
## counter. If no more resources are available, | ||
## the returned future will not complete until | ||
## the resource count goes above 0 again. | ||
## | ||
|
||
let fut = newFuture[void]("AsyncSemaphore.acquire") | ||
if s.tryAcquire(): | ||
fut.complete() | ||
return fut | ||
|
||
s.queue.add(fut) | ||
s.count.dec | ||
trace "Queued slot", available = s.count, queue = s.queue.len | ||
return fut | ||
|
||
proc release*(s: AsyncSemaphore) = | ||
## Release a resource from the semaphore, | ||
## by picking the first future from the queue | ||
## and completing it and incrementing the | ||
## internal resource count | ||
## | ||
|
||
doAssert(s.count <= s.size) | ||
|
||
if s.count < s.size: | ||
trace "Releasing slot", available = s.count, | ||
queue = s.queue.len | ||
|
||
if s.queue.len > 0: | ||
var fut = s.queue.pop() | ||
if not fut.finished(): | ||
fut.complete() | ||
|
||
s.count.inc # increment the resource count | ||
trace "Released slot", available = s.count, | ||
queue = s.queue.len | ||
return |
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
Oops, something went wrong.