@@ -107,47 +107,66 @@ static void reconnect(Shub* shub)
107
107
unsigned addrs [128 ];
108
108
unsigned i , n_addrs = sizeof (addrs ) / sizeof (addrs [0 ]);
109
109
int max_attempts = shub -> params -> max_attempts ;
110
-
110
+ char * host = ( char * ) shub -> params -> host ;
111
111
if (shub -> output >= 0 ) {
112
112
close_socket (shub , shub -> output );
113
113
}
114
114
115
115
sock_inet .sin_family = AF_INET ;
116
116
sock_inet .sin_port = htons (shub -> params -> port );
117
- if (!resolve_host_by_name (shub -> params -> host , addrs , & n_addrs )) {
118
- shub -> params -> error_handler ("Failed to resolve host by name" , SHUB_FATAL_ERROR );
119
- }
120
- shub -> output = socket (AF_INET , SOCK_STREAM , 0 );
121
- if (shub -> output < 0 ) {
122
- shub -> params -> error_handler ("Failed to create inet socket" , SHUB_FATAL_ERROR );
123
- }
117
+
124
118
while (1 ) {
125
- int rc = -1 ;
126
- for (i = 0 ; i < n_addrs ; ++ i ) {
127
- memcpy (& sock_inet .sin_addr , & addrs [i ], sizeof sock_inet .sin_addr );
128
- do {
129
- rc = connect (shub -> output , (struct sockaddr * )& sock_inet , sizeof (sock_inet ));
130
- } while (rc < 0 && errno == EINTR );
131
-
132
- if (rc >= 0 || errno == EINPROGRESS ) {
133
- break ;
134
- }
119
+ char * sep = strchr (host , ',' );
120
+ ShubErrorSeverity severity = SHUB_FATAL_ERROR ;
121
+ if (sep != NULL ) {
122
+ * sep = '\0' ;
123
+ severity = SHUB_RECOVERABLE_ERROR ;
124
+ }
125
+ if (!resolve_host_by_name (host , addrs , & n_addrs )) {
126
+ shub -> params -> error_handler ("Failed to resolve host by name" , severity );
127
+ goto TryNextHost ;
128
+ }
129
+ shub -> output = socket (AF_INET , SOCK_STREAM , 0 );
130
+ if (shub -> output < 0 ) {
131
+ shub -> params -> error_handler ("Failed to create inet socket" , severity );
132
+ goto TryNextHost ;
135
133
}
136
- if (rc < 0 ) {
137
- if (errno != ENOENT && errno != ECONNREFUSED && errno != EINPROGRESS ) {
138
- shub -> params -> error_handler ("Connection can not be establish" , SHUB_FATAL_ERROR );
134
+ while (1 ) {
135
+ int rc = -1 ;
136
+ for (i = 0 ; i < n_addrs ; ++ i ) {
137
+ memcpy (& sock_inet .sin_addr , & addrs [i ], sizeof sock_inet .sin_addr );
138
+ do {
139
+ rc = connect (shub -> output , (struct sockaddr * )& sock_inet , sizeof (sock_inet ));
140
+ } while (rc < 0 && errno == EINTR );
141
+
142
+ if (rc >= 0 || errno == EINPROGRESS ) {
143
+ break ;
144
+ }
145
+ }
146
+ if (rc < 0 ) {
147
+ if (errno != ENOENT && errno != ECONNREFUSED && errno != EINPROGRESS ) {
148
+ shub -> params -> error_handler ("Connection can not be establish" , severity );
149
+ goto TryNextHost ;
150
+ }
151
+ if (max_attempts -- != 0 ) {
152
+ sleep (1 );
153
+ } else {
154
+ shub -> params -> error_handler ("Failed to connect to host" , severity );
155
+ goto TryNextHost ;
156
+ }
157
+ } else {
158
+ int optval = 1 ;
159
+ setsockopt (shub -> output , IPPROTO_TCP , TCP_NODELAY , (char const * )& optval , sizeof (optval ));
160
+ FD_SET (shub -> output , & shub -> inset );
161
+ if (sep != NULL ) {
162
+ * sep = ',' ;
163
+ }
164
+ return ;
139
165
}
140
- if (max_attempts -- != 0 ) {
141
- sleep (1 );
142
- } else {
143
- shub -> params -> error_handler ("Failed to connect to host" , SHUB_FATAL_ERROR );
144
- }
145
- } else {
146
- int optval = 1 ;
147
- setsockopt (shub -> output , IPPROTO_TCP , TCP_NODELAY , (char const * )& optval , sizeof (optval ));
148
- FD_SET (shub -> output , & shub -> inset );
149
- break ;
150
166
}
167
+ TryNextHost :
168
+ * sep = ',' ;
169
+ host = sep + 1 ;
151
170
}
152
171
}
153
172
0 commit comments