scc

package
v1.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = New(context.Background())

Default 是全局默认 SCC 实例。

View Source
var ErrorTimeout = errors.New("timeout")

Functions

func Add

func Add(delta int)

Add 向默认实例的 WaitGroup 添加计数。

func CGO

func CGO(f handle)

CGO 使用默认实例启动一个带取消 context 的协程。

func Cancel

func Cancel() bool

Cancel 关闭默认实例的所有协程。

func Done

func Done()

Done 向默认实例的 WaitGroup 递减计数。

func GO

func GO(f func())

GO 使用默认实例启动一个普通协程。

func SGO

func SGO(f handle)

SGO 使用默认实例启动一个带 recover 保护的协程。

func Stopped

func Stopped() bool

Stopped 返回默认实例是否已关闭。

func Timeout

func Timeout(d time.Duration, fn func() error) error

Timeout 在 d 时间内等待 fn 完成,超时返回 ErrorTimeout。 cher 为带 1 缓冲的 channel:即使超时分支获胜,子 goroutine 的 send 也能立即完成并退出, 不会因无接收者而永久阻塞造成 goroutine 泄漏。

func Trigger added in v1.2.0

func Trigger(handle func())

Trigger 向默认实例注册一个在关闭时执行的函数。

func Try

func Try(f handle)

Try 使用默认实例同步执行 f,带 recover 保护。

func Wait

func Wait(timeout time.Duration) (err error)

Wait 使用默认实例阻塞等待所有协程结束,timeout=0 无限等待。

func WithCancel added in v1.1.0

func WithCancel() (context.Context, context.CancelFunc)

WithCancel 基于默认实例创建带取消的子 context。

func WithTimeout added in v1.1.0

func WithTimeout(t time.Duration) (context.Context, context.CancelFunc)

WithTimeout 基于默认实例创建带超时的子 context。

func WithValue added in v1.2.0

func WithValue(key, val any) context.Context

WithValue 基于默认实例的根 context 创建带键值对的子 context。

Types

type Daemon

type Daemon struct {
	// contains filtered or unexported fields
}

func NewDaemon added in v1.1.0

func NewDaemon(scc *SCC) *Daemon

func (*Daemon) Start added in v1.1.0

func (d *Daemon) Start(f handle) *Worker

Start 守护协程,协程异常退出时会自动重启协程,一般使用在随主进程启动的固定协程

type SCC

type SCC struct {
	sync.WaitGroup // 嵌入的WaitGroup,用于等待所有协程结束

	Catch   func(error)     // 异常捕获函数,默认控制台打印
	Context context.Context // 根上下文
	// contains filtered or unexported fields
}

SCC 协程控制器,用于管理和控制Go协程的生命周期

func New

func New(ctx context.Context) *SCC

New 创建一个新的 SCC 实例。ctx 为 nil 时使用 context.Background()。

func (*SCC) CGO

func (s *SCC) CGO(f handle)

CGO 启动一个带有取消通道的协程。

func (*SCC) Cancel

func (s *SCC) Cancel() bool

Cancel 关闭所有协程。已关闭过返回 false,首次关闭返回 true。

func (*SCC) Deadline added in v1.1.0

func (s *SCC) Deadline() (deadline time.Time, ok bool)

Deadline 返回上下文的截止时间。

func (*SCC) GO

func (s *SCC) GO(f func())

GO 启动一个普通的协程。 注意: Add(1) 必须在 go 之前执行,否则主线程 Wait 可能看到计数 0 提前返回。

func (*SCC) SGO

func (s *SCC) SGO(f handle)

SGO 启动一个使用 recover 保护的协程,防止主进程崩溃。

func (*SCC) Stopped

func (s *SCC) Stopped() bool

Stopped 返回是否已经关闭。

func (*SCC) Timeout added in v0.0.5

func (s *SCC) Timeout(d time.Duration, fn func() error) error

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) Wait

func (s *SCC) Wait(timeout time.Duration) (err error)

Wait 阻塞等待所有协程结束,timeout=0 表示无限等待。 只能在主进程中调用;在 SCC 启动的协程内调用会自己等自己,死循环。

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

func (s *SCC) WithTimeout(t time.Duration) (context.Context, context.CancelFunc)

WithTimeout 创建带超时功能的子上下文。

func (*SCC) WithValue added in v1.2.0

func (s *SCC) WithValue(key, val any) context.Context

WithValue 基于 SCC 的根上下文创建带键值对的子上下文。 签名对齐 stdlib: 参数仅为 key/val。若需基于自定义父 context, 直接使用 context.WithValue(parent, key, val) 即可。

type Worker added in v1.1.0

type Worker struct {
	Handle handle
	Cancel context.CancelFunc
	// contains filtered or unexported fields
}

func (*Worker) Stop added in v1.1.0

func (w *Worker) Stop()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL