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

enhance(load balance): add StickySession load balance strategy #673

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

VendettaReborn
Copy link
Contributor

🤔 This is a ...

  • New feature
  • Bug fix
  • Performance optimization
  • Enhancement feature
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

🔗 Related issue link

enhancement in (#563)

💡 Background and solution

refer to clash.meta's solution

strategy details:

  • use the combination of src and dst ip addresses as the hash key
  • cache the chosen proxy's index in a lru_cache with 10m expire duration
  • always check the aliveness(by default is alive) before select the proxy

tests:

  • create NoopResolver and NoopOutboundHandler to create ProxyManager freely (shall i separate it into another PR?)

📝 Changelog

enhance(load balance): support sticky session strategy.

☑️ Self-Check before Merge

unitests in loadbalance/helpers.rs

  • Doc is updated/provided or not needed
  • Changelog is provided or not needed

@@ -63,3 +80,195 @@ pub fn strategy_consistent_hashring() -> StrategyFn {
)))
})
}

#[cfg(test)]
Copy link
Member

Choose a reason for hiding this comment

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

can we move this into mod tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TEST_LRU_STATE is coupled with the strategy fn, so i guess not.
for better readability, the #[cfg(test)] lines can be removed, but then there is basically no way to perform the unittest for the lru_cache state inside the closure.

index = jump_hash(key + timestamp(), buckets) as usize;
}
// TODO: if we should just remove the key from the cache?
lru_cache_clone.lock().await.insert(key, 0);
Copy link
Member

Choose a reason for hiding this comment

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

if no proxy is alive maybe we can just remove the entry and build the cache next time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think so, the current solution - use the first entry of proxies as fallback - is written by clash.meta.
it's basically ok, since the cached proxy will be checked before chosen as the return result

@VendettaReborn VendettaReborn enabled auto-merge (squash) January 16, 2025 06:54
@ibigbug ibigbug disabled auto-merge January 17, 2025 14:01
@ibigbug ibigbug merged commit 0587fa8 into Watfaq:master Jan 17, 2025
13 of 29 checks passed
lakako pushed a commit to lakako/clash-rs that referenced this pull request Jan 18, 2025
…q#673)

* feat: add StickySession load balance strategy

* move noopxxx to test_utils
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.

2 participants