Skip to content

Commit

Permalink
fix: lazy check
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Oct 30, 2022
1 parent dedb912 commit b9d8b69
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion adapter/outbound/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (b *Base) Addr() string {
}

// 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
}

Expand Down
4 changes: 2 additions & 2 deletions adapter/outboundgroup/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (f *Fallback) MarshalJSON() ([]byte, error) {
}

// Unwrap implements C.ProxyAdapter
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
proxy := f.findAliveProxy(true)
func (f *Fallback) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
proxy := f.findAliveProxy(touch)
return proxy
}

Expand Down
8 changes: 4 additions & 4 deletions adapter/outboundgroup/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func jumpHash(key uint64, buckets int32) int32 {

// DialContext implements C.ProxyAdapter
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() {
if err == nil {
Expand All @@ -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...)...)
}

Expand Down Expand Up @@ -190,8 +190,8 @@ func strategyStickySessions() strategyFn {
}

// Unwrap implements C.ProxyAdapter
func (lb *LoadBalance) Unwrap(metadata *C.Metadata) C.Proxy {
proxies := lb.GetProxies(true)
func (lb *LoadBalance) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
proxies := lb.GetProxies(touch)
return lb.strategyFn(proxies, metadata)
}

Expand Down
4 changes: 2 additions & 2 deletions adapter/outboundgroup/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ func (r *Relay) proxies(metadata *C.Metadata, touch bool) ([]C.Proxy, []C.Proxy)
for n, proxy := range rawProxies {
proxies = append(proxies, proxy)
chainProxies = append(chainProxies, proxy)
subproxy := proxy.Unwrap(metadata)
subproxy := proxy.Unwrap(metadata, touch)
for subproxy != nil {
chainProxies = append(chainProxies, subproxy)
proxies[n] = subproxy
subproxy = subproxy.Unwrap(metadata)
subproxy = subproxy.Unwrap(metadata, touch)
}
}

Expand Down
4 changes: 2 additions & 2 deletions adapter/outboundgroup/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (s *Selector) Set(name string) error {
}

// Unwrap implements C.ProxyAdapter
func (s *Selector) Unwrap(*C.Metadata) C.Proxy {
return s.selectedProxy(true)
func (s *Selector) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return s.selectedProxy(touch)
}

func (s *Selector) selectedProxy(touch bool) C.Proxy {
Expand Down
4 changes: 2 additions & 2 deletions adapter/outboundgroup/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
}

// Unwrap implements C.ProxyAdapter
func (u *URLTest) Unwrap(*C.Metadata) C.Proxy {
return u.fast(true)
func (u *URLTest) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return u.fast(touch)
}

func (u *URLTest) fast(touch bool) C.Proxy {
Expand Down
17 changes: 12 additions & 5 deletions adapter/provider/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,30 @@ func (hc *HealthCheck) process() {

go func() {
time.Sleep(30 * time.Second)
hc.check()
hc.lazyCheck()
}()

for {
select {
case <-ticker.C:
now := time.Now().Unix()
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
hc.check()
}
hc.lazyCheck()
case <-hc.done:
ticker.Stop()
return
}
}
}

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) {
hc.proxies = proxies
}
Expand Down
2 changes: 1 addition & 1 deletion adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
pp.proxies = proxies
pp.healthCheck.setProxy(proxies)
if pp.healthCheck.auto() {
defer func() { go pp.healthCheck.check() }()
defer func() { go pp.healthCheck.lazyCheck() }()
}
}

Expand Down
2 changes: 1 addition & 1 deletion constant/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type ProxyAdapter interface {
ListenPacketOnStreamConn(c net.Conn, metadata *Metadata) (PacketConn, error)

// 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 {
Expand Down
11 changes: 9 additions & 2 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,15 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
continue
}

// only unwrap one group, multi-layer nesting will be invalid
if adapter.Type() == C.Pass || (adapter.Unwrap(metadata) != nil && adapter.Unwrap(metadata).Type() == C.Pass) {
// parse multi-layer nesting
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())
continue
}
Expand Down

0 comments on commit b9d8b69

Please sign in to comment.