syncx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 3 Imported by: 0

README

syncx 包 Package

syncx 提供 context-aware semaphore limiter 和首错取消、错误聚合的 worker group。

Use the limiter for bounded concurrency, TryRelease to detect release misuse, and WorkerGroup when the first worker error should cancel siblings while Wait returns joined worker errors.

Documentation

Overview

Package syncx 提供上下文感知并发限制和工作组。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	Acquire(context.Context) error
	Release()
}

type SemaphoreLimiter

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

func NewSemaphoreLimiter

func NewSemaphoreLimiter(n int) *SemaphoreLimiter
Example
package main

import (
	"context"

	"github.com/ZoneCNH/kernel/syncx"
)

func main() {
	limiter := syncx.NewSemaphoreLimiter(2)
	_ = limiter.Acquire(context.Background())
	limiter.Release()
}

func (*SemaphoreLimiter) Acquire

func (l *SemaphoreLimiter) Acquire(ctx context.Context) error

func (*SemaphoreLimiter) Release

func (l *SemaphoreLimiter) Release()

Release returns a permit to the limiter. Double-release when no permit is held is silently ignored; this is a deliberate design choice to simplify caller cleanup paths.

func (*SemaphoreLimiter) TryRelease added in v0.6.0

func (l *SemaphoreLimiter) TryRelease() bool

type WorkerGroup

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

func NewWorkerGroup

func NewWorkerGroup(ctx context.Context) *WorkerGroup

func (*WorkerGroup) Go

func (g *WorkerGroup) Go(fn func(context.Context) error)

Go 启动一个 worker goroutine。如果 WorkerGroup 已被 Wait 关闭,调用被静默忽略。 如需检测关闭状态,使用 TryGo。

func (*WorkerGroup) TryGo added in v0.6.0

func (g *WorkerGroup) TryGo(fn func(context.Context) error) bool

func (*WorkerGroup) Wait

func (g *WorkerGroup) Wait() error

Jump to

Keyboard shortcuts

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