async: cleaner solution that avoids GC_ref on strings which doesn't exist for --gc:arc
This commit is contained in:
parent
a1e7bf81b3
commit
eea0cb07cf
1 changed files with 5 additions and 6 deletions
|
|
@ -1830,20 +1830,19 @@ proc accept*(socket: AsyncFD,
|
||||||
retFut.complete(future.read.client)
|
retFut.complete(future.read.client)
|
||||||
return retFut
|
return retFut
|
||||||
|
|
||||||
|
proc keepAlive(x: string) =
|
||||||
|
discard "mark 'x' as escaping so that it is put into a closure for us to keep the data alive"
|
||||||
|
|
||||||
proc send*(socket: AsyncFD, data: string,
|
proc send*(socket: AsyncFD, data: string,
|
||||||
flags = {SocketFlag.SafeDisconn}): owned(Future[void]) =
|
flags = {SocketFlag.SafeDisconn}): owned(Future[void]) =
|
||||||
## Sends ``data`` to ``socket``. The returned future will complete once all
|
## Sends ``data`` to ``socket``. The returned future will complete once all
|
||||||
## data has been sent.
|
## data has been sent.
|
||||||
var retFuture = newFuture[void]("send")
|
var retFuture = newFuture[void]("send")
|
||||||
|
|
||||||
var copiedData = data
|
let sendFut = socket.send(unsafeAddr data[0], data.len, flags)
|
||||||
GC_ref(copiedData) # we need to protect data until send operation is completed
|
|
||||||
# or failed.
|
|
||||||
|
|
||||||
let sendFut = socket.send(addr copiedData[0], data.len, flags)
|
|
||||||
sendFut.callback =
|
sendFut.callback =
|
||||||
proc () =
|
proc () =
|
||||||
GC_unref(copiedData)
|
keepAlive(data)
|
||||||
if sendFut.failed:
|
if sendFut.failed:
|
||||||
retFuture.fail(sendFut.error)
|
retFuture.fail(sendFut.error)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue