fix: lazy check

This commit is contained in:
gVisor bot 2022-10-30 23:08:18 +08:00
parent 9f75ad7278
commit 744c058932
10 changed files with 36 additions and 22 deletions

View file

@ -90,7 +90,7 @@ func (b *Base) Addr() string {
} }
// Unwrap implements C.ProxyAdapter // Unwrap implements C.ProxyAdapter
func (b *Base) Unwrap(metadata *C.Metadata) C.Proxy { func (b *Base) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return nil return nil
} }

View file

@ -72,8 +72,8 @@ func (f *Fallback) MarshalJSON() ([]byte, error) {
} }
// Unwrap implements C.ProxyAdapter // Unwrap implements C.ProxyAdapter
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy { func (f *Fallback) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
proxy := f.findAliveProxy(true) proxy := f.findAliveProxy(touch)
return proxy return proxy
} }

View file

@ -82,7 +82,7 @@ func jumpHash(key uint64, buckets int32) int32 {
// DialContext implements C.ProxyAdapter // DialContext implements C.ProxyAdapter
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) { func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
proxy := lb.Unwrap(metadata) proxy := lb.Unwrap(metadata, true)
defer func() { defer func() {
if err == nil { if err == nil {
@ -105,7 +105,7 @@ func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Meta
} }
}() }()
proxy := lb.Unwrap(metadata) proxy := lb.Unwrap(metadata, true)
return proxy.ListenPacketContext(ctx, metadata, lb.Base.DialOptions(opts...)...) return proxy.ListenPacketContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
} }
@ -190,8 +190,8 @@ func strategyStickySessions() strategyFn {
} }
// Unwrap implements C.ProxyAdapter // Unwrap implements C.ProxyAdapter
func (lb *LoadBalance) Unwrap(metadata *C.Metadata) C.Proxy { func (lb *LoadBalance) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
proxies := lb.GetProxies(true) proxies := lb.GetProxies(touch)
return lb.strategyFn(proxies, metadata) return lb.strategyFn(proxies, metadata)
} }

View file

@ -153,11 +153,11 @@ func (r *Relay) proxies(metadata *C.Metadata, touch bool) ([]C.Proxy, []C.Proxy)
for n, proxy := range rawProxies { for n, proxy := range rawProxies {
proxies = append(proxies, proxy) proxies = append(proxies, proxy)
chainProxies = append(chainProxies, proxy) chainProxies = append(chainProxies, proxy)
subproxy := proxy.Unwrap(metadata) subproxy := proxy.Unwrap(metadata, touch)
for subproxy != nil { for subproxy != nil {
chainProxies = append(chainProxies, subproxy) chainProxies = append(chainProxies, subproxy)
proxies[n] = subproxy proxies[n] = subproxy
subproxy = subproxy.Unwrap(metadata) subproxy = subproxy.Unwrap(metadata, touch)
} }
} }

View file

@ -74,8 +74,8 @@ func (s *Selector) Set(name string) error {
} }
// Unwrap implements C.ProxyAdapter // Unwrap implements C.ProxyAdapter
func (s *Selector) Unwrap(*C.Metadata) C.Proxy { func (s *Selector) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return s.selectedProxy(true) return s.selectedProxy(touch)
} }
func (s *Selector) selectedProxy(touch bool) C.Proxy { func (s *Selector) selectedProxy(touch bool) C.Proxy {

View file

@ -56,8 +56,8 @@ func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
} }
// Unwrap implements C.ProxyAdapter // Unwrap implements C.ProxyAdapter
func (u *URLTest) Unwrap(*C.Metadata) C.Proxy { func (u *URLTest) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return u.fast(true) return u.fast(touch)
} }
func (u *URLTest) fast(touch bool) C.Proxy { func (u *URLTest) fast(touch bool) C.Proxy {

View file

@ -35,16 +35,13 @@ func (hc *HealthCheck) process() {
go func() { go func() {
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
hc.check() hc.lazyCheck()
}() }()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
now := time.Now().Unix() hc.lazyCheck()
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
hc.check()
}
case <-hc.done: case <-hc.done:
ticker.Stop() ticker.Stop()
return return
@ -52,6 +49,16 @@ func (hc *HealthCheck) process() {
} }
} }
func (hc *HealthCheck) lazyCheck() bool {
now := time.Now().Unix()
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
hc.check()
return true
} else {
return false
}
}
func (hc *HealthCheck) setProxy(proxies []C.Proxy) { func (hc *HealthCheck) setProxy(proxies []C.Proxy) {
hc.proxies = proxies hc.proxies = proxies
} }

View file

@ -93,7 +93,7 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
pp.proxies = proxies pp.proxies = proxies
pp.healthCheck.setProxy(proxies) pp.healthCheck.setProxy(proxies)
if pp.healthCheck.auto() { if pp.healthCheck.auto() {
defer func() { go pp.healthCheck.check() }() defer func() { go pp.healthCheck.lazyCheck() }()
} }
} }

View file

@ -106,7 +106,7 @@ type ProxyAdapter interface {
ListenPacketOnStreamConn(c net.Conn, metadata *Metadata) (PacketConn, error) ListenPacketOnStreamConn(c net.Conn, metadata *Metadata) (PacketConn, error)
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract. // Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
Unwrap(metadata *Metadata) Proxy Unwrap(metadata *Metadata, touch bool) Proxy
} }
type Group interface { type Group interface {

View file

@ -425,8 +425,15 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
continue continue
} }
// only unwrap one group, multi-layer nesting will be invalid // parse multi-layer nesting
if adapter.Type() == C.Pass || (adapter.Unwrap(metadata) != nil && adapter.Unwrap(metadata).Type() == C.Pass) { passed := false
for adapter := adapter; adapter != nil; adapter = adapter.Unwrap(metadata, false) {
if adapter.Type() == C.Pass {
passed = true
break
}
}
if passed {
log.Debugln("%s match Pass rule", adapter.Name()) log.Debugln("%s match Pass rule", adapter.Name())
continue continue
} }