mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-01-13 22:27:05 +08:00
Compare commits
3 Commits
timer-redu
...
dns-dead-l
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73baf47358 | ||
|
|
ecc2f73108 | ||
|
|
f45ca197a2 |
@@ -15,10 +15,9 @@ type ActivityUpdater interface {
|
||||
|
||||
type ActivityTimer struct {
|
||||
sync.RWMutex
|
||||
updated chan struct{}
|
||||
checkTask *task.Periodic
|
||||
onTimeout func()
|
||||
overridden bool
|
||||
updated chan struct{}
|
||||
checkTask *task.Periodic
|
||||
onTimeout func()
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) Update() {
|
||||
@@ -32,16 +31,14 @@ func (t *ActivityTimer) check() error {
|
||||
select {
|
||||
case <-t.updated:
|
||||
default:
|
||||
t.finish(false)
|
||||
t.finish()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) finish(locked bool) {
|
||||
if !locked {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
}
|
||||
func (t *ActivityTimer) finish() {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
if t.onTimeout != nil {
|
||||
t.onTimeout()
|
||||
@@ -53,12 +50,9 @@ func (t *ActivityTimer) finish(locked bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) setTimeout(timeout time.Duration) {
|
||||
if t.onTimeout == nil {
|
||||
return
|
||||
}
|
||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||
if timeout == 0 {
|
||||
t.finish(true)
|
||||
t.finish()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -67,26 +61,14 @@ func (t *ActivityTimer) setTimeout(timeout time.Duration) {
|
||||
Execute: t.check,
|
||||
}
|
||||
|
||||
t.Lock()
|
||||
|
||||
if t.checkTask != nil {
|
||||
t.checkTask.Close()
|
||||
t.overridden = true
|
||||
}
|
||||
t.checkTask = checkTask
|
||||
t.Update()
|
||||
common.Must(checkTask.Start())
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||
t.Lock()
|
||||
t.setTimeout(timeout)
|
||||
t.Unlock()
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) SetTimeoutIfNotOverridden(timeout time.Duration) {
|
||||
t.Lock()
|
||||
if !t.overridden {
|
||||
t.setTimeout(timeout)
|
||||
}
|
||||
t.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
go_errors "errors"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
@@ -369,6 +370,10 @@ type outboundConn struct {
|
||||
access sync.Mutex
|
||||
dialer func() (stat.Connection, error)
|
||||
|
||||
closeOnce sync.Once
|
||||
dialOnce sync.Once
|
||||
closed atomic.Bool
|
||||
|
||||
conn net.Conn
|
||||
connReady chan struct{}
|
||||
}
|
||||
@@ -378,24 +383,28 @@ func (c *outboundConn) dial() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.closed.Load() {
|
||||
return errors.New("connection closed during dial")
|
||||
}
|
||||
c.conn = conn
|
||||
c.connReady <- struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *outboundConn) Write(b []byte) (int, error) {
|
||||
c.dialOnce.Do(func() {
|
||||
c.dial()
|
||||
})
|
||||
c.access.Lock()
|
||||
|
||||
if c.conn == nil {
|
||||
if err := c.dial(); err != nil {
|
||||
c.access.Unlock()
|
||||
errors.LogWarningInner(context.Background(), err, "failed to dial outbound connection")
|
||||
return len(b), nil
|
||||
}
|
||||
}
|
||||
|
||||
conn := c.conn
|
||||
c.access.Unlock()
|
||||
|
||||
if conn == nil {
|
||||
_, open := <-c.connReady
|
||||
if !open {
|
||||
return 0, io.EOF
|
||||
}
|
||||
conn = c.conn
|
||||
}
|
||||
return c.conn.Write(b)
|
||||
}
|
||||
|
||||
@@ -417,11 +426,14 @@ func (c *outboundConn) Read(b []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (c *outboundConn) Close() error {
|
||||
c.access.Lock()
|
||||
close(c.connReady)
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.access.Unlock()
|
||||
c.closeOnce.Do(func() {
|
||||
c.access.Lock()
|
||||
c.closed.Store(true)
|
||||
close(c.connReady)
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.access.Unlock()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -595,10 +595,10 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net
|
||||
errors.LogInfo(ctx, "CopyRawConn splice")
|
||||
statWriter, _ := writer.(*dispatcher.SizeStatWriter)
|
||||
//runtime.Gosched() // necessary
|
||||
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice
|
||||
timer.SetTimeoutIfNotOverridden(24 * time.Hour) // prevent leak, just in case
|
||||
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice
|
||||
timer.SetTimeout(8 * time.Hour) // prevent leak, just in case
|
||||
if inTimer != nil {
|
||||
inTimer.SetTimeoutIfNotOverridden(24 * time.Hour)
|
||||
inTimer.SetTimeout(8 * time.Hour)
|
||||
}
|
||||
w, err := tc.ReadFrom(readerConn)
|
||||
if readCounter != nil {
|
||||
|
||||
Reference in New Issue
Block a user