Skip to content

[codex] Lazy proxy traffic refresh#47

Merged
acking-you merged 2 commits into
masterfrom
fix/proxy-traffic-lazy-refresh
Jun 18, 2026
Merged

[codex] Lazy proxy traffic refresh#47
acking-you merged 2 commits into
masterfrom
fix/proxy-traffic-lazy-refresh

Conversation

@acking-you

Copy link
Copy Markdown
Owner

Summary

  • Stop eagerly computing proxy traffic when opening /admin/llm-gateway.
  • Add a persisted llm_proxy_config_traffic_snapshots Postgres table and return the last snapshot with proxy configs.
  • Add a per-proxy POST /admin/llm-gateway/proxy-configs/:proxy_id/traffic-refresh endpoint that computes a retained-window snapshot and saves it.
  • Update the admin UI to show the last cached traffic snapshot plus refresh time, with an explicit per-proxy refresh button.

Why

The settings page was doing an expensive all-proxy traffic calculation during normal page reloads/tab entry. That made /admin/llm-gateway slower and could amplify usage-worker load. The new behavior makes the expensive calculation opt-in per proxy while preserving the last calculated value for display.

Validation

  • CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p static-flow-frontend proxy_traffic_snapshot_helpers_show_uncalculated_and_persisted_badge_state --jobs 4
  • CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p static-flow-frontend proxy_traffic_window_label_matches_retained_analytics_window --jobs 4
  • CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p llm-access-core -p llm-access-migrations -p llm-access-store -p llm-access traffic --jobs 4
  • CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo clippy -p llm-access-core -p llm-access-store -p llm-access-migrations -p llm-access -p static-flow-frontend --jobs 4 -- -D warnings
  • git diff --check

Rollout Note

Run the new Postgres migration before relying on proxy config listing in production, because traffic_snapshot is loaded from the new snapshot table.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the ability to manually refresh and persist traffic snapshots for LLM gateway proxy configurations, spanning frontend UI components, database migrations, and backend API endpoints. The review feedback highlights several performance improvements, such as returning early on empty lists, removing redundant database queries, and reusing a shared reqwest::Client to prevent socket exhaustion. Additionally, the feedback recommends translating several Chinese UI strings, feedback messages, and test assertions to English to maintain UI consistency.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +88 to +97
async fn apply_proxy_traffic_snapshots_to_configs(
&self,
proxies: &mut [AdminProxyConfig],
) -> anyhow::Result<()> {
let snapshots = self.list_proxy_traffic_snapshots().await?;
for proxy in proxies {
proxy.traffic_snapshot = snapshots.get(&proxy.id).cloned();
}
Ok(())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To prevent redundant database round-trips, we should return early if the input proxies list is empty. This avoids executing the list_proxy_traffic_snapshots query when there are no proxies to update.

    async fn apply_proxy_traffic_snapshots_to_configs(
        &self,
        proxies: &mut [AdminProxyConfig],
    ) -> anyhow::Result<()> {
        if proxies.is_empty() {
            return Ok(());
        }
        let snapshots = self.list_proxy_traffic_snapshots().await?;
        for proxy in proxies {
            proxy.traffic_snapshot = snapshots.get(&proxy.id).cloned();
        }
        Ok(())
    }
References
  1. For functions performing database queries or cache invalidations based on a list of IDs, return early if the input list is empty to prevent redundant database round-trips and cache operations.

Comment on lines +1454 to +1462
match state
.admin_proxy_store
.get_admin_proxy_config(&proxy_id)
.await
{
Ok(Some(_)) => {},
Ok(None) => return not_found("LLM gateway proxy config not found").into_response(),
Err(_) => return internal_error("Failed to load llm gateway proxy config").into_response(),
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The existence check get_admin_proxy_config is redundant here because the subsequent function record_admin_proxy_traffic_snapshot already performs the same existence check internally and returns None if the proxy config is not found. Removing this pre-check avoids an unnecessary database query.

References
  1. Avoid redundant database queries by ensuring that a subsequent function (such as a patch or update function) does not already perform the same existence check internally before calling it.

Comment thread crates/llm-access/src/admin.rs Outdated
Comment on lines +4895 to +4899
let response = reqwest::Client::new()
.get(&url)
.timeout(USAGE_WORKER_QUERY_TIMEOUT)
.send()
.await

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instantiating a new reqwest::Client on every request prevents connection pooling and can lead to socket exhaustion under high load. Consider reusing a shared reqwest::Client (e.g., by storing it in HttpState or using a thread-safe shared client) to improve performance and efficiency.

proxy_traffic_window_label(snapshot.retention_days),
format_optional_bytes(Some(snapshot.totals.total_bytes))
),
None => "流量未计算".to_string(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The string "流量未计算" is in Chinese, which is inconsistent with the rest of the English UI strings in this file (such as "traffic refreshed" and "traffic not calculated"). We should translate it to English to maintain UI consistency.

        None => "traffic not calculated".to_string(),

Comment on lines +2189 to +2194
feedback.set(Some("流量已刷新".to_string()));
on_flash.emit(("已刷新代理流量".to_string(), false));
},
Err(err) => {
feedback.set(Some(err.clone()));
on_flash.emit((format!("刷新代理流量失败\n{err}"), true));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The feedback and flash messages are in Chinese, which is inconsistent with the rest of the English admin UI. We should translate them to English to maintain UI consistency.

                        feedback.set(Some("Traffic refreshed".to_string()));
                        on_flash.emit(("Refreshed proxy traffic".to_string(), false));
                    },
                    Err(err) => {
                        feedback.set(Some(err.clone()));
                        on_flash.emit((format!("Failed to refresh proxy traffic\n{err}"), true));

onclick={on_refresh_traffic}
disabled={*refreshing_traffic}
>
{ if *refreshing_traffic { "计算中..." } else { "刷新流量" } }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The button text is in Chinese, which is inconsistent with the rest of the English admin UI. We should translate it to English to maintain UI consistency.

                    { if *refreshing_traffic { "Calculating..." } else { "Refresh Traffic" } }

Comment on lines +9926 to +9927
assert_eq!(proxy_traffic_snapshot_badge(None), "流量未计算");
assert_eq!(proxy_traffic_snapshot_meta(None), "traffic not calculated");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Update the test assertion to match the English translation of the badge state.

        assert_eq!(proxy_traffic_snapshot_badge(None), "traffic not calculated");
        assert_eq!(proxy_traffic_snapshot_meta(None), "traffic not calculated");

@acking-you acking-you marked this pull request as ready for review June 18, 2026 18:33
@acking-you acking-you merged commit 431a0c9 into master Jun 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant