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

refactor(currency_conversion): re frame the currency_conversion crate to make api calls on background thread #6906

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

Conversation

prajjwalkumar17
Copy link
Contributor

@prajjwalkumar17 prajjwalkumar17 commented Dec 20, 2024

Type of Change

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

Description

Due to redis lock being held for a long time, which made the forex api-calls synchronous, we had to re-frame the way how things were being operated for forex rates fetch, The changes includes:

  1. Acquiring redis lock, just before api-call and releasing it once data is written on redis.
  2. Always responding back with stale-data and operating everything (redis-write, update, api call) on background thread.
  3. Throwing error, if api_keys are missing in configs, whenever call is being made.
  4. Greater log coverage.

Additional Changes

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

Motivation and Context

How did you test it?

  1. Testing for first call when there is no data present in cache as well as redis:
    Screenshot 2025-01-03 at 4 35 53 PM
curl --location 'http://127.0.0.1:8080/forex/rates' \
--header 'api-key: xxxxxxx' \
--data ''
Screenshot 2025-01-03 at 5 13 35 PM
  1. Testing for the case, when data is present in cache and/or redis:
Screenshot 2025-01-03 at 5 14 59 PM
  1. For cases when data is expired in cache, it will provide us with the stale data and a background thread will be spun which will update the data on cache as well as redis.

Full response:

{
    "data": {
        "base_currency": "USD",
        "conversion": {
            "TWD": {
                "to_factor": "32.939501",
                "from_factor": "0.0303586869758591667797274767"
            },
           ......
            "MOP": {
                "to_factor": "8.011576",
                "from_factor": "0.1248193863479545098242842607"
            }
        }
    },
    "timestamp": 1735904610
}

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

@prajjwalkumar17 prajjwalkumar17 self-assigned this Dec 20, 2024
@prajjwalkumar17 prajjwalkumar17 requested a review from a team as a code owner December 20, 2024 06:29
Copy link

semanticdiff-com bot commented Dec 20, 2024

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/core/currency.rs  85% smaller
  crates/router/src/utils/currency.rs  33% smaller
  crates/router/src/configs/settings.rs  28% smaller
  config/config.example.toml Unsupported file format
  config/deployments/env_specific.toml Unsupported file format
  config/development.toml Unsupported file format
  config/docker_compose.toml Unsupported file format
  crates/analytics/docs/README.md Unsupported file format
  loadtest/config/development.toml Unsupported file format

@prajjwalkumar17 prajjwalkumar17 marked this pull request as draft December 20, 2024 06:30
@prajjwalkumar17 prajjwalkumar17 marked this pull request as ready for review January 3, 2025 11:59
@prajjwalkumar17 prajjwalkumar17 requested a review from a team as a code owner January 3, 2025 11:59
config/config.example.toml Outdated Show resolved Hide resolved
crates/router/src/configs/settings.rs Show resolved Hide resolved
crates/router/src/configs/settings.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
@prajjwalkumar17 prajjwalkumar17 requested a review from a team as a code owner January 6, 2025 10:31
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
crates/router/src/utils/currency.rs Outdated Show resolved Hide resolved
) -> CustomResult<FxExchangeRatesCacheEntry, ForexCacheError> {
if let Some(local_rates) = retrieve_forex_from_local().await {
if local_rates.is_expired(call_delay) {
// expired local data
handler_local_expired(state, call_delay, local_rates).await
logger::debug!("forex_log: Forex stored in cache is expired");
successive_fetch_and_save_forex(state, Some(local_rates)).await
Copy link
Contributor

Choose a reason for hiding this comment

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

rename this method as it is not successive anymore

@prajjwalkumar17 prajjwalkumar17 changed the title refactor(currency_conversion): re frame the crate to make api calls on background thread refactor(currency_conversion): re frame the currency_conversion crate to make api calls on background thread Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-currency-conversion Area: Currency Conversion C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor(currency_conversion): re-frame the crate to make api calls on background thread
2 participants