Joedb 9.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 && 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>(*channel, log);
26 if (handshake_journal)
27 connection->handshake(*handshake_journal, handshake_content_check);
28 return;
29 }
30 catch (std::exception &reconnect_exception)
31 {
32 log_exception(&reconnect_exception);
33 }
34 }
35 }
36
37 size_t Robust_Connection::pread(char *data, size_t size, int64_t offset) const
38 {
39 return try_until_success([&]()
40 {
41 return connection->pread(data, size, offset);}
42 );
43 }
44
46 (
47 const Readonly_Journal &client_journal,
48 Content_Check content_check
49 )
50 {
51 const int64_t result = try_until_success([&]()
52 {
53 return connection->handshake(client_journal, content_check);
54 });
55
56 handshake_journal = &client_journal;
57 handshake_content_check = content_check;
58
59 return result;
60 }
61
63 (
64 Lock_Action lock_action,
65 Data_Transfer data_transfer,
66 Writable_Journal &client_journal,
67 std::chrono::milliseconds wait
68 )
69 {
70 return try_until_success([&]()
71 {
72 return connection->pull(lock_action, data_transfer, client_journal, wait);
73 });
74 }
75
77 (
78 const Readonly_Journal &client_journal,
79 int64_t from_checkpoint,
80 int64_t until_checkpoint,
81 Unlock_Action unlock_action
82 )
83 {
84 return try_until_success([&]()
85 {
86 return connection->push
87 (
88 client_journal,
89 from_checkpoint,
90 until_checkpoint,
91 unlock_action
92 );
93 });
94 }
95
97 {
98 try
99 {
100 connection->unlock();
101 }
102 catch (const std::exception &e)
103 {
104 if (log)
105 *log << "Robust_Connection::unlock() error: " << e.what() << '\n';
106 }
107 }
108}
virtual std::unique_ptr< Channel > new_channel() const =0
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:27
Lock_Action
Definition Connection.h:34
Content_Check
Definition Connection.h:19
Unlock_Action
Definition Connection.h:41
Definition Blob.h:7