Void futures are no longer discardable.
This commit is contained in:
parent
634a416c67
commit
374706b1c3
3 changed files with 18 additions and 12 deletions
|
|
@ -126,6 +126,15 @@ proc failed*(future: PFutureBase): bool =
|
||||||
## Determines whether ``future`` completed with an error.
|
## Determines whether ``future`` completed with an error.
|
||||||
future.error != nil
|
future.error != nil
|
||||||
|
|
||||||
|
proc asyncCheck*[T](future: PFuture[T]) =
|
||||||
|
## Sets a callback on ``future`` which raises an exception if the future
|
||||||
|
## finished with an error.
|
||||||
|
##
|
||||||
|
## This should be used instead of ``discard`` to discard void futures.
|
||||||
|
future.callback =
|
||||||
|
proc () =
|
||||||
|
if future.failed: raise future.error
|
||||||
|
|
||||||
when defined(windows) or defined(nimdoc):
|
when defined(windows) or defined(nimdoc):
|
||||||
import winlean, sets, hashes
|
import winlean, sets, hashes
|
||||||
type
|
type
|
||||||
|
|
@ -1021,8 +1030,6 @@ macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
result[4].del(i)
|
result[4].del(i)
|
||||||
if subtypeIsVoid:
|
if subtypeIsVoid:
|
||||||
# Add discardable pragma.
|
# Add discardable pragma.
|
||||||
if prc.kind == nnkProcDef: # TODO: This is a workaround for #1287
|
|
||||||
result[4].add(newIdentNode("discardable"))
|
|
||||||
if returnType.kind == nnkEmpty:
|
if returnType.kind == nnkEmpty:
|
||||||
# Add PFuture[void]
|
# Add PFuture[void]
|
||||||
result[3][0] = parseExpr("PFuture[void]")
|
result[3][0] = parseExpr("PFuture[void]")
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,6 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
request.hostname = address
|
request.hostname = address
|
||||||
assert client != nil
|
assert client != nil
|
||||||
request.client = client
|
request.client = client
|
||||||
var runCallback = true
|
|
||||||
|
|
||||||
# First line - GET /path HTTP/1.1
|
# First line - GET /path HTTP/1.1
|
||||||
let line = await client.recvLine() # TODO: Timeouts.
|
let line = await client.recvLine() # TODO: Timeouts.
|
||||||
|
|
@ -115,8 +114,8 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
return
|
return
|
||||||
let lineParts = line.split(' ')
|
let lineParts = line.split(' ')
|
||||||
if lineParts.len != 3:
|
if lineParts.len != 3:
|
||||||
request.respond(Http400, "Invalid request. Got: " & line)
|
await request.respond(Http400, "Invalid request. Got: " & line)
|
||||||
runCallback = false
|
continue
|
||||||
|
|
||||||
let reqMethod = lineParts[0]
|
let reqMethod = lineParts[0]
|
||||||
let path = lineParts[1]
|
let path = lineParts[1]
|
||||||
|
|
@ -140,8 +139,9 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
try:
|
try:
|
||||||
request.protocol = protocol.parseProtocol()
|
request.protocol = protocol.parseProtocol()
|
||||||
except EInvalidValue:
|
except EInvalidValue:
|
||||||
request.respond(Http400, "Invalid request protocol. Got: " & protocol)
|
asyncCheck request.respond(Http400, "Invalid request protocol. Got: " &
|
||||||
runCallback = false
|
protocol)
|
||||||
|
continue
|
||||||
|
|
||||||
if reqMethod.normalize == "post":
|
if reqMethod.normalize == "post":
|
||||||
# Check for Expect header
|
# Check for Expect header
|
||||||
|
|
@ -162,11 +162,10 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
assert request.body.len == contentLength
|
assert request.body.len == contentLength
|
||||||
else:
|
else:
|
||||||
await request.respond(Http400, "Bad Request. No Content-Length.")
|
await request.respond(Http400, "Bad Request. No Content-Length.")
|
||||||
runCallback = false
|
continue
|
||||||
|
|
||||||
case reqMethod.normalize
|
case reqMethod.normalize
|
||||||
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
|
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
|
||||||
if runCallback:
|
|
||||||
await callback(request)
|
await callback(request)
|
||||||
else:
|
else:
|
||||||
await request.respond(Http400, "Invalid request method. Got: " & reqMethod)
|
await request.respond(Http400, "Invalid request method. Got: " & reqMethod)
|
||||||
|
|
@ -199,7 +198,7 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
|
||||||
# TODO: Causes compiler crash.
|
# TODO: Causes compiler crash.
|
||||||
#var (address, client) = await server.socket.acceptAddr()
|
#var (address, client) = await server.socket.acceptAddr()
|
||||||
var fut = await server.socket.acceptAddr()
|
var fut = await server.socket.acceptAddr()
|
||||||
processClient(fut.client, fut.address, callback)
|
asyncCheck processClient(fut.client, fut.address, callback)
|
||||||
|
|
||||||
proc close*(server: PAsyncHttpServer) =
|
proc close*(server: PAsyncHttpServer) =
|
||||||
## Terminates the async http server instance.
|
## Terminates the async http server instance.
|
||||||
|
|
|
||||||
|
|
@ -654,7 +654,7 @@ when isMainModule:
|
||||||
resp = await client.request("http://nimrod-lang.org/download.html")
|
resp = await client.request("http://nimrod-lang.org/download.html")
|
||||||
echo("Got response: ", resp.status)
|
echo("Got response: ", resp.status)
|
||||||
|
|
||||||
main()
|
asyncCheck main()
|
||||||
runForever()
|
runForever()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue