refactor: improve task management with xsync for concurrent access and enhance callback and subtasks handling as well as memory allocation

This commit is contained in:
yusing
2025-05-25 15:01:44 +08:00
parent ade93d49a3
commit c1221e61d4
16 changed files with 447 additions and 211 deletions

View File

@@ -1,43 +1,46 @@
package task
import (
"slices"
"strings"
"github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/gperr"
)
// debug only.
func (t *Task) listChildren() []string {
var children []string
allTasks.Range(func(child *Task) bool {
if child.parent == t {
children = append(children, strings.TrimPrefix(child.name, t.name+"."))
}
return true
})
return children
}
// debug only.
func (t *Task) listCallbacks() []string {
var callbacks []string
t.mu.Lock()
defer t.mu.Unlock()
func (t *Task) listStuckedCallbacks() []string {
callbacks := make([]string, 0, len(t.callbacks))
for c := range t.callbacks {
callbacks = append(callbacks, c.about)
if !c.done.Load() {
callbacks = append(callbacks, c.about)
}
}
return callbacks
}
// DebugTaskList returns list of all tasks.
//
// The returned string is suitable for printing to the console.
func DebugTaskList() []string {
l := make([]string, 0, allTasks.Size())
allTasks.RangeAll(func(t *Task) {
l = append(l, t.name)
})
slices.Sort(l)
return l
// debug only.
func (t *Task) listStuckedChildren() []string {
children := make([]string, 0, len(t.children))
for c := range t.children {
if c.isFinished() {
continue
}
children = append(children, c.String())
if len(c.children) > 0 {
children = append(children, c.listStuckedChildren()...)
}
}
return children
}
func (t *Task) reportStucked() {
callbacks := t.listStuckedCallbacks()
children := t.listStuckedChildren()
fmtOutput := gperr.Multiline().
Addf("stucked callbacks: %d, stucked children: %d",
len(callbacks), len(children),
).
Addf("callbacks").
AddLinesString(callbacks...).
Addf("children").
AddLinesString(children...)
log.Warn().Msg(fmtOutput.Error())
}