Skip to content

Commit

Permalink
恢复
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jul 15, 2024
1 parent aaa4ebd commit e23be26
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions asyncLocal/newAsyncLocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@ import (
"github.com/timandy/routine"
)

var list []routine.ThreadLocal

type AsyncLocal[T any] struct {
threadLocal routine.ThreadLocal
}

// 将同一个协程所New的AsyncLocal放到同一个数组中,在使用完后释放
var list = make(map[int64][]routine.ThreadLocal)

// New 创建一个AsyncLocal
func New[T any]() AsyncLocal[T] {
// 加入到list集合,用于手动GC
al := AsyncLocal[T]{
threadLocal: routine.NewInheritableThreadLocal(),
}
al.AddRelease()
return al
}
threadLocal := routine.NewInheritableThreadLocal()
list = append(list, threadLocal)

// AddRelease 将同一个协程所New的AsyncLocal放到同一个数组中,在使用完后自动释放
func (receiver AsyncLocal[T]) AddRelease() {
goId := routine.Goid()
list[goId] = append(list[goId], receiver.threadLocal)
return AsyncLocal[T]{
threadLocal: threadLocal,
}
}

// Get 获取值
Expand All @@ -47,11 +41,10 @@ func (receiver AsyncLocal[T]) Remove() {
receiver.threadLocal.Remove()
}

// Release 释放当前使用的AsyncLocal
// Release 释放
func Release() {
goId := routine.Goid()
for _, threadLocal := range list[goId] {
for _, threadLocal := range list {
threadLocal.Remove()
}
delete(list, goId)
routineContext.Remove()
}

0 comments on commit e23be26

Please sign in to comment.