nim-chronos icon indicating copy to clipboard operation
nim-chronos copied to clipboard

question: how to simply turn my server to multiple threads

Open bung87 opened this issue 3 years ago • 3 comments

I tried below , found only one single thread actually handle requests.

I read httpbeast source its thread func use global dispatcher, then I checked chronos almost threaded dispatcher.

when compileOption("threads"):
    proc threadFunc(){.thread.} =
      let address = "0.0.0.0:" & $port
      let flags = {ReuseAddr, ReusePort}
      let r = newRouter[ScorperCallback]()
      r.addRoute(jsonHandler)
      r.addRoute(plaintextHandler)
      var server = newScorper(address, r, flags)
      onThreadDestruction proc(){.raises: [].} =
        try:
          server.stop(); waitFor server.closeWait()
        except:
          discard
      server.start()
      waitFor server.join()

    let numThreads = countProcessors()
    var thr = newSeq[Thread[void]](numThreads)
    for i in 0..high(thr):
      # pinToCpu(thr[i],i+1)
      createThread(thr[i], threadFunc)
    joinThreads(thr)

bung87 avatar Jun 16 '21 19:06 bung87

the classic way to use multiple threads is to use a single thread for accepting incoming connections then pass those connections to worker threads that handle them

arnetheduck avatar Jun 17 '21 07:06 arnetheduck

Thanks! I think I need look into createStreamServer since the callback handle accepted connection.

bung87 avatar Jun 17 '21 07:06 bung87

I tried something like this https://github.com/bung87/scorper/blob/827233a807d5a1eb3cc50ccd7ca10a52199b6f92/src/scorper/http/streamserver.nim#L897-L919
it does not work, could you help me with this?

bung87 avatar Jun 18 '21 11:06 bung87