-
Notifications
You must be signed in to change notification settings - Fork 50
replace reusePort by failOnExistingPort, simpler state (single bool), no TOCTOU for numThreads=1
#53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
timotheecour
wants to merge
4
commits into
dom96:master
from
timotheecour:pr_httpbeast_reusePort_multithread_v3
Closed
replace reusePort by failOnExistingPort, simpler state (single bool), no TOCTOU for numThreads=1
#53
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4237486
reusePort = false now default; honored regarless of numThreads
timotheecour 3c9adbd
use failOnExistingPort
timotheecour 74ef5fc
use failOnExistingPort and remove reusePort option
timotheecour 1ea9edd
bump version because of the change
timotheecour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,9 +54,10 @@ type | |||||||||||||||||||
| domain*: Domain | ||||||||||||||||||||
| numThreads: int | ||||||||||||||||||||
| loggers: seq[Logger] | ||||||||||||||||||||
| reusePort: bool | ||||||||||||||||||||
| ## controls whether to fail with "Address already in use". | ||||||||||||||||||||
| ## Setting this to false will raise when `threads` are on. | ||||||||||||||||||||
| failOnExistingPort: bool | ||||||||||||||||||||
| ## Fail with "Address already in use" if port is in use, by attempting | ||||||||||||||||||||
| ## to bind to a temporary socket to fail early if `numThreads>1`, or | ||||||||||||||||||||
| ## not setting `OptReusePort` if `numThreads==1`. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| HttpBeastDefect* = ref object of Defect | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -67,14 +68,14 @@ proc initSettings*(port: Port = Port(8080), | |||||||||||||||||||
| bindAddr: string = "", | ||||||||||||||||||||
| numThreads: int = 0, | ||||||||||||||||||||
| domain = Domain.AF_INET, | ||||||||||||||||||||
| reusePort = true): Settings = | ||||||||||||||||||||
| failOnExistingPort = true): Settings = | ||||||||||||||||||||
| Settings( | ||||||||||||||||||||
| port: port, | ||||||||||||||||||||
| bindAddr: bindAddr, | ||||||||||||||||||||
| domain: domain, | ||||||||||||||||||||
| numThreads: numThreads, | ||||||||||||||||||||
| loggers: getHandlers(), | ||||||||||||||||||||
| reusePort: reusePort, | ||||||||||||||||||||
| failOnExistingPort: failOnExistingPort, | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| proc initData(fdKind: FdKind, ip = ""): Data = | ||||||||||||||||||||
|
|
@@ -311,6 +312,12 @@ proc updateDate(fd: AsyncFD): bool = | |||||||||||||||||||
| result = false # Returning true signifies we want timer to stop. | ||||||||||||||||||||
| serverDate = now().utc().format("ddd, dd MMM yyyy HH:mm:ss 'GMT'") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| proc newSocketAux(settings: Settings): owned(Socket) = | ||||||||||||||||||||
| result = newSocket(settings.domain) | ||||||||||||||||||||
| result.setSockOpt(OptReuseAddr, true) | ||||||||||||||||||||
| result.setSockOpt(OptReusePort, not settings.failOnExistingPort) | ||||||||||||||||||||
| result.bindAddr(settings.port, settings.bindAddr) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+315
to
+320
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| proc eventLoop(params: (OnRequest, Settings)) = | ||||||||||||||||||||
| let (onRequest, settings) = params | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -319,12 +326,7 @@ proc eventLoop(params: (OnRequest, Settings)) = | |||||||||||||||||||
|
|
||||||||||||||||||||
| let selector = newSelector[Data]() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| let server = newSocket(settings.domain) | ||||||||||||||||||||
| server.setSockOpt(OptReuseAddr, true) | ||||||||||||||||||||
| if compileOption("threads") and not settings.reusePort: | ||||||||||||||||||||
| raise HttpBeastDefect(msg: "--threads:on requires reusePort to be enabled in settings") | ||||||||||||||||||||
| server.setSockOpt(OptReusePort, settings.reusePort) | ||||||||||||||||||||
| server.bindAddr(settings.port, settings.bindAddr) | ||||||||||||||||||||
|
Comment on lines
-322
to
-327
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this. |
||||||||||||||||||||
| let server = newSocketAux(settings) | ||||||||||||||||||||
| server.listen() | ||||||||||||||||||||
| server.getFd().setBlocking(false) | ||||||||||||||||||||
| selector.registerHandle(server.getFd(), {Event.Read}, initData(Server)) | ||||||||||||||||||||
|
|
@@ -476,6 +478,10 @@ proc run*(onRequest: OnRequest, settings: Settings) = | |||||||||||||||||||
|
|
||||||||||||||||||||
| echo("Starting ", numThreads, " threads") | ||||||||||||||||||||
| if numThreads > 1: | ||||||||||||||||||||
| if settings.failOnExistingPort: | ||||||||||||||||||||
| close(newSocketAux(settings)) # attempt to bind to a temporary socket | ||||||||||||||||||||
| var settings = settings | ||||||||||||||||||||
| settings.failOnExistingPort = false | ||||||||||||||||||||
|
Comment on lines
+481
to
+484
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| when compileOption("threads"): | ||||||||||||||||||||
| var threads = newSeq[Thread[(OnRequest, Settings)]](numThreads) | ||||||||||||||||||||
| for i in 0 ..< numThreads: | ||||||||||||||||||||
|
|
||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.