Documentation
¶
Index ¶
- Variables
- func Add(delta int)
- func CGO(f handle)
- func Cancel() bool
- func Done()
- func GO(f func())
- func SGO(f handle)
- func Stopped() bool
- func Timeout(d time.Duration, fn func() error) error
- func Trigger(handle func())
- func Try(f handle)
- func Wait(timeout time.Duration) (err error)
- func WithCancel() (context.Context, context.CancelFunc)
- func WithTimeout(t time.Duration) (context.Context, context.CancelFunc)
- func WithValue(key, val any) context.Context
- type Daemon
- type SCC
- func (s *SCC) CGO(f handle)
- func (s *SCC) Cancel() bool
- func (s *SCC) Deadline() (deadline time.Time, ok bool)
- func (s *SCC) GO(f func())
- func (s *SCC) SGO(f handle)
- func (s *SCC) Stopped() bool
- func (s *SCC) Timeout(d time.Duration, fn func() error) error
- func (s *SCC) Trigger(f func())
- func (s *SCC) Try(f handle)
- func (s *SCC) Wait(timeout time.Duration) (err error)
- func (s *SCC) WithCancel() (context.Context, context.CancelFunc)
- func (s *SCC) WithTimeout(t time.Duration) (context.Context, context.CancelFunc)
- func (s *SCC) WithValue(key, val any) context.Context
- type Worker
Constants ¶
This section is empty.
Variables ¶
View Source
var Default = New(context.Background())
Default 是全局默认 SCC 实例。
View Source
var ErrorTimeout = errors.New("timeout")
Functions ¶
func Timeout ¶
Timeout 在 d 时间内等待 fn 完成,超时返回 ErrorTimeout。 cher 为带 1 缓冲的 channel:即使超时分支获胜,子 goroutine 的 send 也能立即完成并退出, 不会因无接收者而永久阻塞造成 goroutine 泄漏。
func WithCancel ¶ added in v1.1.0
func WithCancel() (context.Context, context.CancelFunc)
WithCancel 基于默认实例创建带取消的子 context。
func WithTimeout ¶ added in v1.1.0
WithTimeout 基于默认实例创建带超时的子 context。
Types ¶
type SCC ¶
type SCC struct {
sync.WaitGroup // 嵌入的WaitGroup,用于等待所有协程结束
Catch func(error) // 异常捕获函数,默认控制台打印
Context context.Context // 根上下文
// contains filtered or unexported fields
}
SCC 协程控制器,用于管理和控制Go协程的生命周期
func (*SCC) GO ¶
func (s *SCC) GO(f func())
GO 启动一个普通的协程。 注意: Add(1) 必须在 go 之前执行,否则主线程 Wait 可能看到计数 0 提前返回。
func (*SCC) Trigger ¶ added in v1.2.0
func (s *SCC) Trigger(f func())
Trigger 注册一个在服务器关闭时执行的函数。 调用假定:Trigger 在启动阶段单线程注册;Cancel 由 CAS 保证只执行一次。 已 Cancel 后再调用 Trigger 会被直接丢弃,不做追加(避免与 Cancel 遍历 handle 切片产生 race)。
func (*SCC) Try ¶
func (s *SCC) Try(f handle)
Try 在当前 goroutine 同步执行 f,使用 recover 保护。 与 GO/CGO/SGO 不同,本函数不启动新 goroutine。 WaitGroup 的 Add/Done 在同一线程内,无竞态。
func (*SCC) WithCancel ¶ added in v1.1.0
func (s *SCC) WithCancel() (context.Context, context.CancelFunc)
WithCancel 创建带取消功能的子上下文。
func (*SCC) WithTimeout ¶ added in v1.1.0
WithTimeout 创建带超时功能的子上下文。
Click to show internal directories.
Click to hide internal directories.