Skip to content

Commit

Permalink
Connector
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Nov 25, 2023
1 parent 9085609 commit 39848cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ NNet::TTestTask TNode::DoDrain() {
co_return;
}

NNet::TTestTask TNode::DoConnect() {
while (!Connected) {
try {
auto deadline = TimeSource->Now() + std::chrono::milliseconds(15000);
co_await Socket.Connect(deadline);
Connected = true;
} catch (const std::exception& ex) {
std::cout << "Error on connect: " << ex.what() << "\n";
}
}
co_return;
}

NNet::TSimpleTask TRaftServer::InboundConnection(NNet::TSocket socket) {
try {
while (true) {
Expand Down
5 changes: 4 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,24 @@ class TWriter {

class TNode: public INode {
public:
TNode(NNet::TPoll& poller, uint32_t id, NNet::TAddress address)
TNode(NNet::TPoll& poller, uint32_t id, NNet::TAddress address, const std::shared_ptr<ITimeSource>& ts)
: Poller(poller)
, Id(id)
, Address(address)
, TimeSource(ts)
{ }

void Send(const TMessageHolder<TMessage>& message) override;
void Drain() override;

private:
NNet::TTestTask DoDrain();
NNet::TTestTask DoConnect();

NNet::TPoll& Poller;
uint32_t Id;
NNet::TAddress Address;
std::shared_ptr<ITimeSource> TimeSource;
NNet::TPoll::TSocket Socket;
bool Connected = false;

Expand Down
4 changes: 2 additions & 2 deletions src/timesource.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include <chrono>

struct ITimeSource {
using Time = std::chrono::time_point<std::chrono::system_clock>;
using Time = std::chrono::time_point<std::chrono::steady_clock>;
virtual ~ITimeSource() = default;
virtual Time Now() = 0;
};

class TTimeSource: public ITimeSource {
public:
Time Now() override {
return std::chrono::system_clock::now();
return std::chrono::steady_clock::now();
}
};
2 changes: 1 addition & 1 deletion test/test_raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TFakeNode: public INode {
class TFakeTimeSource: public ITimeSource {
public:
TFakeTimeSource()
: T(std::chrono::system_clock::now())
: T(std::chrono::steady_clock::now())
{ }

ITimeSource::Time Now() override {
Expand Down

0 comments on commit 39848cd

Please sign in to comment.