fix: high cpu usage

This commit is contained in:
yusing
2025-01-22 05:44:04 +08:00
parent 3781bb93e1
commit b984386bab
12 changed files with 82 additions and 44 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"runtime/debug"
"sync"
"sync/atomic"
"time"
"github.com/yusing/go-proxy/internal/common"
@@ -44,8 +45,11 @@ type (
callbacks map[*Callback]struct{}
callbacksDone chan struct{}
finished chan struct{}
finishedCalled bool
finished chan struct{}
// finishedCalled == 1 Finish has been called
// but does not mean that the task is finished yet
// this is used to avoid calling Finish twice
finishedCalled uint32
mu sync.Mutex
@@ -93,13 +97,19 @@ func (t *Task) OnCancel(about string, fn func()) {
// Finish cancel all subtasks and wait for them to finish,
// then marks the task as finished, with the given reason (if any).
func (t *Task) Finish(reason any) {
if atomic.LoadUint32(&t.finishedCalled) == 1 {
return
}
t.mu.Lock()
if t.finishedCalled {
if t.finishedCalled == 1 {
t.mu.Unlock()
return
}
t.finishedCalled = true
t.finishedCalled = 1
t.mu.Unlock()
t.finish(reason)
}