Joedb 10.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Robust_Connection.cpp
Go to the documentation of this file.
2
3namespace joedb
4{
5 void Robust_Connection::log_exception(const std::exception *e) const
6 {
7 if (e)
8 logger.log("Robust_Connection: exception: " + std::string(e->what()));
9 }
10
11 void Robust_Connection::reconnect(const std::exception *e) const
12 {
13 log_exception(e);
14
15 while (true)
16 {
17 std::this_thread::sleep_until(last_connection_time + period);
18 last_connection_time = clock::now();
19
20 try
21 {
22 connection.reset();
23 channel.reset();
24 channel = connector.new_channel();
25 connection = std::make_unique<Server_Connection>
26 (
27 *channel,
28 logger,
29 connector.get_keep_alive_interval()
30 );
31 if (handshake_journal)
32 connection->handshake(*handshake_journal, handshake_content_check);
33 return;
34 }
35 catch (const Disconnection &)
36 {
37 throw;
38 }
39 catch (std::exception &reconnect_exception)
40 {
41 log_exception(&reconnect_exception);
42 }
43 }
44 }
45
46 size_t Robust_Connection::pread(char *data, size_t size, int64_t offset) const
47 {
48 return try_until_success([&]()
49 {
50 return connection->pread(data, size, offset);}
51 );
52 }
53
55 (
56 const Readonly_Journal &client_journal,
57 Content_Check content_check
58 )
59 {
60 const int64_t result = try_until_success([&]()
61 {
62 return connection->handshake(client_journal, content_check);
63 });
64
65 handshake_journal = &client_journal;
66 handshake_content_check = content_check;
67
68 return result;
69 }
70
72 (
73 const Readonly_Journal &client_journal,
74 Content_Check content_check,
75 int64_t from,
76 int64_t until
77 )
78 {
80 {
81 connection->check_matching_content(client_journal, content_check, from, until);
82 });
83 }
84
86 (
87 Lock_Action lock_action,
88 Data_Transfer data_transfer,
89 Writable_Journal &client_journal,
90 std::chrono::milliseconds wait
91 )
92 {
93 return try_until_success([&]()
94 {
95 return connection->pull(lock_action, data_transfer, client_journal, wait);
96 });
97 }
98
100 (
101 const Readonly_Journal &client_journal,
102 int64_t from_checkpoint,
103 int64_t until_checkpoint,
104 Unlock_Action unlock_action
105 )
106 {
107 return try_until_success([&]()
108 {
109 return connection->push
110 (
111 client_journal,
112 from_checkpoint,
113 until_checkpoint,
114 unlock_action
115 );
116 });
117 }
118
120 {
121 try_until_success([&](){connection->ping();});
122 }
123
125 {
126 try
127 {
128 connection->unlock();
129 }
130 catch (const std::exception &e)
131 {
132 logger.log("Robust_Connection::unlock() error: " + std::string(e.what()));
133 }
134 }
135}
joedb::Robust_Connection does not try to reconnect when thrown
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.
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