mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-03 15:55:20 -05:00
incorporate changes in router implementation
This commit is contained in:
@@ -2,30 +2,9 @@ package router
|
||||
|
||||
import (
|
||||
sync "sync"
|
||||
"time"
|
||||
|
||||
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
||||
)
|
||||
|
||||
func (b *Balancer) overrideSelecting(selects []string, validity time.Duration) error {
|
||||
if validity <= 0 {
|
||||
b.override.Clear()
|
||||
return nil
|
||||
}
|
||||
hs, ok := b.ohm.(outbound.HandlerSelector)
|
||||
if !ok {
|
||||
return newError("outbound.Manager is not a HandlerSelector")
|
||||
}
|
||||
tags := hs.Select(selects)
|
||||
if len(tags) == 0 {
|
||||
return newError("no outbound selected")
|
||||
}
|
||||
b.override.Put(tags, time.Now().Add(validity))
|
||||
return nil
|
||||
}
|
||||
|
||||
// OverrideSelecting implements routing.BalancingOverrider
|
||||
func (r *Router) OverrideSelecting(balancer string, selects []string, validity time.Duration) error {
|
||||
func (r *Router) OverrideBalancer(balancer string, target string) error {
|
||||
var b *Balancer
|
||||
for tag, bl := range r.balancers {
|
||||
if tag == balancer {
|
||||
@@ -36,16 +15,12 @@ func (r *Router) OverrideSelecting(balancer string, selects []string, validity t
|
||||
if b == nil {
|
||||
return newError("balancer '", balancer, "' not found")
|
||||
}
|
||||
err := b.overrideSelecting(selects, validity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.override.Put(target)
|
||||
return nil
|
||||
}
|
||||
|
||||
type overrideSettings struct {
|
||||
selects []string
|
||||
until time.Time
|
||||
target string
|
||||
}
|
||||
|
||||
type override struct {
|
||||
@@ -54,30 +29,22 @@ type override struct {
|
||||
}
|
||||
|
||||
// Get gets the override settings
|
||||
func (o *override) Get() *overrideSettings {
|
||||
func (o *override) Get() string {
|
||||
o.access.RLock()
|
||||
defer o.access.RUnlock()
|
||||
if len(o.settings.selects) == 0 || time.Now().After(o.settings.until) {
|
||||
return nil
|
||||
}
|
||||
return &overrideSettings{
|
||||
selects: o.settings.selects,
|
||||
until: o.settings.until,
|
||||
}
|
||||
return o.settings.target
|
||||
}
|
||||
|
||||
// Put updates the override settings
|
||||
func (o *override) Put(selects []string, until time.Time) {
|
||||
func (o *override) Put(target string) {
|
||||
o.access.Lock()
|
||||
defer o.access.Unlock()
|
||||
o.settings.selects = selects
|
||||
o.settings.until = until
|
||||
o.settings.target = target
|
||||
}
|
||||
|
||||
// Clear clears the override settings
|
||||
func (o *override) Clear() {
|
||||
o.access.Lock()
|
||||
defer o.access.Unlock()
|
||||
o.settings.selects = nil
|
||||
o.settings.until = time.Time{}
|
||||
o.settings.target = ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user