Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(analytics): retry implementation for forex crate call #7280

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ShivanshMathurJuspay
Copy link
Contributor

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

Implementing a retry logic for forex crate call with linear time increment in retry counter

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

crates/router/src/core/currency.rs

Motivation and Context

This was required as the Analytics API was returning 5xx when forex was returning error which would eventually resolve on retry

How did you test it?

Added logs for the same

Screenshot 2025-02-17 at 12 26 37 PM

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

Copy link

semanticdiff-com bot commented Feb 17, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/core/currency.rs  11% smaller
  crates/router/src/consts.rs  0% smaller

@ShivanshMathurJuspay ShivanshMathurJuspay linked an issue Feb 17, 2025 that may be closed by this pull request
2 tasks
@ShivanshMathurJuspay ShivanshMathurJuspay changed the title bug(analytics): retry implementation for forex crate call bugfix(analytics): retry implementation for forex crate call Feb 17, 2025
@ShivanshMathurJuspay ShivanshMathurJuspay changed the title bugfix(analytics): retry implementation for forex crate call chores(analytics): retry implementation for forex crate call Feb 17, 2025
@ShivanshMathurJuspay ShivanshMathurJuspay changed the title chores(analytics): retry implementation for forex crate call fix(analytics): retry implementation for forex crate call Feb 17, 2025
Comment on lines 64 to 72
Err(error) => {
if attempt >= max_attempts {
logger::error!("Failed to fetch forex rates after {max_attempts} attempts");
return Err(error.change_context(AnalyticsError::ForexFetchFailed));
}
logger::warn!("Forex rates fetch failed, retrying in {attempt} seconds");
tokio::time::sleep(std::time::Duration::from_secs(attempt)).await;
attempt += 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of retrying on all error cases, should we match on the error type and retry only if its a retryable error? Something like failing to attain redis lock could be retried but incorrect forex creds error cannot be

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Redis lock is not there now on the function which we are invoking

let rates = get_forex_rates(&state, forex_api.call_delay)
.await
.change_context(AnalyticsError::ForexFetchFailed)?;
let max_attempts = 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

should this max_attempt be hard coded or should this be a config option, if hardcoded can we maintain in the consts.rs

@ShivanshMathurJuspay ShivanshMathurJuspay requested a review from a team as a code owner February 18, 2025 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Analytics giving 5xx on forex crate returning no data
3 participants