mirror of https://github.com/status-im/nim-eth.git
use Moment for tracking timeouts (#25)
* use Moment for tracking timeouts * Use the new Duration type throughout RLPx
This commit is contained in:
parent
ba53f4bc9b
commit
cbe67720ce
|
@ -128,7 +128,7 @@ type
|
||||||
OutstandingRequest* = object
|
OutstandingRequest* = object
|
||||||
id*: int
|
id*: int
|
||||||
future*: FutureBase
|
future*: FutureBase
|
||||||
timeoutAt*: uint64
|
timeoutAt*: Moment
|
||||||
|
|
||||||
# Private types:
|
# Private types:
|
||||||
MessageHandlerDecorator* = proc(msgId: int, n: NimNode): NimNode
|
MessageHandlerDecorator* = proc(msgId: int, n: NimNode): NimNode
|
||||||
|
|
|
@ -12,7 +12,7 @@ logScope:
|
||||||
|
|
||||||
const
|
const
|
||||||
devp2pVersion* = 4
|
devp2pVersion* = 4
|
||||||
defaultReqTimeout = 10000
|
defaultReqTimeout = milliseconds(10000)
|
||||||
maxMsgSize = 1024 * 1024
|
maxMsgSize = 1024 * 1024
|
||||||
|
|
||||||
include p2p_tracing
|
include p2p_tracing
|
||||||
|
@ -298,13 +298,13 @@ proc send*[Msg](peer: Peer, msg: Msg): Future[void] =
|
||||||
peer.sendMsg rlpWriter.finish
|
peer.sendMsg rlpWriter.finish
|
||||||
|
|
||||||
proc registerRequest*(peer: Peer,
|
proc registerRequest*(peer: Peer,
|
||||||
timeout: int,
|
timeout: Duration,
|
||||||
responseFuture: FutureBase,
|
responseFuture: FutureBase,
|
||||||
responseMsgId: int): int =
|
responseMsgId: int): int =
|
||||||
inc peer.lastReqId
|
inc peer.lastReqId
|
||||||
result = peer.lastReqId
|
result = peer.lastReqId
|
||||||
|
|
||||||
let timeoutAt = fastEpochTime() + uint64(timeout)
|
let timeoutAt = Moment.fromNow(timeout)
|
||||||
let req = OutstandingRequest(id: result,
|
let req = OutstandingRequest(id: result,
|
||||||
future: responseFuture,
|
future: responseFuture,
|
||||||
timeoutAt: timeoutAt)
|
timeoutAt: timeoutAt)
|
||||||
|
@ -371,7 +371,7 @@ proc resolveResponseFuture(peer: Peer, msgId: int, msg: pointer, reqId: int) =
|
||||||
template req: auto = outstandingReqs()[idx]
|
template req: auto = outstandingReqs()[idx]
|
||||||
|
|
||||||
if req.future.finished:
|
if req.future.finished:
|
||||||
doAssert req.timeoutAt <= fastEpochTime()
|
doAssert req.timeoutAt <= Moment.now()
|
||||||
# Here we'll remove the expired request by swapping
|
# Here we'll remove the expired request by swapping
|
||||||
# it with the last one in the deque (if necessary):
|
# it with the last one in the deque (if necessary):
|
||||||
if idx != outstandingReqs.len - 1:
|
if idx != outstandingReqs.len - 1:
|
||||||
|
@ -529,8 +529,9 @@ proc dispatchMessages*(peer: Peer) {.async.} =
|
||||||
macro p2pProtocolImpl(name: static[string],
|
macro p2pProtocolImpl(name: static[string],
|
||||||
version: static[uint],
|
version: static[uint],
|
||||||
body: untyped,
|
body: untyped,
|
||||||
|
# TODO Nim can't handle a proper duration paramter here
|
||||||
|
timeout: static[int64] = defaultReqTimeout.milliseconds,
|
||||||
useRequestIds: static[bool] = true,
|
useRequestIds: static[bool] = true,
|
||||||
timeout: static[int] = defaultReqTimeout,
|
|
||||||
shortName: static[string] = "",
|
shortName: static[string] = "",
|
||||||
outgoingRequestDecorator: untyped = nil,
|
outgoingRequestDecorator: untyped = nil,
|
||||||
incomingRequestDecorator: untyped = nil,
|
incomingRequestDecorator: untyped = nil,
|
||||||
|
@ -575,6 +576,8 @@ macro p2pProtocolImpl(name: static[string],
|
||||||
# Int = bindSym "int"
|
# Int = bindSym "int"
|
||||||
Int = ident "int"
|
Int = ident "int"
|
||||||
Peer = bindSym "Peer"
|
Peer = bindSym "Peer"
|
||||||
|
Duration = bindSym "Duration"
|
||||||
|
milliseconds = bindSym "milliseconds"
|
||||||
createNetworkState = bindSym "createNetworkState"
|
createNetworkState = bindSym "createNetworkState"
|
||||||
createPeerState = bindSym "createPeerState"
|
createPeerState = bindSym "createPeerState"
|
||||||
finish = bindSym "finish"
|
finish = bindSym "finish"
|
||||||
|
@ -725,7 +728,7 @@ macro p2pProtocolImpl(name: static[string],
|
||||||
if reqTimeout == nil:
|
if reqTimeout == nil:
|
||||||
reqTimeout = newTree(nnkIdentDefs,
|
reqTimeout = newTree(nnkIdentDefs,
|
||||||
ident"timeout",
|
ident"timeout",
|
||||||
Int, newLit(defaultTimeout))
|
Duration, newCall(milliseconds, newLit(defaultTimeout)))
|
||||||
|
|
||||||
let reqToResponseOffset = responseMsgId - msgId
|
let reqToResponseOffset = responseMsgId - msgId
|
||||||
let responseMsgId = quote do: `perPeerMsgIdVar` + `reqToResponseOffset`
|
let responseMsgId = quote do: `perPeerMsgIdVar` + `reqToResponseOffset`
|
||||||
|
@ -1102,11 +1105,11 @@ proc callDisconnectHandlers(peer: Peer, reason: DisconnectionReason): Future[voi
|
||||||
|
|
||||||
proc handshakeImpl(peer: Peer,
|
proc handshakeImpl(peer: Peer,
|
||||||
handshakeSendFut: Future[void],
|
handshakeSendFut: Future[void],
|
||||||
timeout: int,
|
timeout: Duration,
|
||||||
HandshakeType: type): Future[HandshakeType] {.async.} =
|
HandshakeType: type): Future[HandshakeType] {.async.} =
|
||||||
asyncCheck handshakeSendFut
|
asyncCheck handshakeSendFut
|
||||||
var response = nextMsg(peer, HandshakeType)
|
var response = nextMsg(peer, HandshakeType)
|
||||||
if timeout > 0:
|
if timeout.milliseconds > 0:
|
||||||
await response or sleepAsync(timeout)
|
await response or sleepAsync(timeout)
|
||||||
if not response.finished:
|
if not response.finished:
|
||||||
discard disconnectAndRaise(peer, BreachOfProtocol,
|
discard disconnectAndRaise(peer, BreachOfProtocol,
|
||||||
|
@ -1115,7 +1118,7 @@ proc handshakeImpl(peer: Peer,
|
||||||
discard await response
|
discard await response
|
||||||
return response.read
|
return response.read
|
||||||
|
|
||||||
macro handshake*(peer: Peer, timeout = 0, sendCall: untyped): untyped =
|
macro handshake*(peer: Peer, timeout: untyped, sendCall: untyped): untyped =
|
||||||
let
|
let
|
||||||
msgName = $sendCall[0]
|
msgName = $sendCall[0]
|
||||||
msgType = newDotExpr(ident"CurrentProtocol", ident(msgName))
|
msgType = newDotExpr(ident"CurrentProtocol", ident(msgName))
|
||||||
|
|
Loading…
Reference in New Issue