@@ -161,14 +161,14 @@ class coro_rpc_client {
161
161
" client has been closed" };
162
162
struct config {
163
163
uint64_t client_id = get_global_client_id();
164
- std::chrono::milliseconds timeout_duration =
165
- std::chrono::milliseconds{ 30000 } ;
166
- std::string host;
167
- std::string port;
164
+ std::optional<std:: chrono::milliseconds> connect_timeout_duration;
165
+ std::optional<std:: chrono::milliseconds> request_timeout_duration ;
166
+ std::string host{} ;
167
+ std::string port{} ;
168
168
bool enable_tcp_no_delay = true ;
169
169
#ifdef YLT_ENABLE_SSL
170
- std::filesystem::path ssl_cert_path;
171
- std::string ssl_domain;
170
+ std::filesystem::path ssl_cert_path{} ;
171
+ std::string ssl_domain{} ;
172
172
#endif
173
173
};
174
174
@@ -203,6 +203,8 @@ class coro_rpc_client {
203
203
204
204
const config &get_config () const { return config_; }
205
205
206
+ config &get_config () { return config_; }
207
+
206
208
[[nodiscard]] bool init_config (const config &conf) {
207
209
config_ = conf;
208
210
#ifdef YLT_ENABLE_SSL
@@ -228,57 +230,45 @@ class coro_rpc_client {
228
230
*
229
231
* @param host server address
230
232
* @param port server port
231
- * @param timeout_duration RPC call timeout
233
+ * @param connect_timeout_duration RPC call timeout seconds
232
234
* @return error code
233
235
*/
234
236
[[nodiscard]] async_simple::coro::Lazy<coro_rpc::err_code> connect (
235
237
std::string host, std::string port,
236
- std::chrono::steady_clock::duration timeout_duration =
238
+ std::chrono::steady_clock::duration connect_timeout_duration =
237
239
std::chrono::seconds (30 )) {
238
240
auto lock_ok = connect_mutex_.tryLock ();
239
241
if (!lock_ok) {
240
242
co_await connect_mutex_.coScopedLock ();
241
243
co_return err_code{};
242
244
// do nothing, someone has reconnect the client
243
245
}
244
- config_.host = std::move (host);
245
- config_.port = std::move (port);
246
- config_.timeout_duration =
247
- std::chrono::duration_cast<std::chrono::milliseconds>(timeout_duration);
246
+
247
+ if (config_.host .empty ()) {
248
+ config_.host = std::move (host);
249
+ }
250
+ if (config_.port .empty ()) {
251
+ config_.port = std::move (port);
252
+ }
253
+ if (!config_.connect_timeout_duration ) {
254
+ config_.connect_timeout_duration =
255
+ std::chrono::duration_cast<std::chrono::milliseconds>(
256
+ connect_timeout_duration);
257
+ }
258
+
248
259
auto ret = co_await connect_impl ();
249
260
connect_mutex_.unlock ();
250
261
co_return std::move (ret);
251
262
}
252
263
[[nodiscard]] async_simple::coro::Lazy<coro_rpc::err_code> connect (
253
264
std::string_view endpoint,
254
- std::chrono::steady_clock::duration timeout_duration =
265
+ std::chrono::steady_clock::duration connect_timeout_duration =
255
266
std::chrono::seconds (30 )) {
256
267
auto pos = endpoint.find (' :' );
257
- auto lock_ok = connect_mutex_.tryLock ();
258
- if (!lock_ok) {
259
- co_await connect_mutex_.coScopedLock ();
260
- co_return err_code{};
261
- // do nothing, someone has reconnect the client
262
- }
263
- config_.host = endpoint.substr (0 , pos);
264
- config_.port = endpoint.substr (pos + 1 );
265
- config_.timeout_duration =
266
- std::chrono::duration_cast<std::chrono::milliseconds>(timeout_duration);
267
- auto ret = co_await connect_impl ();
268
- connect_mutex_.unlock ();
269
- co_return std::move (ret);
270
- }
268
+ std::string host (endpoint.substr (0 , pos));
269
+ std::string port (endpoint.substr (pos + 1 ));
271
270
272
- [[nodiscard]] async_simple::coro::Lazy<coro_rpc::err_code> connect () {
273
- auto lock_ok = connect_mutex_.tryLock ();
274
- if (!lock_ok) {
275
- co_await connect_mutex_.coScopedLock ();
276
- co_return err_code{};
277
- // do nothing, someone has reconnect the client
278
- }
279
- auto ret = co_await connect_impl ();
280
- connect_mutex_.unlock ();
281
- co_return std::move (ret);
271
+ return connect (std::move (host), std::move (port), connect_timeout_duration);
282
272
}
283
273
284
274
#ifdef YLT_ENABLE_SSL
@@ -323,11 +313,12 @@ class coro_rpc_client {
323
313
*/
324
314
template <auto func, typename ... Args>
325
315
async_simple::coro::Lazy<rpc_result<decltype(get_return_type<func>())>>
326
- call_for(auto duration , Args &&...args) {
316
+ call_for(auto request_timeout_duration , Args &&...args) {
327
317
using return_type = decltype (get_return_type<func>());
328
318
auto async_result =
329
319
co_await co_await send_request_for_with_attachment<func, Args...>(
330
- duration, req_attachment_, std::forward<Args>(args)...);
320
+ request_timeout_duration, req_attachment_,
321
+ std::forward<Args>(args)...);
331
322
req_attachment_ = {};
332
323
if (async_result) {
333
324
control_->resp_buffer_ = async_result->release_buffer ();
@@ -410,9 +401,12 @@ class coro_rpc_client {
410
401
411
402
ELOGV (INFO, " client_id %d begin to connect %s" , config_.client_id ,
412
403
config_.port .data ());
413
- timeout (*this ->timer_ , config_.timeout_duration , " connect timer canceled" )
414
- .start ([](auto &&) {
415
- });
404
+ auto conn_timeout_dur = *config_.connect_timeout_duration ;
405
+ if (conn_timeout_dur.count () >= 0 ) {
406
+ timeout (*this ->timer_ , conn_timeout_dur, " connect timer canceled" )
407
+ .start ([](auto &&) {
408
+ });
409
+ }
416
410
417
411
std::error_code ec = co_await coro_io::async_connect (
418
412
&control_->executor_ , control_->socket_ , config_.host , config_.port );
@@ -747,7 +741,7 @@ class coro_rpc_client {
747
741
private:
748
742
template <auto func, typename ... Args>
749
743
async_simple::coro::Lazy<rpc_error> send_request_for_impl (
750
- auto duration , uint32_t &id, coro_io::period_timer &timer,
744
+ auto request_timeout_duration , uint32_t &id, coro_io::period_timer &timer,
751
745
std::string_view attachment, Args &&...args) {
752
746
using R = decltype (get_return_type<func>());
753
747
@@ -766,9 +760,10 @@ class coro_rpc_client {
766
760
767
761
static_check<func, Args...>();
768
762
769
- if (duration.count () > 0 ) {
770
- timeout (timer, duration, " rpc call timer canceled" ).start ([](auto &&) {
771
- });
763
+ if (request_timeout_duration.count () >= 0 ) {
764
+ timeout (timer, request_timeout_duration, " rpc call timer canceled" )
765
+ .start ([](auto &&) {
766
+ });
772
767
}
773
768
774
769
#ifdef YLT_ENABLE_SSL
@@ -965,16 +960,20 @@ class coro_rpc_client {
965
960
template <auto func, typename ... Args>
966
961
async_simple::coro::Lazy<async_simple::coro::Lazy<
967
962
async_rpc_result<decltype (get_return_type<func>())>>>
968
- send_request_for_with_attachment (auto time_out_duration ,
963
+ send_request_for_with_attachment (auto request_timeout_duration ,
969
964
std::string_view request_attachment,
970
965
Args &&...args ) {
971
966
using rpc_return_t = decltype (get_return_type<func>());
972
967
recving_guard guard (control_.get ());
973
968
uint32_t id;
969
+ if (!config_.request_timeout_duration ) {
970
+ config_.request_timeout_duration = request_timeout_duration;
971
+ }
972
+
974
973
auto timer = std::make_unique<coro_io::period_timer>(
975
974
control_->executor_ .get_asio_executor ());
976
975
auto result = co_await send_request_for_impl<func>(
977
- time_out_duration , id, *timer, request_attachment,
976
+ *config_. request_timeout_duration , id, *timer, request_attachment,
978
977
std::forward<Args>(args)...);
979
978
auto &control = *control_;
980
979
if (!result) {
0 commit comments