fix splice-timeout

This commit is contained in:
patterniha
2025-08-22 19:04:44 +03:30
parent 3f3cf89ade
commit c527ff6749
2 changed files with 33 additions and 17 deletions

View File

@@ -15,9 +15,10 @@ type ActivityUpdater interface {
type ActivityTimer struct { type ActivityTimer struct {
sync.RWMutex sync.RWMutex
updated chan struct{} updated chan struct{}
checkTask *task.Periodic checkTask *task.Periodic
onTimeout func() onTimeout func()
overridden bool
} }
func (t *ActivityTimer) Update() { func (t *ActivityTimer) Update() {
@@ -31,14 +32,16 @@ func (t *ActivityTimer) check() error {
select { select {
case <-t.updated: case <-t.updated:
default: default:
t.finish() t.finish(false)
} }
return nil return nil
} }
func (t *ActivityTimer) finish() { func (t *ActivityTimer) finish(locked bool) {
t.Lock() if !locked {
defer t.Unlock() t.Lock()
defer t.Unlock()
}
if t.onTimeout != nil { if t.onTimeout != nil {
t.onTimeout() t.onTimeout()
@@ -50,17 +53,15 @@ func (t *ActivityTimer) finish() {
} }
} }
func (t *ActivityTimer) SetTimeout(timeout time.Duration) { func (t *ActivityTimer) setTimeout(timeout time.Duration) {
if timeout == 0 {
t.finish()
return
}
t.Lock()
defer t.Unlock()
if t.onTimeout == nil { if t.onTimeout == nil {
return return
} }
if timeout == 0 {
t.finish(true)
return
}
checkTask := &task.Periodic{ checkTask := &task.Periodic{
Interval: timeout, Interval: timeout,
Execute: t.check, Execute: t.check,
@@ -68,12 +69,27 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
if t.checkTask != nil { if t.checkTask != nil {
t.checkTask.Close() t.checkTask.Close()
t.overridden = true
} }
t.checkTask = checkTask t.checkTask = checkTask
t.Update() t.Update()
common.Must(checkTask.Start()) 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()
}
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer { func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
timer := &ActivityTimer{ timer := &ActivityTimer{
updated: make(chan struct{}, 1), updated: make(chan struct{}, 1),

View File

@@ -596,9 +596,9 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net
statWriter, _ := writer.(*dispatcher.SizeStatWriter) statWriter, _ := writer.(*dispatcher.SizeStatWriter)
//runtime.Gosched() // necessary //runtime.Gosched() // necessary
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice 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 timer.SetTimeoutIfNotOverridden(8 * time.Hour) // prevent leak, just in case
if inTimer != nil { if inTimer != nil {
inTimer.SetTimeout(8 * time.Hour) inTimer.SetTimeoutIfNotOverridden(8 * time.Hour)
} }
w, err := tc.ReadFrom(readerConn) w, err := tc.ReadFrom(readerConn)
if readCounter != nil { if readCounter != nil {