Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Robust_Connection.h
Go to the documentation of this file.
1#ifndef joedb_Robust_Connection_declared
2#define joedb_Robust_Connection_declared
3
6
7#include <ostream>
8
9namespace joedb
10{
11 /// @ref Server_Connection that automatically reconnects on error
12 ///
13 /// @ingroup concurrency
15 {
16 using clock = std::chrono::steady_clock;
17 using time_point = std::chrono::time_point<clock>;
18
19 private:
20 const Connector &connector;
21 std::ostream *log;
22 mutable std::unique_ptr<Channel> channel;
23
24 const Readonly_Journal *handshake_journal = nullptr;
25 Content_Check handshake_content_check = Content_Check::quick;
26
27 static constexpr auto period = std::chrono::seconds(5);
28 mutable time_point last_connection_time = clock::now() - period;
29
30 void log_exception(const std::exception *e) const;
31
32 protected:
33 mutable std::unique_ptr<Server_Connection> connection;
34
35 void reconnect(const std::exception *e) const;
36
37 template<typename F> auto try_until_success(const F &f) const
38 {
39 while (true)
40 {
41 try
42 {
43 return f();
44 }
45 catch (const std::exception &e)
46 {
47 reconnect(&e);
48 }
49 }
50 }
51
52 public:
53 Robust_Connection(const Connector &connector, std::ostream *log):
54 connector(connector),
55 log(log)
56 {
57 reconnect(nullptr);
58 }
59
60 size_t pread(char *data, size_t size, int64_t offset) const;
61
62 int64_t handshake
63 (
64 const Readonly_Journal &client_journal,
65 Content_Check content_check
66 ) override;
67
68 int64_t pull
69 (
70 Lock_Action lock_action,
71 Data_Transfer data_transfer,
72 Writable_Journal &client_journal,
73 std::chrono::milliseconds wait
74 ) override;
75
76 int64_t push
77 (
78 const Readonly_Journal &client_journal,
79 int64_t from_checkpoint,
80 int64_t until_checkpoint,
81 Unlock_Action unlock_action
82 ) override;
83
84 void unlock() override;
85 bool is_pullonly() const override {return connection->is_pullonly();}
86 };
87}
88
89#endif
Used by Robust_Connection to reconnect after an error.
Definition Connector.h:14
Server_Connection that automatically reconnects on error
bool is_pullonly() const override
void reconnect(const std::exception *e) const
int64_t pull(Lock_Action lock_action, Data_Transfer data_transfer, Writable_Journal &client_journal, std::chrono::milliseconds wait) override
Pull from the connection.
std::unique_ptr< Server_Connection > connection
size_t pread(char *data, size_t size, int64_t offset) const
int64_t handshake(const Readonly_Journal &client_journal, Content_Check content_check) override
Called during Client construction.
Robust_Connection(const Connector &connector, std::ostream *log)
auto try_until_success(const F &f) const
int64_t push(const Readonly_Journal &client_journal, int64_t from_checkpoint, int64_t until_checkpoint, Unlock_Action unlock_action) override
Push new data to the connection.
void unlock() override
Unlock the connection.
Data_Transfer
Definition Connection.h:27
Lock_Action
Definition Connection.h:34
Content_Check
Definition Connection.h:19
Unlock_Action
Definition Connection.h:41
Definition Blob.h:7