Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpbeast.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.3.0"
version = "0.4.0"
author = "Dominik Picheta"
description = "A super-fast epoll-backed and parallel HTTP server."
license = "MIT"
Expand Down
19 changes: 14 additions & 5 deletions src/httpbeast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type
domain*: Domain
numThreads: int
loggers: seq[Logger]
reusePort: bool
## controlls whether to fail with "Address already in use".
Comment thread
dom96 marked this conversation as resolved.
Outdated
## This is currently ignored if multiple threads are spawned.
Comment thread
dom96 marked this conversation as resolved.
Outdated

HttpBeastDefect* = ref object of Defect

Expand All @@ -63,14 +66,15 @@ const
proc initSettings*(port: Port = Port(8080),
bindAddr: string = "",
numThreads: int = 0,
domain = Domain.AF_INET): Settings =

domain = Domain.AF_INET,
reusePort = false): Settings =
Comment thread
dom96 marked this conversation as resolved.
Outdated
Settings(
port: port,
bindAddr: bindAddr,
domain: domain,
numThreads: numThreads,
loggers: getHandlers()
loggers: getHandlers(),
reusePort: reusePort,
)

proc initData(fdKind: FdKind, ip = ""): Data =
Expand Down Expand Up @@ -317,7 +321,7 @@ proc eventLoop(params: (OnRequest, Settings)) =

let server = newSocket(settings.domain)
server.setSockOpt(OptReuseAddr, true)
server.setSockOpt(OptReusePort, true)
server.setSockOpt(OptReusePort, settings.reusePort)
Comment thread
dom96 marked this conversation as resolved.
server.bindAddr(settings.port, settings.bindAddr)
server.listen()
server.getFd().setBlocking(false)
Expand Down Expand Up @@ -473,8 +477,13 @@ proc run*(onRequest: OnRequest, settings: Settings) =
when compileOption("threads"):
var threads = newSeq[Thread[(OnRequest, Settings)]](numThreads)
for i in 0 ..< numThreads:
var settings2 = settings
if numThreads > 1: settings2.reusePort = true
# TODO: in future work, we can honor `reusePort = false` by
# attempting to bind to the port, and then on success spawn the threads
# with `reusePort = false`.
Comment thread
dom96 marked this conversation as resolved.
Outdated
createThread[(OnRequest, Settings)](
threads[i], eventLoop, (onRequest, settings)
threads[i], eventLoop, (onRequest, settings2)
Comment thread
dom96 marked this conversation as resolved.
Outdated
)
echo("Listening on port ", settings.port) # This line is used in the tester to signal readiness.
joinThreads(threads)
Expand Down