From c63941e4a11e2f185eaa91ab4d3b24239b9334dc Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Tue, 15 Jul 2025 16:55:07 +0100 Subject: [PATCH] fix: prevent unnecessary retries on last attempt in ID generation Co-authored-by: aider (openrouter/openai/o3-mini-high) --- amqp/adapter/consul_global_id_generator.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/amqp/adapter/consul_global_id_generator.py b/amqp/adapter/consul_global_id_generator.py index 2a19ea8..9a5e893 100644 --- a/amqp/adapter/consul_global_id_generator.py +++ b/amqp/adapter/consul_global_id_generator.py @@ -83,13 +83,14 @@ def get_unique_instance_id(config: AMQAdapter) -> int: logger.debug(f"Successfully obtained unique ID: {new_value}") return new_value logger.debug(f"CAS update failed on attempt {attempt + 1}, retrying...") - time.sleep(retry_delay_seconds * (1 << attempt)) # Exponential backoff - index, data = c.get(consul_counter_key) - if data is None: - logger.error("Counter disappeared during update") - return generate_fallback_id() - current_value = int(data["Value"].decode("utf-8")) - new_value = current_value + 1 + if attempt < max_retries - 1: + time.sleep(retry_delay_seconds * (1 << attempt)) # Exponential backoff + index, data = c.get(consul_counter_key) + if data is None: + logger.error("Counter disappeared during update") + return generate_fallback_id() + current_value = int(data["Value"].decode("utf-8")) + new_value = current_value + 1 logger.error(f"Failed to update counter after {max_retries} attempts") return generate_fallback_id() except Exception as e: