Void futures are no longer discardable.

This commit is contained in:
Dominik Picheta
2014-07-13 10:19:48 +01:00
parent 634a416c67
commit 374706b1c3
3 changed files with 18 additions and 12 deletions

View File

@@ -126,6 +126,15 @@ proc failed*(future: PFutureBase): bool =
## Determines whether ``future`` completed with an error.
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):
import winlean, sets, hashes
type
@@ -1021,8 +1030,6 @@ macro async*(prc: stmt): stmt {.immediate.} =
result[4].del(i)
if subtypeIsVoid:
# Add discardable pragma.
if prc.kind == nnkProcDef: # TODO: This is a workaround for #1287
result[4].add(newIdentNode("discardable"))
if returnType.kind == nnkEmpty:
# Add PFuture[void]
result[3][0] = parseExpr("PFuture[void]")

View File

@@ -106,7 +106,6 @@ proc processClient(client: PAsyncSocket, address: string,
request.hostname = address
assert client != nil
request.client = client
var runCallback = true
# First line - GET /path HTTP/1.1
let line = await client.recvLine() # TODO: Timeouts.
@@ -115,8 +114,8 @@ proc processClient(client: PAsyncSocket, address: string,
return
let lineParts = line.split(' ')
if lineParts.len != 3:
request.respond(Http400, "Invalid request. Got: " & line)
runCallback = false
await request.respond(Http400, "Invalid request. Got: " & line)
continue
let reqMethod = lineParts[0]
let path = lineParts[1]
@@ -140,8 +139,9 @@ proc processClient(client: PAsyncSocket, address: string,
try:
request.protocol = protocol.parseProtocol()
except EInvalidValue:
request.respond(Http400, "Invalid request protocol. Got: " & protocol)
runCallback = false
asyncCheck request.respond(Http400, "Invalid request protocol. Got: " &
protocol)
continue
if reqMethod.normalize == "post":
# Check for Expect header
@@ -162,12 +162,11 @@ proc processClient(client: PAsyncSocket, address: string,
assert request.body.len == contentLength
else:
await request.respond(Http400, "Bad Request. No Content-Length.")
runCallback = false
continue
case reqMethod.normalize
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
if runCallback:
await callback(request)
await callback(request)
else:
await request.respond(Http400, "Invalid request method. Got: " & reqMethod)
@@ -199,7 +198,7 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
# TODO: Causes compiler crash.
#var (address, client) = 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) =
## Terminates the async http server instance.

View File

@@ -654,7 +654,7 @@ when isMainModule:
resp = await client.request("http://nimrod-lang.org/download.html")
echo("Got response: ", resp.status)
main()
asyncCheck main()
runForever()
else: