batch

package module
v0.1.1-0...-7ef84b4 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README

Go Report Card GitHub Actions Go Reference

batch

import "github.com/ninedraft/batch"

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

Constants

DefaultSize is a default batch size.

const DefaultSize uint = 64

type Batcher

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.

type Batcher[T any] struct {
    // contains filtered or unexported fields
}
Example

package main

import (
	"context"
	"fmt"
	"github.com/ninedraft/batch"
	"strconv"
)

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
func New[T any](size uint, fn Callback[T]) *Batcher[T]

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 (*BADRECV) Flush
func (b *Batcher[T]) Flush(ctx context.Context) error

Flush passes internal buffer into the callback function and resets buffer. It returns resulting error, if got one.

func (*BADRECV) Push
func (b *Batcher[T]) Push(ctx context.Context, v T) error

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 (*BADRECV) Reset
func (b *Batcher[T]) Reset(fn Callback[T])

Reset drops internal buffer and error.

func (*BADRECV) Size
func (b *Batcher[T]) Size() uint

Size returns internal buffer size.

type Callback

Callback describes a value consuming function. Values slice must be used only while function execution and must not be shared between calls.

type Callback[T any] func(ctx context.Context, values []T) error

Generated by gomarkdoc

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

View Source
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

func New[T any](size uint, fn Callback[T]) *Batcher[T]

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

func (b *Batcher[T]) Flush(ctx context.Context) error

Flush passes internal buffer into the callback function and resets buffer. It returns resulting error, if got one.

func (*Batcher[T]) Push

func (b *Batcher[T]) Push(ctx context.Context, v T) error

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

func (b *Batcher[T]) Reset(fn Callback[T])

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]

func (*Batcher[T]) Size

func (b *Batcher[T]) Size() uint

Size returns internal buffer size.

type Callback

type Callback[T any] func(ctx context.Context, values []T) error

Callback describes a value consuming function. Values slice must be used only while function execution and must not be shared between calls.

Jump to

Keyboard shortcuts

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