Joedb 10.3.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 size_t pread(char *data, size_t size, int64_t offset) const;
64
65 int64_t handshake
66 (
67 const Readonly_Journal &client_journal,
68 Content_Check content_check
69 ) override;
70
71 int64_t pull
72 (
73 Lock_Action lock_action,
74 Data_Transfer data_transfer,
75 Writable_Journal &client_journal,
76 std::chrono::milliseconds wait
77 ) override;
78
79 int64_t push
80 (
81 const Readonly_Journal &client_journal,
82 int64_t from_checkpoint,
83 int64_t until_checkpoint,
84 Unlock_Action unlock_action
85 ) override;
86
87 void unlock() override;
88 bool is_pullonly() const override {return connection->is_pullonly();}
89 };
90}
91
92#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
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