Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions common/signal/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type ActivityUpdater interface {

type ActivityTimer struct {
sync.RWMutex
updated chan struct{}
checkTask *task.Periodic
onTimeout func()
updated chan struct{}
checkTask *task.Periodic
onTimeout func()
overridden bool
}

func (t *ActivityTimer) Update() {
Expand All @@ -31,14 +32,16 @@ func (t *ActivityTimer) check() error {
select {
case <-t.updated:
default:
t.finish()
t.finish(false)
}
return nil
}

func (t *ActivityTimer) finish() {
t.Lock()
defer t.Unlock()
func (t *ActivityTimer) finish(locked bool) {
if !locked {
t.Lock()
defer t.Unlock()
}

if t.onTimeout != nil {
t.onTimeout()
Expand All @@ -50,9 +53,12 @@ func (t *ActivityTimer) finish() {
}
}

func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
func (t *ActivityTimer) setTimeout(timeout time.Duration) {
if t.onTimeout == nil {
return
}
if timeout == 0 {
t.finish()
t.finish(true)
return
}

Expand All @@ -61,14 +67,26 @@ 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()
}

Expand Down
6 changes: 3 additions & 3 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.SetTimeout(8 * time.Hour) // prevent leak, just in case
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
if inTimer != nil {
inTimer.SetTimeout(8 * time.Hour)
inTimer.SetTimeoutIfNotOverridden(24 * time.Hour)
}
w, err := tc.ReadFrom(readerConn)
if readCounter != nil {
Expand Down
Loading