mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-15 22:48:02 +08:00
parent
05e8f13a8d
commit
a7e56f1c43
@ -21,10 +21,14 @@ type Client struct {
|
|||||||
|
|
||||||
dialOut util.DialOutFunc
|
dialOut util.DialOutFunc
|
||||||
|
|
||||||
sessionCounter atomic.Uint64
|
sessionCounter atomic.Uint64
|
||||||
|
|
||||||
idleSession *skiplist.SkipList[uint64, *Session]
|
idleSession *skiplist.SkipList[uint64, *Session]
|
||||||
idleSessionLock sync.Mutex
|
idleSessionLock sync.Mutex
|
||||||
|
|
||||||
|
sessions map[uint64]*Session
|
||||||
|
sessionsLock sync.Mutex
|
||||||
|
|
||||||
padding *atomic.TypedValue[*padding.PaddingFactory]
|
padding *atomic.TypedValue[*padding.PaddingFactory]
|
||||||
|
|
||||||
idleSessionTimeout time.Duration
|
idleSessionTimeout time.Duration
|
||||||
@ -33,6 +37,7 @@ type Client struct {
|
|||||||
|
|
||||||
func NewClient(ctx context.Context, dialOut util.DialOutFunc, _padding *atomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration, minIdleSession int) *Client {
|
func NewClient(ctx context.Context, dialOut util.DialOutFunc, _padding *atomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration, minIdleSession int) *Client {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
|
sessions: make(map[uint64]*Session),
|
||||||
dialOut: dialOut,
|
dialOut: dialOut,
|
||||||
padding: _padding,
|
padding: _padding,
|
||||||
idleSessionTimeout: idleSessionTimeout,
|
idleSessionTimeout: idleSessionTimeout,
|
||||||
@ -130,15 +135,35 @@ func (c *Client) createSession(ctx context.Context) (*Session, error) {
|
|||||||
c.idleSessionLock.Lock()
|
c.idleSessionLock.Lock()
|
||||||
c.idleSession.Remove(math.MaxUint64 - session.seq)
|
c.idleSession.Remove(math.MaxUint64 - session.seq)
|
||||||
c.idleSessionLock.Unlock()
|
c.idleSessionLock.Unlock()
|
||||||
|
|
||||||
|
c.sessionsLock.Lock()
|
||||||
|
delete(c.sessions, session.seq)
|
||||||
|
c.sessionsLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.sessionsLock.Lock()
|
||||||
|
c.sessions[session.seq] = session
|
||||||
|
c.sessionsLock.Unlock()
|
||||||
|
|
||||||
session.Run()
|
session.Run()
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
c.dieCancel()
|
c.dieCancel()
|
||||||
c.minIdleSession = 0
|
|
||||||
go c.idleCleanupExpTime(time.Time{})
|
c.sessionsLock.Lock()
|
||||||
|
sessionToClose := make([]*Session, 0, len(c.sessions))
|
||||||
|
for seq, session := range c.sessions {
|
||||||
|
sessionToClose = append(sessionToClose, session)
|
||||||
|
delete(c.sessions, seq)
|
||||||
|
}
|
||||||
|
c.sessionsLock.Unlock()
|
||||||
|
|
||||||
|
for _, session := range sessionToClose {
|
||||||
|
session.Close()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user