mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +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
|
@ -22,12 +22,14 @@ type Manager struct {
|
|||
taggedHandler map[string]outbound.Handler
|
||||
untaggedHandlers []outbound.Handler
|
||||
running bool
|
||||
tagsCache map[string][]string
|
||||
}
|
||||
|
||||
// New creates a new Manager.
|
||||
func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error) {
|
||||
m := &Manager{
|
||||
taggedHandler: make(map[string]outbound.Handler),
|
||||
tagsCache: make(map[string][]string),
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
@ -104,6 +106,8 @@ func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) erro
|
|||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
m.tagsCache = make(map[string][]string)
|
||||
|
||||
if m.defaultHandler == nil {
|
||||
m.defaultHandler = handler
|
||||
}
|
||||
|
@ -133,6 +137,8 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
|||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
m.tagsCache = make(map[string][]string)
|
||||
|
||||
delete(m.taggedHandler, tag)
|
||||
if m.defaultHandler != nil && m.defaultHandler.Tag() == tag {
|
||||
m.defaultHandler = nil
|
||||
|
@ -146,6 +152,11 @@ func (m *Manager) Select(selectors []string) []string {
|
|||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
|
||||
key := strings.Join(selectors, ",")
|
||||
if cache, ok := m.tagsCache[key]; ok {
|
||||
return cache
|
||||
}
|
||||
|
||||
tags := make([]string, 0, len(selectors))
|
||||
|
||||
for tag := range m.taggedHandler {
|
||||
|
@ -156,7 +167,10 @@ func (m *Manager) Select(selectors []string) []string {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(tags)
|
||||
m.tagsCache[key] = tags
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue