mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Add tags cache to app.proxyman.ohm.Select() (#2927)
* Add tags cache to ohm.Select(). * Refactor round-robin. * Fix a bug. --------- Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
parent
0ea2a50264
commit
7f7f57d3b6
2 changed files with 22 additions and 24 deletions
|
@ -2,7 +2,6 @@ package router
|
|||
|
||||
import (
|
||||
"context"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
|
||||
"github.com/xtls/xray-core/common/dice"
|
||||
|
@ -26,35 +25,20 @@ func (s *RandomStrategy) PickOutbound(tags []string) string {
|
|||
}
|
||||
|
||||
type RoundRobinStrategy struct {
|
||||
mu sync.Mutex
|
||||
tags []string
|
||||
index int
|
||||
roundRobin *RoundRobinStrategy
|
||||
}
|
||||
|
||||
func NewRoundRobin(tags []string) *RoundRobinStrategy {
|
||||
return &RoundRobinStrategy{
|
||||
tags: tags,
|
||||
}
|
||||
}
|
||||
func (r *RoundRobinStrategy) NextTag() string {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
tags := r.tags[r.index]
|
||||
r.index = (r.index + 1) % len(r.tags)
|
||||
return tags
|
||||
mu sync.Mutex
|
||||
index int
|
||||
}
|
||||
|
||||
func (s *RoundRobinStrategy) PickOutbound(tags []string) string {
|
||||
if len(tags) == 0 {
|
||||
n := len(tags)
|
||||
if n == 0 {
|
||||
panic("0 tags")
|
||||
}
|
||||
if s.roundRobin == nil || !reflect.DeepEqual(s.roundRobin.tags, tags) {
|
||||
s.roundRobin = NewRoundRobin(tags)
|
||||
}
|
||||
tag := s.roundRobin.NextTag()
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
tag := tags[s.index%n]
|
||||
s.index = (s.index + 1) % n
|
||||
return tag
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue