Skip to content

Configure Telegram API retries#528

Open
rob93c wants to merge 7 commits into
mainfrom
configure-telegram-api-retries
Open

Configure Telegram API retries#528
rob93c wants to merge 7 commits into
mainfrom
configure-telegram-api-retries

Conversation

@rob93c

@rob93c rob93c commented Jul 4, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of Telegram API interactions by retrying requests when the API reports a temporary failure with a specified retry_after delay (up to 3 attempts).
    • Retries now wait for the indicated period and stop cleanly if the wait is interrupted, reporting the final error details.
  • Tests
    • Added coverage to verify the retry flow by confirming a follow-up successful message is sent after an initial rate-limit style failure.

@rob93c rob93c requested a review from MartelliEnrico July 4, 2026 10:42
@rob93c rob93c self-assigned this Jul 4, 2026
@rob93c rob93c added the enhancement New feature or request label Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Stickerify.execute(...) now retries Telegram API responses that include retry_after, waits between attempts, and preserves interrupt status if sleeping is interrupted. Tests add a retryable failure mock and verify the retry path.

Changes

Execute Retry Behaviour

Layer / File(s) Summary
Add retry with delay to execute(...)
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java
Imports ResponseParameters, @Nullable, and Duration, then updates execute(...) to sleep for the reported retry-after interval, re-run the request up to three times, return on an OK retry, and otherwise throw TelegramApiException using the final response description. A private isRetriable(@nullable ResponseParameters) helper gates the retry path.
Add retry failure mock and test
src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java, src/test/java/com/github/stickerifier/stickerify/bot/StickerifyTest.java
Adds a failure mock response with a 429 payload and retry_after value, then adds a test that queues HELP, failure, and success responses, runs the bot, and verifies the second sendRichMessage call contains the HELP markdown payload.

Sequence Diagram(s)

sequenceDiagram
  participant Stickerify
  participant Telegram API

  Stickerify->>Telegram API: execute(request)
  Telegram API-->>Stickerify: non-OK response with retry_after
  Stickerify->>Stickerify: sleep(Duration.ofSeconds(retryAfter))
  Stickerify->>Telegram API: execute(request) retry
  Telegram API-->>Stickerify: retry response
  alt response OK
    Stickerify-->>Stickerify: return response
  else interrupted or still not OK
    Stickerify->>Stickerify: throw TelegramApiException
  end
Loading

Related issues: None specified.

Related PRs: None specified.

Suggested labels: enhancement, reliability

Suggested reviewers: None specified.

Poem

A request returns with “try again”,
so Stickerify waits, then sends it on;
if sleep is cut, it keeps its chain,
and ends with error when retries are gone.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding Telegram API retry handling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (1)

227-247: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Retry only transient Telegram failures. This helper sleeps for 15s and retries every non-OK response, which just delays permanent errors and can duplicate non-idempotent sends. Use response.parameters()/retry_after for 429 responses and fail fast for everything else.

🧹 Nitpick comments (1)
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (1)

234-245: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Magic number and no retry logging.

Duration.ofSeconds(15) is a hardcoded literal embedded in the method body; extracting it to a named constant (e.g. RETRY_DELAY) would make the intent and tunability clearer. Also consider logging the retry attempt (and eventual failure) for observability, since silent 15s delays followed by a thrown exception are otherwise hard to diagnose in production.

♻️ Suggested refactor
+	private static final Duration RETRY_DELAY = Duration.ofSeconds(15);
+
 	private <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequest<T, R> request) throws TelegramApiException {
 		var response = bot.execute(request);
 
 		if (response.isOk()) {
 			return response;
 		}
 
 		try {
-			Thread.sleep(Duration.ofSeconds(15));
+			LOGGER.at(Level.WARN).log("Retrying failed Telegram API request after delay");
+			Thread.sleep(RETRY_DELAY);

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a4ce9f04-5d46-49fa-ad11-d5a026ef2a66

📥 Commits

Reviewing files that changed from the base of the PR and between b411ee0 and fa74aa8.

📒 Files selected for processing (1)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

@rob93c

rob93c commented Jul 4, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (1)

227-253: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Add test coverage for the new retry behaviour.

This is a non-trivial control-flow change (retry count cap, conditional retry-after check, sleep, interrupt handling) with no accompanying test changes provided. StickerifyTest.java exists as the natural home for these cases (successful retry, exhausted retries, interrupt during sleep, no retry-after present).


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d7c0d709-4108-4ed7-9a8e-d1928eb4ad05

📥 Commits

Reviewing files that changed from the base of the PR and between fa74aa8 and 099bb67.

📒 Files selected for processing (1)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Comment thread src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java (1)

16-26: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Reduce retry_after to shrink real test wall-time.

Stickerify.execute() performs an actual Thread.sleep(Duration.ofSeconds(retryDelay)) on retry, so this mock's retry_after: 3 makes retryApiCall genuinely block for 3 real seconds every run. Since isRetriable only requires retryAfter > 0, using 1 instead of 3 still exercises the retry path while keeping the test suite faster.

♻️ Proposed fix
 	static final MockResponse FAILURE_RESPONSE = new MockResponse.Builder().body("""
 			{
 				ok: false,
 				error_code: 429,
-				description: "Too Many Requests: retry after 3",
+				description: "Too Many Requests: retry after 1",
 				parameters: {
-					retry_after: 3
+					retry_after: 1
 				}
 			}
 			""").build();

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f2a0e62d-15e4-414c-a1c2-3fd7f7f64bc5

📥 Commits

Reviewing files that changed from the base of the PR and between 099bb67 and ef68805.

📒 Files selected for processing (3)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java
  • src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java
  • src/test/java/com/github/stickerifier/stickerify/bot/StickerifyTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant