commit 3f0f843: [Feature] Implement address retry on connection failure

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Aug 22 12:14:09 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-08-22 13:07:44 +0100
URL: https://github.com/rspamd/rspamd/commit/3f0f843468f2c7922141748b04faf7faf34dcf1d (HEAD -> master)

[Feature] Implement address retry on connection failure

---
 src/libserver/maps/map.c | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/src/libserver/maps/map.c b/src/libserver/maps/map.c
index 712bdeaa8..c49896829 100644
--- a/src/libserver/maps/map.c
+++ b/src/libserver/maps/map.c
@@ -1223,21 +1223,23 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
 
 	if (cbd->stage == http_map_http_conn && cbd->addrs->len > 0) {
 		rspamd_ptr_array_shuffle (cbd->addrs);
+		gint idx = 0;
 		/*
 		 * For the existing addr we can just select any address as we have
 		 * data available
 		 */
 		if (cbd->map->nelts > 0 && rspamd_random_double_fast () > 0.5) {
 			/* Already shuffled, use whatever is the first */
-			cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, 0);
+			cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx);
 		}
 		else {
 			/* Always prefer IPv4 as IPv6 is almost all the time broken */
 			g_ptr_array_sort (cbd->addrs, rspamd_map_dns_address_sort_func);
-			cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, 0);
+			cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx);
 		}
 
-		msg_debug_map ("open http connection to %s",
+retry:
+		msg_debug_map ("try open http connection to %s",
 				rspamd_inet_address_to_string_pretty (cbd->addr));
 		cbd->conn = rspamd_http_connection_new_client (NULL,
 				NULL,
@@ -1250,14 +1252,29 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
 			write_http_request (cbd);
 		}
 		else {
-			cbd->periodic->errored = TRUE;
-			msg_err_map ("error reading %s(%s): "
-						 "connection with http server terminated incorrectly: %s",
-					cbd->bk->uri,
-					cbd->addr ? rspamd_inet_address_to_string_pretty (cbd->addr) : "",
-					strerror (errno));
-
-			rspamd_map_process_periodic (cbd->periodic);
+			if (idx < cbd->addrs->len - 1) {
+				/* We can retry */
+				idx++;
+				rspamd_inet_addr_t *prev_addr = cbd->addr;
+				cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx);
+				msg_info_map ("cannot connect to %s to get data for %s: %s, retry with %s",
+						rspamd_inet_address_to_string_pretty (prev_addr),
+						cbd->bk->uri,
+						strerror (errno),
+						rspamd_inet_address_to_string_pretty (cbd->addr));
+				goto retry;
+			}
+			else {
+				/* Nothing else left */
+				cbd->periodic->errored = TRUE;
+				msg_err_map ("error reading %s(%s): "
+							 "connection with http server terminated incorrectly: %s",
+						cbd->bk->uri,
+						cbd->addr ? rspamd_inet_address_to_string_pretty (cbd->addr) : "",
+						strerror (errno));
+
+				rspamd_map_process_periodic (cbd->periodic);
+			}
 		}
 	}
 


More information about the Commits mailing list