Documentation ¶
Overview ¶
Package syncs 异步控制.
包括 RoutineGroup、LeakyBucket.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrSizeLessZero = errors.New("size less than 0") ErrLimit = errors.New("limit time") )
Functions ¶
func Consume ¶ added in v0.3.22
func Consume[E any](cacheSize, sliceMax, routine int, minTime time.Duration, pac ProducerAndConsumer[E], ) int64
Consume 消费. cacheSize 缓存数量. sliceMax 每次消费最大数量. routine 消费协程数量. minTime 预计的最小消费时间. producer 生产者. consumer 消费者.
func ConsumeFunc ¶ added in v0.3.22
func ConsumeFunc[E any](cacheSize, sliceMax, routine int, minTime time.Duration, producFunc func(produceChan chan E), consumFunc func(num int, elems []E), ) int64
ConsumeFunc 消费. cacheSize 缓存数量. sliceMax 每次消费最大数量. routine 消费协程数量. minTime 预计的最小消费时间. producer 生产者. consumer 消费者. nolint
Example ¶
package main import ( "fmt" "time" "github.com/xuender/oils/syncs" ) func main() { syncs.ConsumeFunc(10, 3, 3, time.Millisecond, func(produceChan chan int) { for i := 0; i < 100; i++ { produceChan <- i } }, func(num int, elems []int) { time.Sleep(time.Microsecond) fmt.Println(num, elems) }) }
Output:
Types ¶
type LeakyBucket ¶
type LeakyBucket struct {
// contains filtered or unexported fields
}
LeakyBucket 漏桶算法.
func NewLeakyBucket ¶
func NewLeakyBucket(capacity uint, leakInterval time.Duration) *LeakyBucket
NewLeakyBucket 新建漏桶.
func (*LeakyBucket) Consume ¶
func (p *LeakyBucket) Consume(drop uint)
Consume 消费,如果无法消费,则 Sleep 直到可以消费.
func (*LeakyBucket) TryConsume ¶
func (p *LeakyBucket) TryConsume(drop uint) bool
TryConsume 尝试消费,消费失败返回 false.
type Limit ¶ added in v0.3.24
type Limit struct {
// contains filtered or unexported fields
}
Limit 频率限制.
type ProducerAndConsumer ¶ added in v0.3.22
type RoutineGroup ¶
type RoutineGroup struct {
// contains filtered or unexported fields
}
RoutineGroup 协程组,是sync.WaitGroup的增强版本.
Click to show internal directories.
Click to hide internal directories.