Help us shape the future of Go! Take the 2026 Go Developer Survey

syncx

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package syncx 并发编排原语(stdlib-only):按键在飞闸门 + 按序扇出收集。 收编自消费方手写形态——LLM 按模型名限在飞(429 风暴治理)与「按 index 写 结果免锁」的并行扇出样板。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FanIn

func FanIn[T any](n int, fn func(i int) T) []T

FanIn 并行执行 fn(0..n-1),各 goroutine 只写自己的 results[i] 槽位(免锁), 全部完成后按 index 序返回。fn 经 async.GoSafe 收编(AGENTS.md「禁裸 go func」, v0.2.1 前为裸 go、fn panic 会打死宿主进程):单个 fn panic 被恢复并走全局 panic handler 落日志,该槽位保持零值、其余槽位与收口不受影响——消费方按 「空槽降级」处理(零值歧义由调用方区分)。n<=0 返回空切片。

Types

type KeyedGate

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

KeyedGate 按键在飞上限闸门(并发安全;channel 令牌先到先得;键集由调用方 保证有界——如按模型名/租户名,键量级恒小不回收)。

func NewKeyedGate

func NewKeyedGate(cap int) *KeyedGate

NewKeyedGate 构造;cap<=0 = 不限(Acquire 直通零开销)。

func (*KeyedGate) Acquire

func (g *KeyedGate) Acquire(ctx context.Context, key string) (func(), error)

Acquire 取一个键下在飞名额;ctx 取消(排队等待中)返回 ctx.Err()。 返回的 release 归还名额,幂等可重复调用。

type Slot added in v0.5.0

type Slot[T any] struct {
	Val      T
	Err      error
	Panicked any
}

Slot 是 FanInErr 的逐槽结果:Val/Err 为 fn 的正常返回;Panicked 非 nil 表示 fn panic(已恢复并经 async.ReportPanic 上报全局 handler),此时 Val 为零值、 Err 为 nil——T 的合法零值与 panic 空槽由此可区分,消费方按槽降级不再需要 自写 recover→占位样板。

func FanInErr added in v0.5.0

func FanInErr[T any](n int, fn func(i int) (T, error)) []Slot[T]

FanInErr 逐槽错误变体:并行执行 fn(0..n-1)(fn 可返回 error),全部完成后 按 index 序返回逐槽结果。fn 正常返回 → Val/Err 如实记录;fn panic → 该槽 Panicked=payload、Val 零值、Err nil——不再静默(FanIn 契约保留不动,供无 error 形态的轻量路径)。n<=0 返回空切片。

Jump to

Keyboard shortcuts

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