syncs

package
v0.4.28 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 6 Imported by: 0

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 频率限制.

func NewLimit added in v0.3.24

func NewLimit(qps uint) *Limit

NewLimit 频率限制,qps每秒限制次数.

func (*Limit) Try added in v0.3.24

func (p *Limit) Try() error

Try 尝试执行.

func (*Limit) Wait added in v0.4.25

func (p *Limit) Wait()

Wait 等待执行.

type ProducerAndConsumer added in v0.3.22

type ProducerAndConsumer[E any] interface {
	Produc(produceChan chan E)
	Consum(num int, elems []E)
}

type RoutineGroup

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

RoutineGroup 协程组,是sync.WaitGroup的增强版本.

func NewRoutineGroup

func NewRoutineGroup(size uint) *RoutineGroup

NewRoutineGroup 协程组,控制协程总数量.

func (*RoutineGroup) Add

func (p *RoutineGroup) Add(delta uint)

Add 增加协程.

func (*RoutineGroup) Done

func (p *RoutineGroup) Done()

Done 协程完成.

func (*RoutineGroup) Wait

func (p *RoutineGroup) Wait()

Wait 等待全部完成.

Jump to

Keyboard shortcuts

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