From 0848a816b291927f8fc09253122b4928846eb9da Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 13 Nov 2020 17:03:07 +0100 Subject: [PATCH] ported to FreeRTOS (cherry picked from commit 9f566881f15bed58276d3df4b5b2cb2289016396) --- lib/pure/asyncdispatch.nim | 22 ++++++++++++---------- lib/pure/asynchttpserver.nim | 11 ++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index 0bacf3bb55..90345676fe 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -1946,13 +1946,15 @@ proc activeDescriptors*(): int {.inline.} = when defined(posix): import posix -proc maxDescriptors*(): int {.raises: OSError.} = - ## Returns the maximum number of active file descriptors for the current - ## process. This involves a system call. - when defined(windows): - result = 16_700_000 - else: - var fdLim: RLimit - if getrlimit(RLIMIT_NOFILE, fdLim) < 0: - raiseOSError(osLastError()) - result = int(fdLim.rlim_cur) - 1 +when defined(linux) or defined(windows) or defined(macosx) or defined(bsd): + proc maxDescriptors*(): int {.raises: OSError.} = + ## Returns the maximum number of active file descriptors for the current + ## process. This involves a system call. For now `maxDescriptors` is + ## supported on the following OSes: Windows, Linux, OSX, BSD. + when defined(windows): + result = 16_700_000 + else: + var fdLim: RLimit + if getrlimit(RLIMIT_NOFILE, fdLim) < 0: + raiseOSError(osLastError()) + result = int(fdLim.rlim_cur) - 1 diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim index b69f3f9a42..a5f6a0bc85 100644 --- a/lib/pure/asynchttpserver.nim +++ b/lib/pure/asynchttpserver.nim @@ -308,9 +308,18 @@ proc processClient(server: AsyncHttpServer, client: AsyncSocket, address: string ) if not retry: break +const + nimMaxDescriptorsFallback* {.intdefine.} = 16_000 ## fallback value for \ + ## when `maxDescriptors` is not available. + ## This can be set on the command line during compilation + ## via `-d:nimMaxDescriptorsFallback=N` + proc listen*(server: AsyncHttpServer; port: Port; address = "") = ## Listen to the given port and address. - server.maxFDs = maxDescriptors() + when declared(maxDescriptors): + server.maxFDs = try: maxDescriptors() except: nimMaxDescriptorsFallback + else: + server.maxFDs = nimMaxDescriptorsFallback server.socket = newAsyncSocket() if server.reuseAddr: server.socket.setSockOpt(OptReuseAddr, true)