Joedb 10.2.1
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 && log)
8 *log << "Robust_Connection: " << e->what() << std::endl;
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 log,
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 Lock_Action lock_action,
74 Data_Transfer data_transfer,
75 Writable_Journal &client_journal,
76 std::chrono::milliseconds wait
77 )
78 {
79 return try_until_success([&]()
80 {
81 return connection->pull(lock_action, data_transfer, client_journal, wait);
82 });
83 }
84
86 (
87 const Readonly_Journal &client_journal,
88 int64_t from_checkpoint,
89 int64_t until_checkpoint,
90 Unlock_Action unlock_action
91 )
92 {
93 return try_until_success([&]()
94 {
95 return connection->push
96 (
97 client_journal,
98 from_checkpoint,
99 until_checkpoint,
100 unlock_action
101 );
102 });
103 }
104
106 {
107 try
108 {
109 connection->unlock();
110 }
111 catch (const std::exception &e)
112 {
113 if (log)
114 *log << "Robust_Connection::unlock() error: " << e.what() << '\n';
115 }
116 }
117}
std::chrono::milliseconds get_keep_alive_interval() const
Definition Connector.h:23
virtual std::unique_ptr< Channel > new_channel() const =0
joedb::Robust_Connection does not try to reconnect when thrown
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.
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