From 1de2422cde2c98fa1c6863558b5c7a0591251976 Mon Sep 17 00:00:00 2001 From: Raycho Mukelov Date: Sat, 9 Sep 2023 21:07:12 +0300 Subject: [PATCH] Fixes --- misc/http_client.nim | 30 ++++++++++++++++++++++++++++++ raft/raft_api.nim | 12 ++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 misc/http_client.nim diff --git a/misc/http_client.nim b/misc/http_client.nim new file mode 100644 index 0000000..2da6316 --- /dev/null +++ b/misc/http_client.nim @@ -0,0 +1,30 @@ +# nim-raft +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import asyncdispatch, httpclient + +proc nonAsyncHttp() = + var client = newHttpClient() + echo "Non async started" + for i in 0 .. 30: + let resp = client.get("http://example.com") + echo resp.status + echo "Non async finished" + +proc asyncHttp() {.async.} = + var client = newAsyncHttpClient() + echo "Async started" + for i in 0 .. 30: + let resp = await client.get("http://example.com") + echo resp.status + echo "Async finished" + + +waitFor asyncHttp() +nonAsyncHttp() diff --git a/raft/raft_api.nim b/raft/raft_api.nim index 56bca9c..905e31a 100644 --- a/raft/raft_api.nim +++ b/raft/raft_api.nim @@ -29,7 +29,11 @@ proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateM proc new*[SmCommandType, SmStateType](T: type RaftNode[SmCommandType, SmStateType]; # Create New Raft Node id: RaftNodeId; peersIds: seq[RaftNodeId]; # persistentStorage: RaftNodePersistentStorage, - msgSendCallback: RaftMessageSendCallback): T = + msgSendCallback: RaftMessageSendCallback, + electionTimeout: int=150, + heartBeatTimeout: int=180, + appendEntriesTimeout: int=150) +): T = var peers: RaftNodePeers @@ -39,7 +43,7 @@ proc new*[SmCommandType, SmStateType](T: type RaftNode[SmCommandType, SmStateTyp result = T( id: id, state: rnsFollower, currentTerm: 0, peers: peers, commitIndex: 0, lastApplied: 0, msgSendCallback: msgSendCallback, votedFor: DefaultUUID, currentLeaderId: DefaultUUID, - + electionTimeout: electionTimeout, heartBeatTimeout: heartBeatTimeout, appendEntriesTimeout: appendEntriesTimeout ) RaftNodeSmInit(result.stateMachine) @@ -118,7 +122,7 @@ template RaftTimerCreate(timerInterval: int, timerCallback: RaftTimerCallback): # Timers scheduling stuff etc. proc RaftNodeScheduleHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) = - node.heartBeatTimer = RaftTimerCreate(150, proc() = asyncSpawn RaftNodeSendHeartBeat(node)) + node.heartBeatTimer = RaftTimerCreate(node.heartBeatTimeout, proc() = asyncSpawn RaftNodeSendHeartBeat(node)) proc RaftNodeSendHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) {.async.} = debug "Raft Node sending Heart-Beat to peers", node_id=node.id @@ -134,7 +138,7 @@ proc RaftNodeSendHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommand RaftNodeScheduleHeartBeat(node) proc RaftNodeScheduleElectionTimeout*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) = - node.electionTimeoutTimer = RaftTimerCreate(150 + rand(150), proc = + node.electionTimeoutTimer = RaftTimerCreate(node.electionTimeout + rand(node.electionTimeout), proc = asyncSpawn RaftNodeStartElection(node) )