asynchttpserver cleanups [backport:1.0] (#15966)

* asynchttpserver cleanups [backport:1.0]
This commit is contained in:
Andreas Rumpf 2020-11-15 07:27:13 +01:00 • committed by GitHub
commit 122f22d163
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

View file

@ -14,8 +14,8 @@
## application in production you should use a reverse proxy (for example nginx) ## application in production you should use a reverse proxy (for example nginx)
## instead of allowing users to connect directly to this server. ## instead of allowing users to connect directly to this server.
## ##
## Basic usage ## Example
## =========== ## =======
## ##
## This example will create an HTTP server on port 8080. The server will ## This example will create an HTTP server on port 8080. The server will
## respond to all requests with a ``200 OK`` response code and "Hello World" ## respond to all requests with a ``200 OK`` response code and "Hello World"
@ -32,11 +32,10 @@
## "Content-type": "text/plain; charset=utf-8"} ## "Content-type": "text/plain; charset=utf-8"}
## await req.respond(Http200, "Hello World", headers.newHttpHeaders()) ## await req.respond(Http200, "Hello World", headers.newHttpHeaders())
## ##
## server.listen Port(5555) ## server.listen Port(8080)
## while true: ## while true:
## if server.shouldAcceptRequest(5): ## if server.shouldAcceptRequest():
## var (address, client) = await server.socket.acceptAddr() ## asyncCheck server.acceptRequest(cb)
## asyncCheck processClient(server, client, address, cb)
## else: ## else:
## poll() ## poll()
## ##
@ -333,7 +332,7 @@ proc shouldAcceptRequest*(server: AsyncHttpServer;
result = assumedDescriptorsPerRequest < 0 or result = assumedDescriptorsPerRequest < 0 or
(activeDescriptors() + assumedDescriptorsPerRequest < server.maxFDs) (activeDescriptors() + assumedDescriptorsPerRequest < server.maxFDs)
proc acceptRequest*(server: AsyncHttpServer, port: Port, proc acceptRequest*(server: AsyncHttpServer,
callback: proc (request: Request): Future[void] {.closure, gcsafe.}) {.async.} = callback: proc (request: Request): Future[void] {.closure, gcsafe.}) {.async.} =
## Accepts a single request. Write an explicit loop around this proc so that ## Accepts a single request. Write an explicit loop around this proc so that
## errors can be handled properly. ## errors can be handled properly.
@ -343,7 +342,7 @@ proc acceptRequest*(server: AsyncHttpServer, port: Port,
proc serve*(server: AsyncHttpServer, port: Port, proc serve*(server: AsyncHttpServer, port: Port,
callback: proc (request: Request): Future[void] {.closure, gcsafe.}, callback: proc (request: Request): Future[void] {.closure, gcsafe.},
address = ""; address = "";
assumedDescriptorsPerRequest = 5) {.async.} = assumedDescriptorsPerRequest = -1) {.async.} =
## Starts the process of listening for incoming HTTP connections on the ## Starts the process of listening for incoming HTTP connections on the
## specified address and port. ## specified address and port.
## ##
@ -382,9 +381,8 @@ when not defined(testing) and isMainModule:
server.listen Port(5555) server.listen Port(5555)
while true: while true:
if server.shouldAcceptRequest(5): if server.shouldAcceptRequest():
var (address, client) = await server.socket.acceptAddr() asyncCheck server.acceptRequest(cb)
asyncCheck processClient(server, client, address, cb)
else: else:
poll() poll()

View file

@ -6,7 +6,7 @@ type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.vo
but expected one of: but expected one of:
proc serve(server: AsyncHttpServer; port: Port; proc serve(server: AsyncHttpServer; port: Port;
callback: proc (request: Request): Future[void] {.closure, gcsafe.}; callback: proc (request: Request): Future[void] {.closure, gcsafe.};
address = ""; assumedDescriptorsPerRequest = 5): owned(Future[void]) address = ""; assumedDescriptorsPerRequest = -1): owned(Future[void])
first type mismatch at position: 3 first type mismatch at position: 3
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.} required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.} but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}