Fixes the asynchttpserver example some more (#16599)
I dislike this example a lot (busy looping for FDs to be closed is a very poor waste of resources) but at least with these changes it's a little bit better.
This commit is contained in:
parent
04b765c16d
commit
4754806fb5
1 changed files with 7 additions and 8 deletions
|
|
@ -18,29 +18,28 @@ runnableExamples:
|
||||||
# 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"
|
||||||
# as the response body. Run locally with:
|
# as the response body. Run locally with:
|
||||||
# `nim doc --doccmd:-d:nimAsynchttpserverEnableTest --lib:lib lib/pure/asynchttpserver.nim`
|
# `nim doc --doccmd:-d:nimAsyncHttpServerEnableTest --lib:lib lib/pure/asynchttpserver.nim`
|
||||||
import asyncdispatch
|
import asyncdispatch
|
||||||
if defined(nimAsynchttpserverEnableTest):
|
if defined(nimAsyncHttpServerEnableTest):
|
||||||
proc main {.async.} =
|
proc main {.async.} =
|
||||||
const port = 8080
|
const port = 8080
|
||||||
var server = newAsyncHttpServer()
|
var server = newAsyncHttpServer()
|
||||||
proc cb(req: Request) {.async.} =
|
proc cb(req: Request) {.async.} =
|
||||||
echo (req.reqMethod, req.url, req.headers)
|
echo (req.reqMethod, req.url, req.headers)
|
||||||
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
let headers = {"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())
|
||||||
|
|
||||||
echo "test this with: curl localhost:" & $port & "/"
|
echo "test this with: curl localhost:" & $port & "/"
|
||||||
server.listen Port(port)
|
server.listen(Port(port))
|
||||||
while true:
|
while true:
|
||||||
if server.shouldAcceptRequest():
|
if server.shouldAcceptRequest():
|
||||||
await server.acceptRequest(cb)
|
await server.acceptRequest(cb)
|
||||||
else:
|
else:
|
||||||
# too many concurrent connections, `maxFDs` exceeded
|
# too many concurrent connections, `maxFDs` exceeded
|
||||||
poll()
|
# wait 500ms for FDs to be closed
|
||||||
|
await sleepAsync(500)
|
||||||
|
|
||||||
asyncCheck main()
|
waitFor main()
|
||||||
runForever()
|
|
||||||
|
|
||||||
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
||||||
import httpcore
|
import httpcore
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue