fix cancellation syntax (#615)

* fix cancellation syntax

* simplify
This commit is contained in:
Jacek Sieka 2023-06-02 14:33:06 +02:00 committed by GitHub
parent 91b2b9d2ed
commit 55b9da0bea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 15 deletions

View File

@ -284,10 +284,8 @@ proc generateNewUniqueSocket[A](
return none[UtpSocket[A]]()
proc innerConnect[A](s: UtpSocket[A]): Future[ConnectionResult[A]] {.async.} =
let startFut = s.startOutgoingSocket()
try:
await startFut
await s.startOutgoingSocket()
utp_success_outgoing.inc()
debug "Outgoing connection successful", dst = s.socketKey
return ok(s)
@ -304,18 +302,7 @@ proc innerConnect[A](s: UtpSocket[A]): Future[ConnectionResult[A]] {.async.} =
proc connect[A](s: UtpSocket[A]): Future[ConnectionResult[A]] =
debug "Initiating connection", dst = s.socketKey
# Create inner future, to make sure we are installing cancelCallback
# on whole connection future, and not only part of it
let connectionFuture = s.innerConnect()
connectionFuture.cancelCallback = proc(udata: pointer) {.gcsafe.} =
debug "Connection cancel callback fired",
socketKey = s.socketKey
# if for some reason the future is cancelled, destroy socket to clear it
# from the active socket list
s.destroy()
return connectionFuture
s.innerConnect()
proc socketAlreadyExists[A](): ConnectionResult[A] =
return err(OutgoingConnectionError(kind: SocketAlreadyExists))