batch

package module
v0.0.0-...-d8c507f Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 2 Imported by: 0

README

batch

License GoDoc travis Go Report Card coveralls

仅支持Go 1.18及以上版本, 因为泛型的原因。

批处理一组元素,或者等待一定的时间。

一旦元素达到设定的阈值,就进行处理,否则调用者就阻塞等待。

如果没有充足的元素需要处理,那么会把既有的元素处理完毕后继续等待.

如果设置了timeout,即使还没有达到设定的阈值,也会进行批处理。

使用这个库既可以避免CPU的无谓空转,也可以有效的进行成批数据的处理。

使用方法

    in := make(chan int, 100)
	go func() {
        for {
            // 往in中塞数据
            ......
        }
		
	}()

	
	Batch(in, 80, func(items []int) {
		// 处理一批数据
	})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Batch

func Batch[T any](in chan T, batchSize int, fn func(items []T)) error

Batch reads items from in channel, and prepares a batch of items to the handler. If you close the channel, Batch will drain all items and exit without any error. If there is no sufficient items, it handles current read items and waits for the next batch.

func BatchTimeout

func BatchTimeout[T any](in chan T, batchSize int, timeout time.Duration, fn func(items []T)) error

BatchTimeout reads items from in channel or tmeout, and then prepares a batch of items to the handler. Not like the Batch function, it waits until timeout if there is no sufficient items for batch.

Types

This section is empty.

Jump to

Keyboard shortcuts

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