1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-19 20:15:59 +08:00

fix: correctly implement references to proxies

This commit is contained in:
wwqgtxx
2025-04-04 09:08:52 +08:00
parent 323973f22f
commit e81f3a97af
4 changed files with 86 additions and 24 deletions

View File

@ -9,10 +9,8 @@ import (
"net/http"
"net/netip"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/metacubex/mihomo/common/atomic"
@ -41,9 +39,6 @@ type Proxy struct {
alive atomic.Bool
history *queue.Queue[C.DelayHistory]
extra *xsync.MapOf[string, *internalProxyState]
closeOnce sync.Once
closeErr error
}
// Adapter implements C.Proxy
@ -296,27 +291,12 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
return
}
func (p *Proxy) Close() error {
p.closeOnce.Do(func() {
runtime.SetFinalizer(p, nil)
p.closeErr = p.ProxyAdapter.Close()
})
return p.closeErr
}
func NewProxy(adapter C.ProxyAdapter) *Proxy {
proxy := &Proxy{
return &Proxy{
ProxyAdapter: adapter,
history: queue.New[C.DelayHistory](defaultHistoriesNum),
alive: atomic.NewBool(true),
extra: xsync.NewMapOf[string, *internalProxyState]()}
// auto close ProxyAdapter
runtime.SetFinalizer(proxy, func(p *Proxy) {
log.Debugln("Closing outdated proxy [%s]", p.Name())
_ = p.Close()
})
return proxy
}
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {