Created
August 15, 2024 17:15
-
-
Save raninho/0e104c18bd7b9e35fdfdb206fa6f591c to your computer and use it in GitHub Desktop.
Retry simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
import random | |
# Parâmetros default | |
max_attempt = 5 | |
backoff_seconds = 18000 | |
backoff_factor = 2 | |
jitter_min_seconds = 0 | |
jitter_max_seconds = 300 | |
last_attempt_time_str = "25/01/2024 12:00:00" | |
current_attempt = 1 | |
print(f"A data da primeira execução foi em {last_attempt_time_str}") | |
# Convertendo last_attempt_time para um objeto datetime | |
last_attempt_time = datetime.strptime(last_attempt_time_str, "%d/%m/%Y %H:%M:%S") | |
while True: | |
# Calculando o tempo de espera com backoff exponencial | |
wait_time = backoff_seconds * (backoff_factor ** (current_attempt - 1)) | |
# Adicionando jitter aleatório | |
jitter = random.uniform(jitter_min_seconds, jitter_max_seconds) | |
total_wait_time = wait_time + jitter | |
# Calculando o tempo da próxima tentativa | |
next_attempt_time = last_attempt_time + timedelta(seconds=total_wait_time) | |
print(f"A data da tentativa de número {current_attempt} será em {next_attempt_time.strftime('%d/%m/%Y %H:%M:%S')}") | |
current_attempt = current_attempt + 1 | |
if current_attempt > max_attempt: | |
break | |
last_attempt_time = next_attempt_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment