mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-21 16:56:27 -05:00
Fix a potential issue causing high CPU usage
This commit is contained in:
parent
9206d7a741
commit
514dcffe2e
@ -93,6 +93,7 @@ func (w *tcpWorker) handleConnections(conns <-chan internet.Connection) {
|
||||
for {
|
||||
select {
|
||||
case conn := <-conns:
|
||||
conn.SetReusable(false)
|
||||
conn.Close()
|
||||
default:
|
||||
break L
|
||||
|
@ -202,6 +202,7 @@ func (v *Listener) OnReceive(payload *buf.Buffer, src v2net.Destination, origina
|
||||
select {
|
||||
case v.conns <- netConn:
|
||||
case <-time.After(time.Second * 5):
|
||||
conn.SetReusable(false)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/errors"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/retry"
|
||||
"v2ray.com/core/transport/internet"
|
||||
"v2ray.com/core/transport/internet/internal"
|
||||
v2tls "v2ray.com/core/transport/internet/tls"
|
||||
@ -21,7 +21,6 @@ var (
|
||||
)
|
||||
|
||||
type TCPListener struct {
|
||||
sync.Mutex
|
||||
ctx context.Context
|
||||
listener *net.TCPListener
|
||||
tlsConfig *tls.Config
|
||||
@ -76,13 +75,20 @@ func (v *TCPListener) KeepAccepting() {
|
||||
return
|
||||
default:
|
||||
}
|
||||
conn, err := v.listener.Accept()
|
||||
v.Lock()
|
||||
var conn net.Conn
|
||||
err := retry.ExponentialBackoff(5, 200).On(func() error {
|
||||
rawConn, err := v.listener.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn = rawConn
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Warning("TCP|Listener: Failed to accepted raw connections: ", err)
|
||||
v.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
if v.tlsConfig != nil {
|
||||
conn = tls.Server(conn, v.tlsConfig)
|
||||
}
|
||||
@ -95,19 +101,16 @@ func (v *TCPListener) KeepAccepting() {
|
||||
case <-time.After(time.Second * 5):
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
v.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (v *TCPListener) Put(id internal.ConnectionID, conn net.Conn) {
|
||||
select {
|
||||
case <-v.ctx.Done():
|
||||
conn.Close()
|
||||
return
|
||||
case v.conns <- internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())):
|
||||
case <-time.After(time.Second * 5):
|
||||
conn.Close()
|
||||
case <-v.ctx.Done():
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user