Skip to content

Commit da518bb

Browse files
committed
httpbeast: option to set reusePort (#47)
Squashed commit of the following: commit 43ee8c3 Author: Dominik Picheta <[email protected]> Date: Fri Jul 2 21:50:46 2021 +0100 Fix `run`. commit 804d42c Author: Dominik Picheta <[email protected]> Date: Fri Jul 2 21:42:03 2021 +0100 Update src/httpbeast.nim commit eca38f9 Author: Dominik Picheta <[email protected]> Date: Fri Jul 2 21:40:22 2021 +0100 Update src/httpbeast.nim commit c316b87 Author: Dominik Picheta <[email protected]> Date: Fri Jul 2 21:36:33 2021 +0100 Apply suggestions from code review commit e08209b Author: Timothee Cour <[email protected]> Date: Thu Jul 1 17:02:43 2021 -0700 ignore reusePort when running with multiple threads for now commit 64310f4 Author: Timothee Cour <[email protected]> Date: Sun Jun 27 15:42:52 2021 -0700 fix tests (use an Option to make code backward compatible) commit d6564d0 Author: Timothee Cour <[email protected]> Date: Sun Jun 27 14:52:39 2021 -0700 bump version commit 39b93cf Author: Timothee Cour <[email protected]> Date: Wed Apr 17 10:09:08 2019 -0700 httpbeast: option to set reusePort
1 parent 16a5bec commit da518bb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

httpbeast.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.3.0"
3+
version = "0.4.0"
44
author = "Dominik Picheta"
55
description = "A super-fast epoll-backed and parallel HTTP server."
66
license = "MIT"

src/httpbeast.nim

+11-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type
5454
domain*: Domain
5555
numThreads: int
5656
loggers: seq[Logger]
57+
reusePort: bool
58+
## controls whether to fail with "Address already in use".
59+
## Setting this to false will raise when `threads` are on.
5760

5861
HttpBeastDefect* = ref object of Defect
5962

@@ -63,14 +66,15 @@ const
6366
proc initSettings*(port: Port = Port(8080),
6467
bindAddr: string = "",
6568
numThreads: int = 0,
66-
domain = Domain.AF_INET): Settings =
67-
69+
domain = Domain.AF_INET,
70+
reusePort = true): Settings =
6871
Settings(
6972
port: port,
7073
bindAddr: bindAddr,
7174
domain: domain,
7275
numThreads: numThreads,
73-
loggers: getHandlers()
76+
loggers: getHandlers(),
77+
reusePort: reusePort,
7478
)
7579

7680
proc initData(fdKind: FdKind, ip = ""): Data =
@@ -317,7 +321,9 @@ proc eventLoop(params: (OnRequest, Settings)) =
317321

318322
let server = newSocket(settings.domain)
319323
server.setSockOpt(OptReuseAddr, true)
320-
server.setSockOpt(OptReusePort, true)
324+
if compileOption("threads") and not settings.reusePort:
325+
raise HttpBeastDefect(msg: "--threads:on requires reusePort to be enabled in settings")
326+
server.setSockOpt(OptReusePort, settings.reusePort)
321327
server.bindAddr(settings.port, settings.bindAddr)
322328
server.listen()
323329
server.getFd().setBlocking(false)
@@ -488,7 +494,7 @@ proc run*(onRequest: OnRequest) {.inline.} =
488494
## request.
489495
##
490496
## See the other ``run`` proc for more info.
491-
run(onRequest, Settings(port: Port(8080), bindAddr: "", domain: Domain.AF_INET))
497+
run(onRequest, initSettings(port=Port(8080), bindAddr="", domain=Domain.AF_INET))
492498

493499
when false:
494500
proc close*(port: Port) =

0 commit comments

Comments
 (0)