mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 14:09:44 +02:00
fix: high cpu usage
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user