This is an automated email from the ASF dual-hosted git repository.

nickva pushed a commit to branch improve-replicator-redirects
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit ab53906fca8ac90a034f82ee39d17afc61f2dc79
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Wed Sep 30 01:06:16 2026 -0400

    Improve redirects in replicator
    
    Limit replicator redirects to a few tries.
    
    Co-Authored-By: Robert Newson <[email protected]>
---
 src/couch_replicator/src/couch_replicator_httpc.erl | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/couch_replicator/src/couch_replicator_httpc.erl 
b/src/couch_replicator/src/couch_replicator_httpc.erl
index 7f4f43afd..762dae258 100644
--- a/src/couch_replicator/src/couch_replicator_httpc.erl
+++ b/src/couch_replicator/src/couch_replicator_httpc.erl
@@ -28,6 +28,7 @@
 
 -define(replace(L, K, V), lists:keystore(K, 1, L, {K, V})).
 -define(MAX_WAIT, 5 * 60 * 1000).
+-define(MAX_REDIRECTS, 5).
 -define(STREAM_STATUS, ibrowse_stream_status).
 -define(STOP_HTTP_WORKER, stop_http_worker).
 
@@ -465,10 +466,16 @@ query_args_to_string([], Acc) ->
 query_args_to_string([{K, V} | Rest], Acc) ->
     query_args_to_string(Rest, [K ++ "=" ++ couch_httpd:quote(V) | Acc]).
 
-do_redirect(_Worker, Code, Headers, #httpdb{url = Url} = HttpDb, Params, _Cb) 
->
-    RedirectUrl = redirect_url(Headers, Url),
-    {HttpDb2, Params2} = after_redirect(RedirectUrl, Code, HttpDb, Params),
-    throw({retry, HttpDb2, Params2}).
+do_redirect(Worker, Code, Headers, #httpdb{url = Url} = HttpDb, Params, _Cb) ->
+    Redirects = get_value(redirects, Params, 0),
+    case Redirects >= ?MAX_REDIRECTS of
+        true ->
+            report_error(Worker, HttpDb, Params, {error, too_many_redirects});
+        false ->
+            RedirectUrl = redirect_url(Headers, Url),
+            {HttpDb2, Params2} = after_redirect(RedirectUrl, Code, HttpDb, 
Params),
+            throw({retry, HttpDb2, ?replace(Params2, redirects, Redirects + 
1)})
+    end.
 
 redirect_url(RespHeaders, OrigUrl) ->
     MochiHeaders = mochiweb_headers:make(RespHeaders),

Reply via email to