The function ngx_http_upstream_get_round_robin_peer in nginx will loop for a long period of times when pc->tries equals to 0.
Well then, under what conditions does pc->tries equal to zero?
When executing ngx_http_upstream_get_round_robin_peer for the first time, in some cases it returns NGX_OK while pc->tries equals to 1.
When in ngx_http_upstream_process_header, nginx process may enter the following:
1524 if (n == NGX_ERROR || n == 0) {
1525 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1526 return;
1527 }
then enter ngx_http_upstream_next function
In the ngx_http_upstream_next,nginx process will call ngx_http_upstream_free_round_robin_peer
2796 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
2797 u->peer.free(&u->peer, u->peer.data, state);
2798 }
we now enter ngx_http_upstream_free_round_robin_peer.
Pay attention to the following codes in the function:
if (pc->tries) {
pc->tries--;
}
After executing the above, pc->tries changes from 1 to 0.
then the process returns to ngx_http_upstream_next
2892 ngx_http_upstream_connect(r, u);
2893 }
we enter ngx_http_upstream_connect here:
this function will call
1081 rc = ngx_event_connect_peer(&u->peer);
...
It finally enter ngx_http_upstream_get_round_robin_peer again. And then the process will loop for a long time which is unpredictable.