refactor(tcpfwd): заменить busy-loop poll пула на Ready-канал; тихий accept-цикл при shutdown
This commit is contained in:
1 parent
1459f39551
commit
2b24471553
2 files changed
+21
-9
No files matched your search
@@ -32,6 +32,20 @@ type SessionPool struct {
|
|||||||
sessions []*PooledSession
|
sessions []*PooledSession
|
||||||
counter atomic.Uint64
|
counter atomic.Uint64
|
||||||
connCounter atomic.Uint64
|
connCounter atomic.Uint64
|
||||||
|
|
||||||
|
readyOnce sync.Once
|
||||||
|
ready chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ready returns a channel closed the first time the pool gains a session.
|
||||||
|
func (p *SessionPool) Ready() <-chan struct{} {
|
||||||
|
p.mu.Lock()
|
||||||
|
if p.ready == nil {
|
||||||
|
p.ready = make(chan struct{})
|
||||||
|
}
|
||||||
|
ch := p.ready
|
||||||
|
p.mu.Unlock()
|
||||||
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add registers a freshly connected session in the pool.
|
// Add registers a freshly connected session in the pool.
|
||||||
@@ -39,7 +53,12 @@ func (p *SessionPool) Add(id int, s *smux.Session) *PooledSession {
|
|||||||
ps := &PooledSession{ID: id, Sess: s}
|
ps := &PooledSession{ID: id, Sess: s}
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.sessions = append(p.sessions, ps)
|
p.sessions = append(p.sessions, ps)
|
||||||
|
if p.ready == nil {
|
||||||
|
p.ready = make(chan struct{})
|
||||||
|
}
|
||||||
|
ready := p.ready
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
|
p.readyOnce.Do(func() { close(ready) })
|
||||||
return ps
|
return ps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,16 +70,11 @@ func Run(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr, lis
|
|||||||
}
|
}
|
||||||
|
|
||||||
deps.log().Infof("VLESS mode: waiting for sessions to connect (total: %d)...", numSessions)
|
deps.log().Infof("VLESS mode: waiting for sessions to connect (total: %d)...", numSessions)
|
||||||
for {
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
wgMaint.Wait()
|
wgMaint.Wait()
|
||||||
return nil
|
return nil
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-pool.Ready():
|
||||||
}
|
|
||||||
if pool.Count() > 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", listenAddr)
|
listener, err := net.Listen("tcp", listenAddr)
|
||||||
@@ -105,12 +100,10 @@ func Run(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr, lis
|
|||||||
for {
|
for {
|
||||||
tcpConn, err := wrappedListener.Accept()
|
tcpConn, err := wrappedListener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
select {
|
if ctx.Err() != nil {
|
||||||
case <-ctx.Done():
|
|
||||||
wgConn.Wait()
|
wgConn.Wait()
|
||||||
wgMaint.Wait()
|
wgMaint.Wait()
|
||||||
return nil
|
return nil
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
deps.log().Errorf("TCP accept error: %s", err)
|
deps.log().Errorf("TCP accept error: %s", err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in new issue
Block a user