From c8c86116a3b6e3838145ef9ce25d1bcf68773049 Mon Sep 17 00:00:00 2001 From: Arham Jain <7073362+ajusa@users.noreply.github.com> Date: Tue, 26 Oct 2021 12:06:15 -0400 Subject: [PATCH] Use main thread to serve requests prevents blocking and plays more nicely with other async code. --- src/httpbeast.nim | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/httpbeast.nim b/src/httpbeast.nim index 452e778..92d2654 100644 --- a/src/httpbeast.nim +++ b/src/httpbeast.nim @@ -497,17 +497,15 @@ proc run*(onRequest: OnRequest, settings: Settings) = echo("Starting ", numThreads, " threads") if numThreads > 1: when compileOption("threads"): - var threads = newSeq[Thread[(OnRequest, Settings)]](numThreads) - for i in 0 ..< numThreads: + var threads = newSeq[Thread[(OnRequest, Settings)]](numThreads - 1) + for t in threads.mitems(): createThread[(OnRequest, Settings)]( - threads[i], eventLoop, (onRequest, settings) + t, eventLoop, (onRequest, settings) ) echo("Listening on port ", settings.port) # This line is used in the tester to signal readiness. - joinThreads(threads) else: assert false - else: - eventLoop((onRequest, settings)) + eventLoop((onRequest, settings)) proc run*(onRequest: OnRequest) {.inline.} = ## Starts the HTTP server with default settings. Calls `onRequest` for each