-
Notifications
You must be signed in to change notification settings - Fork 243
/
asio_service_options.hxx
290 lines (245 loc) · 8.39 KB
/
asio_service_options.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/************************************************************************
Copyright 2017-2019 eBay Inc.
Author/Developer(s): Jung-Sang Ahn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#pragma once
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <system_error>
typedef struct ssl_ctx_st SSL_CTX;
namespace nuraft {
class buffer;
class req_msg;
class resp_msg;
/**
* Parameters for meta callback functions in `options`.
*/
struct asio_service_meta_cb_params {
asio_service_meta_cb_params(int m = 0,
int s = 0,
int d = 0,
uint64_t t = 0,
uint64_t lt = 0,
uint64_t li = 0,
uint64_t ci = 0,
req_msg* req = nullptr,
resp_msg* resp = nullptr)
: msg_type_(m), src_id_(s), dst_id_(d)
, term_(t), log_term_(lt), log_idx_(li), commit_idx_(ci)
, req_(req), resp_(resp)
{}
/**
* Type of request.
*/
int msg_type_;
/**
* Source server ID that sends request.
*/
int src_id_;
/**
* Destination server ID that sends response.
*/
int dst_id_;
/**
* Term of source server.
*/
uint64_t term_;
/**
* Term of the corresponding log.
*/
uint64_t log_term_;
/**
* Log index number.
*/
uint64_t log_idx_;
/**
* Last committed index number.
*/
uint64_t commit_idx_;
/**
* Pointer to request instance.
*/
req_msg* req_;
/**
* Pointer to response instance.
* Will be given for `read_resp_meta_` and `write_resp_meta_` only.
*/
resp_msg* resp_;
};
/**
* Response callback function for customer resolvers.
*/
using asio_service_custom_resolver_response =
std::function< void(const std::string&, const std::string&, std::error_code) >;
/**
* Options used for initialization of Asio service.
*/
struct asio_service_options {
asio_service_options()
: thread_pool_size_(0)
, worker_start_(nullptr)
, worker_stop_(nullptr)
, enable_ssl_(false)
, skip_verification_(false)
, write_req_meta_(nullptr)
, read_req_meta_(nullptr)
, invoke_req_cb_on_empty_meta_(true)
, write_resp_meta_(nullptr)
, read_resp_meta_(nullptr)
, invoke_resp_cb_on_empty_meta_(true)
, verify_sn_(nullptr)
, custom_resolver_(nullptr)
, replicate_log_timestamp_(false)
, crc_on_entire_message_(false)
, crc_on_payload_(false)
, corrupted_msg_handler_(nullptr)
, streaming_mode_(false)
{}
/**
* Number of ASIO worker threads.
* If zero, it will be automatically set to number of cores.
*/
size_t thread_pool_size_;
/**
* Lifecycle callback function on worker thread start.
*/
std::function< void(uint32_t) > worker_start_;
/**
* Lifecycle callback function on worker thread stop.
*/
std::function< void(uint32_t) > worker_stop_;
/**
* If `true`, enable SSL/TLS secure connection.
*/
bool enable_ssl_;
/**
* If `true`, skip certificate verification.
*/
bool skip_verification_;
/**
* Path to server certificate file.
*/
std::string server_cert_file_;
/**
* Path to server key file.
*/
std::string server_key_file_;
/**
* Path to root certificate file.
*/
std::string root_cert_file_;
/**
* Callback function for writing Raft RPC request metadata.
*/
std::function< std::string(const asio_service_meta_cb_params&) > write_req_meta_;
/**
* Callback function for reading and verifying Raft RPC request metadata.
* If it returns `false`, the request will be discarded.
*/
std::function< bool( const asio_service_meta_cb_params&,
const std::string& ) > read_req_meta_;
/**
* If `true`, it will invoke `read_req_meta_` even though
* the received meta is empty.
*/
bool invoke_req_cb_on_empty_meta_;
/**
* Callback function for writing Raft RPC response metadata.
*/
std::function< std::string(const asio_service_meta_cb_params&) > write_resp_meta_;
/**
* Callback function for reading and verifying Raft RPC response metadata.
* If it returns false, the response will be ignored.
*/
std::function< bool( const asio_service_meta_cb_params&,
const std::string& ) > read_resp_meta_;
/**
* If `true`, it will invoke `read_resp_meta_` even though
* the received meta is empty.
*/
bool invoke_resp_cb_on_empty_meta_;
/**
* Callback function for verifying certificate subject name.
* If not given, subject name will not be verified.
*/
std::function< bool(const std::string&) > verify_sn_;
/**
* Callback function that provides pre-configured SSL_CTX.
* Asio takes ownership of the provided object
* and disposes it later with SSL_CTX_free.
*
* No configuration changes are applied to the provided context,
* so callback must return properly configured and operational SSL_CTX.
*
* Note that it might be unsafe to share SSL_CTX with other threads,
* consult with your OpenSSL library documentation/guidelines.
*/
std::function<SSL_CTX* (void)> ssl_context_provider_server_;
std::function<SSL_CTX* (void)> ssl_context_provider_client_;
/**
* Custom IP address resolver. If given, it will be invoked
* before the connection is established.
*
* If you want to selectively bypass some hosts, just pass the given
* host and port to the response function as they are.
*/
std::function< void( const std::string&,
const std::string&,
asio_service_custom_resolver_response ) > custom_resolver_;
/**
* If `true`, each log entry will contain timestamp when it was generated
* by the leader, and those timestamps will be replicated to all followers
* so that they will see the same timestamp for the same log entry.
*
* To support this feature, the log store implementation should be able to
* restore the timestamp when it reads log entries.
*
* This feature is not backward compatible. To enable this feature, there
* should not be any member running with old version before supporting
* this flag.
*/
bool replicate_log_timestamp_;
/**
* If `true`, NuRaft will validate the entire message with CRC.
* Otherwise, it validates the header part only.
*/
bool crc_on_entire_message_;
/**
* If `true`, each log entry will contain a CRC checksum of the entry's
* payload.
*
* To support this feature, the log store implementation should be able to
* store and retrieve the CRC checksum when it reads log entries.
*
* This feature is not backward compatible. To enable this feature, there
* should not be any member running with the old version before supporting
* this flag.
*/
bool crc_on_payload_;
/**
* Callback function that will be invoked when the received message is corrupted.
* The first `buffer` contains the raw binary of message header,
* and the second `buffer` contains the user payload including metadata,
* if it is not null.
*/
std::function< void( std::shared_ptr<buffer>,
std::shared_ptr<buffer> ) > corrupted_msg_handler_;
/**
* If `true`, NuRaft will use streaming mode, which allows it to send
* subsequent requests without waiting for the response to previous requests.
* The order of responses will be identical to the order of requests.
*/
bool streaming_mode_;
};
}