@@ -60,10 +60,18 @@ void DialOptions::set_entity(boost::optional<std::string> entity) {
6060 auth_entity_ = std::move (entity);
6161}
6262
63+ void DialOptions::set_initial_connection_attempts (int attempts) {
64+ initial_connection_attempts_ = attempts;
65+ }
66+
6367void DialOptions::set_timeout (std::chrono::duration<float > timeout) {
6468 timeout_ = std::move (timeout);
6569}
6670
71+ void DialOptions::set_initial_connection_attempt_timeout (std::chrono::duration<float > timeout) {
72+ initial_connection_attempt_timeout_ = std::move (timeout);
73+ }
74+
6775const boost::optional<std::string>& DialOptions::entity () const {
6876 return auth_entity_;
6977}
@@ -72,10 +80,18 @@ const boost::optional<Credentials>& DialOptions::credentials() const {
7280 return credentials_;
7381}
7482
83+ int DialOptions::initial_connection_attempts () const {
84+ return initial_connection_attempts_;
85+ }
86+
7587const std::chrono::duration<float >& DialOptions::timeout () const {
7688 return timeout_;
7789}
7890
91+ const std::chrono::duration<float >& DialOptions::initial_connection_attempt_timeout () const {
92+ return initial_connection_attempt_timeout_;
93+ }
94+
7995void DialOptions::set_allow_insecure_downgrade (bool allow) {
8096 allow_insecure_downgrade_ = allow;
8197}
@@ -84,6 +100,34 @@ bool DialOptions::allows_insecure_downgrade() const {
84100 return allow_insecure_downgrade_;
85101}
86102
103+ std::shared_ptr<ViamChannel> ViamChannel::dial (const char * uri,
104+ const boost::optional<DialOptions>& options,
105+ bool initial_attempt) {
106+ if (!initial_attempt) {
107+ return dial (std::move (uri), options);
108+ }
109+
110+ DialOptions opts = options.get_value_or (DialOptions ());
111+ auto timeout = opts.timeout ();
112+ auto attempts_remaining = opts.initial_connection_attempts ();
113+ if (attempts_remaining == 0 ) {
114+ attempts_remaining = -1 ;
115+ }
116+ opts.set_timeout (opts.initial_connection_attempt_timeout ());
117+
118+ while (attempts_remaining > 0 ) {
119+ try {
120+ auto connection = dial (uri, opts);
121+ opts.set_timeout (timeout);
122+ return connection;
123+ } catch (...) {
124+ attempts_remaining -= 1 ;
125+ }
126+ }
127+
128+ throw Exception (ErrorCondition::k_connection, " Unable to establish connection" );
129+ }
130+
87131std::shared_ptr<ViamChannel> ViamChannel::dial (const char * uri,
88132 const boost::optional<DialOptions>& options) {
89133 void * ptr = init_rust_runtime ();
0 commit comments