Skip to content

Commit

Permalink
Add Idle
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Nov 25, 2023
1 parent f750b9f commit ed59147
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <chrono>
#include <coroutine>
#include <exception>
#include <iostream>
Expand Down Expand Up @@ -66,7 +67,12 @@ NNet::TSimpleTask TRaftServer::InboundCounnection(NNet::TSocket socket) {
co_return;
}

NNet::TSimpleTask TRaftServer::Serve() {
void TRaftServer::Serve() {
Idle();
InboundServe();
}

NNet::TSimpleTask TRaftServer::InboundServe() {
Socket.Bind();
Socket.Listen();
while (true) {
Expand All @@ -75,3 +81,18 @@ NNet::TSimpleTask TRaftServer::Serve() {
}
co_return;
}

NNet::TSimpleTask TRaftServer::Idle() {
auto t0 = TimeSource->Now();
auto dt = std::chrono::milliseconds(2000);
auto sleep = std::chrono::milliseconds(10);
while (true) {
Raft->Process(NewTimeout());
auto t1 = TimeSource->Now();
if (t1 > t0 + dt) {
t0 = t1;
}
Poller.Sleep(sleep);
}
co_return;
}
16 changes: 14 additions & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <coroutine>

#include <all.hpp>

#include "timesource.h"
#include "messages.h"
#include "raft.h"
#include "socket.hpp"
Expand Down Expand Up @@ -106,21 +108,31 @@ class TWriter {

class TRaftServer {
public:
TRaftServer(NNet::TPoll& poller, NNet::TAddress address, const std::shared_ptr<TRaft>& raft, const TNodeDict& nodes)
TRaftServer(
NNet::TPoll& poller,
NNet::TAddress address,
const std::shared_ptr<TRaft>& raft,
const TNodeDict& nodes,
const std::shared_ptr<ITimeSource>& ts)
: Poller(poller)
, Socket(std::move(address), Poller)
, Raft(raft)
, Nodes(nodes)
, TimeSource(ts)
{ }

NNet::TSimpleTask Serve();
void Serve();

private:
NNet::TSimpleTask InboundServe();
NNet::TSimpleTask Idle();

NNet::TSimpleTask InboundCounnection(NNet::TSocket socket);
NNet::TTestTask Connector(std::shared_ptr<INode> node);

NNet::TPoll& Poller;
NNet::TPoll::TSocket Socket;
std::shared_ptr<TRaft> Raft;
TNodeDict Nodes;
std::shared_ptr<ITimeSource> TimeSource;
};

0 comments on commit ed59147

Please sign in to comment.