[PATCH] Asynchronous close event handling for single peer upstreams

J Carter jordanc.carter at outlook.com
Sun Apr 9 20:52:46 UTC 2023


Hello,

resubmitting with the changes suggested.

# HG changeset patch
# User jordanc.carter at outlook.com
# Date 1681067934 -3600
#      Sun Apr 09 20:18:54 2023 +0100
# Node ID c8dcf584b36505e42bd2ea2965c1020069adb677
# Parent  5f1d05a21287ba0290dd3a17ad501595b442a194
Asynchronous close event handling for single peer upstreams

Limits single peer upstreams to a single retry when consecutive
asynchronous close events are encountered.

diff -r 5f1d05a21287 -r c8dcf584b365 src/event/ngx_event_connect.h
--- a/src/event/ngx_event_connect.h	Tue Mar 28 18:01:54 2023
+0300 +++ b/src/event/ngx_event_connect.h	Sun Apr 09 20:18:54
2023 +0100 @@ -17,6 +17,7 @@
 #define NGX_PEER_KEEPALIVE           1
 #define NGX_PEER_NEXT                2
 #define NGX_PEER_FAILED              4
+#define NGX_PEER_ASYNC_FAILED        8
 
 
 typedef struct ngx_peer_connection_s  ngx_peer_connection_t;
@@ -64,6 +65,7 @@
     unsigned                         transparent:1;
     unsigned                         so_keepalive:1;
     unsigned                         down:1;
+    unsigned                         async_failed:1;
 
                                      /* ngx_connection_log_error_e */
     unsigned                         log_error:2;
diff -r 5f1d05a21287 -r c8dcf584b365 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Tue Mar 28 18:01:54 2023 +0300
+++ b/src/http/ngx_http_upstream.c	Sun Apr 09 20:18:54 2023 +0100
@@ -4317,6 +4317,9 @@
         {
             state = NGX_PEER_NEXT;
 
+        } else if (u->peer.cached && ft_type ==
NGX_HTTP_UPSTREAM_FT_ERROR) {
+            state = NGX_PEER_FAILED | NGX_PEER_ASYNC_FAILED;
+
         } else {
             state = NGX_PEER_FAILED;
         }
@@ -4330,11 +4333,6 @@
                       "upstream timed out");
     }
 
-    if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
-        /* TODO: inform balancer instead */
-        u->peer.tries++;
-    }
-
     switch (ft_type) {
 
     case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
@@ -4421,7 +4419,6 @@
             return;
         }
 #endif
-
         ngx_http_upstream_finalize_request(r, u, status);
         return;
     }
diff -r 5f1d05a21287 -r c8dcf584b365
src/http/ngx_http_upstream_round_robin.c ---
a/src/http/ngx_http_upstream_round_robin.c	Tue Mar 28 18:01:54
2023 +0300 +++ b/src/http/ngx_http_upstream_round_robin.c	Sun
Apr 09 20:18:54 2023 +0100 @@ -616,14 +616,14 @@
ngx_http_upstream_rr_peer_lock(rrp->peers, peer); 
     if (rrp->peers->single) {
-
-        peer->conns--;
+        pc->tries = 0;
 
-        ngx_http_upstream_rr_peer_unlock(rrp->peers, peer);
-        ngx_http_upstream_rr_peers_unlock(rrp->peers);
+        if (state & NGX_PEER_ASYNC_FAILED && pc->async_failed == 0) {
+            pc->tries = 2;
+            pc->async_failed = 1;
+        }
 
-        pc->tries = 0;
-        return;
+        goto cleanup;
     }
 
     if (state & NGX_PEER_FAILED) {
@@ -659,6 +659,7 @@
         }
     }
 
+    cleanup:
     peer->conns--;
 
     ngx_http_upstream_rr_peer_unlock(rrp->peers, peer);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hgexport
Type: application/octet-stream
Size: 3002 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20230409/2a113a51/attachment.obj>


More information about the nginx-devel mailing list