Joedb 10.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
7
8namespace joedb
9{
10 /// @ref Server_Connection that automatically reconnects on error
11 ///
12 /// @ingroup concurrency
14 {
15 using clock = std::chrono::steady_clock;
16 using time_point = std::chrono::time_point<clock>;
17
18 private:
19 const Connector &connector;
20 Logger &logger;
21 mutable std::unique_ptr<Channel> channel;
22
23 const Readonly_Journal *handshake_journal = nullptr;
24 Content_Check handshake_content_check = Content_Check::fast;
25
26 static constexpr auto period = std::chrono::seconds(5);
27 mutable time_point last_connection_time = clock::now() - period;
28
29 void log_exception(const std::exception *e) const;
30
31 protected:
32 mutable std::unique_ptr<Server_Connection> connection;
33
34 void reconnect(const std::exception *e) const;
35
36 template<typename F> auto try_until_success(const F &f) const
37 {
38 while (true)
39 {
40 try
41 {
42 return f();
43 }
44 catch (const Disconnection &e)
45 {
46 throw;
47 }
48 catch (std::exception &e)
49 {
50 reconnect(&e);
51 }
52 }
53 }
54
55 public:
56 Robust_Connection(const Connector &connector, Logger &logger):
57 connector(connector),
58 logger(logger)
59 {
60 reconnect(nullptr);
61 }
62
63 Channel &get_channel() {return *channel;}
64
65 size_t pread(char *data, size_t size, int64_t offset) const;
66
67 int64_t handshake
68 (
69 const Readonly_Journal &client_journal,
70 Content_Check content_check
71 ) override;
72
74 (
75 const Readonly_Journal &client_journal,
76 Content_Check content_check,
77 int64_t from,
78 int64_t until
79 ) override;
80
81 int64_t pull
82 (
83 Lock_Action lock_action,
84 Data_Transfer data_transfer,
85 Writable_Journal &client_journal,
86 std::chrono::milliseconds wait
87 ) override;
88
89 int64_t push
90 (
91 const Readonly_Journal &client_journal,
92 int64_t from_checkpoint,
93 int64_t until_checkpoint,
94 Unlock_Action unlock_action
95 ) override;
96
97 void ping();
98
99 void unlock() override;
100 bool is_pullonly() const override {return connection->is_pullonly();}
101 };
102}
103
104#endif
Connection()=default
Used by Robust_Connection to reconnect after an error.
Definition Connector.h:15
joedb::Robust_Connection does not try to reconnect when thrown
bool is_pullonly() const override
void reconnect(const std::exception *e) const
void check_matching_content(const Readonly_Journal &client_journal, Content_Check content_check, int64_t from, int64_t until) override
Compare content of file and connection. Throw if mismatch.
Robust_Connection(const Connector &connector, Logger &logger)
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.
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:28
Lock_Action
Definition Connection.h:35
Content_Check
Definition Connection.h:20
Unlock_Action
Definition Connection.h:42