Joedb 10.2.1
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
8#include <ostream>
9
10namespace joedb
11{
12 /// @ref Server_Connection that automatically reconnects on error
13 ///
14 /// @ingroup concurrency
16 {
17 using clock = std::chrono::steady_clock;
18 using time_point = std::chrono::time_point<clock>;
19
20 private:
21 const Connector &connector;
22 std::ostream *log;
23 mutable std::unique_ptr<Channel> channel;
24
25 const Readonly_Journal *handshake_journal = nullptr;
26 Content_Check handshake_content_check = Content_Check::fast;
27
28 static constexpr auto period = std::chrono::seconds(5);
29 mutable time_point last_connection_time = clock::now() - period;
30
31 void log_exception(const std::exception *e) const;
32
33 protected:
34 mutable std::unique_ptr<Server_Connection> connection;
35
36 void reconnect(const std::exception *e) const;
37
38 template<typename F> auto try_until_success(const F &f) const
39 {
40 while (true)
41 {
42 try
43 {
44 return f();
45 }
46 catch (const Disconnection &e)
47 {
48 throw;
49 }
50 catch (std::exception &e)
51 {
52 reconnect(&e);
53 }
54 }
55 }
56
57 public:
58 Robust_Connection(const Connector &connector, std::ostream *log):
59 connector(connector),
60 log(log)
61 {
62 reconnect(nullptr);
63 }
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
73 int64_t pull
74 (
75 Lock_Action lock_action,
76 Data_Transfer data_transfer,
77 Writable_Journal &client_journal,
78 std::chrono::milliseconds wait
79 ) override;
80
81 int64_t push
82 (
83 const Readonly_Journal &client_journal,
84 int64_t from_checkpoint,
85 int64_t until_checkpoint,
86 Unlock_Action unlock_action
87 ) override;
88
89 void unlock() override;
90 bool is_pullonly() const override {return connection->is_pullonly();}
91 };
92}
93
94#endif
Used by Robust_Connection to reconnect after an error.
Definition Connector.h:15
joedb::Robust_Connection does not try to reconnect when thrown
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:28
Lock_Action
Definition Connection.h:35
Content_Check
Definition Connection.h:20
Unlock_Action
Definition Connection.h:42