Documentation ¶
Overview ¶
Package batch contains a generic batch buffer, which accumulates multiple items into one slice and pass it into user defined callback.
Known issues
- goreport card does not support generics (yet);
- gomarkdoc does not support generics (yet);
- doc.go.dev does not support generics (yet);
Code quality politics
- no external non-test dependencies;
- code coverage >= 90% (~100% currently);
Index ¶
Examples ¶
Constants ¶
const DefaultSize uint = 64
DefaultSize is a default batch size.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[T any] struct { // contains filtered or unexported fields }
Batcher implements buffering (similar to bufio.Writer). Pushed values are accumulated in an internal buffer until Size number of values is reached. Then accumulated values are passed into the provided callback. If callback returns an error, then all subsequent calls of Push and Flush will return the same error until the Bather is reset with Reset.
Example ¶
package main import ( "context" "fmt" "strconv" "github.com/ninedraft/batch" ) func main() { var ctx = context.Background() var fn = func(_ context.Context, values []string) error { fmt.Println(values) return nil } var b = batch.New(4, fn) for i := 0; i < 15; i++ { if err := b.Push(ctx, strconv.Itoa(i)); err != nil { panic(err) } } if err := b.Flush(ctx); err != nil { panic(err) } }
Output: [0 1 2 3] [4 5 6 7] [8 9 10 11] [12 13 14]
func New ¶
New creates a new batcher with defined size and callback. If size is 0, then DefaultSize is used. If fn callback is nil, then Batcher may panic with future methods calls.
func (*Batcher[T]) Flush ¶
Flush passes internal buffer into the callback function and resets buffer. It returns resulting error, if got one.
func (*Batcher[T]) Push ¶
Push puts message into internal buffer. It will call the callback with provided context and filled buffer if len(buffer) > size. If callback is called, then Push returns resulting error. If callback is nil, then Push will panic.
func (*Batcher[T]) Reset ¶
Reset drops internal buffer and error.
Example ¶
package main import ( "context" "fmt" "github.com/ninedraft/batch" ) func main() { var ctx = context.Background() var fn = func(_ context.Context, values []string) error { fmt.Println(values) return nil } var b = batch.New(4, fn) if err := b.Push(ctx, "bad balue"); err != nil { panic(err) } b.Reset(fn) if err := b.Push(ctx, "good value"); err != nil { panic(err) } if err := b.Flush(ctx); err != nil { panic(err) } }
Output: [good value]