membphis commented on code in PR #12906:
URL: https://github.com/apache/apisix/pull/12906#discussion_r2693630237


##########
apisix/discovery/eureka/init.lua:
##########
@@ -145,26 +148,36 @@ local function fetch_full_registry(premature)
         return
     end
 
-    local request_uri, basic_auth = service_info()
-    if not request_uri then
+    local endpoints = build_endpoints()
+    if not endpoints or #endpoints == 0 then
         return
     end
 
-    local res, err = request(request_uri, basic_auth, "GET", "apps")
-    if not res then
-        log.error("failed to fetch registry", err)
-        return
+    -- try endpoints from random position, failover on error
+    local selected_endpoint
+    local selected_body
+    local num_endpoints = #endpoints
+    local start = math_random(num_endpoints)
+    for i = 0, num_endpoints - 1 do
+        local endpoint = endpoints[(start + i - 1) % num_endpoints + 1]
+        local r, e = request(endpoint.url, endpoint.auth, "GET", "apps")
+        if r and r.body and r.status == 200 then
+            selected_endpoint = endpoint
+            selected_body = r.body
+            break
+        end
+        log.warn("failed to fetch registry from ", endpoint.url, ": ",

Review Comment:
   should use error log level



##########
apisix/discovery/eureka/init.lua:
##########
@@ -145,26 +148,36 @@ local function fetch_full_registry(premature)
         return
     end
 
-    local request_uri, basic_auth = service_info()
-    if not request_uri then
+    local endpoints = build_endpoints()
+    if not endpoints or #endpoints == 0 then
         return
     end
 
-    local res, err = request(request_uri, basic_auth, "GET", "apps")
-    if not res then
-        log.error("failed to fetch registry", err)
-        return
+    -- try endpoints from random position, failover on error
+    local selected_endpoint
+    local selected_body
+    local num_endpoints = #endpoints
+    local start = math_random(num_endpoints)
+    for i = 0, num_endpoints - 1 do
+        local endpoint = endpoints[(start + i - 1) % num_endpoints + 1]

Review Comment:
   ```suggestion
           local endpoint = endpoints[(start + i) % num_endpoints]
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to