Skip to content

Commit d859b9c

Browse files
committed
Refactor ProxyContext to remove upstream_override and update traffic split plugin to use context-based upstream selection
1 parent f007db9 commit d859b9c

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/core/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ pub struct ProxyContext {
613613
/// Parameters extracted from the route pattern.
614614
/// Stored as Vec for better performance with small number of params (typical case).
615615
pub route_params: Option<Vec<(String, String)>>,
616-
// Override upstream selector for traffic splitting and similar use cases.
617-
pub upstream_override: Option<Arc<dyn UpstreamSelector>>,
618616
// Selected HTTP peer for the upstream request.
619617
pub peer: Option<Box<HttpPeer>>,
620618
/// Number of retry attempts so far.
@@ -636,7 +634,6 @@ impl Default for ProxyContext {
636634
Self {
637635
route: None,
638636
route_params: None,
639-
upstream_override: None,
640637
peer: None,
641638
tries: 0,
642639
plugin: ProxyPluginExecutor::default_shared(),

src/plugin/traffic_split.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::utils::request::request_selector_key;
1414
pub const PLUGIN_NAME: &str = "traffic-split";
1515
const PRIORITY: i32 = 966;
1616

17+
pub const CTX_KEY_UPSTREAM_OVERRIDE: &str = "pingsix_upstream_override";
18+
1719
#[derive(Debug, Serialize, Deserialize, Validate)]
1820
struct WeightedUpstream {
1921
pub upstream_id: Option<String>,
@@ -56,7 +58,7 @@ impl ProxyPlugin for PluginTrafficSplit {
5658
if self.match_vars(session, &rule.vars) {
5759
// 命中规则,执行加权选择
5860
if let Some(selected_ups) = self.pick_upstream(rule_idx, &rule.weighted_upstreams) {
59-
ctx.upstream_override = Some(selected_ups);
61+
ctx.set(CTX_KEY_UPSTREAM_OVERRIDE, selected_ups);
6062
}
6163
break; // 匹配到第一个规则即停止
6264
}

src/service/http.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ use pingora_proxy::{ProxyHttp, Session};
2424

2525
use crate::{
2626
config,
27-
core::{ProxyContext, ProxyError, ProxyPlugin, RouteContext},
28-
plugin::cache::{CacheSettings, CTX_KEY_CACHE_SETTINGS},
27+
core::{ProxyContext, ProxyError, ProxyPlugin, RouteContext, UpstreamSelector},
28+
plugin::{
29+
cache::{CacheSettings, CTX_KEY_CACHE_SETTINGS},
30+
traffic_split::CTX_KEY_UPSTREAM_OVERRIDE,
31+
},
2932
proxy::{global_rule::global_plugin_fetch, route::global_route_match_fetch},
3033
};
3134

@@ -120,7 +123,9 @@ impl ProxyHttp for HttpService {
120123
session: &mut Session,
121124
ctx: &mut Self::CTX,
122125
) -> Result<Box<HttpPeer>> {
123-
let peer = if let Some(ups_override) = ctx.upstream_override.as_ref() {
126+
let peer = if let Some(ups_override) = ctx
127+
.get::<Arc<dyn UpstreamSelector + 'static>>(CTX_KEY_UPSTREAM_OVERRIDE)
128+
{
124129
let mut backend = ups_override.select_backend(session).ok_or_else(|| {
125130
ProxyError::UpstreamSelection("Traffic-split selected no backend".to_string())
126131
})?;

0 commit comments

Comments
 (0)