goaccum

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 5 Imported by: 0

README

go-accumulator

Go Reference Go Report Card codecov

Solution for accumulation of events and their subsequent processing.

Logo
go get github.com/nar10z/go-accumulator

What for?

Sometimes there is a situation where processing data on 1 item is too long. The go-accumulator package comes to the rescue!

The solution is to accumulate the data and then process it in a batch. There are 2 situations where the processing function (flushFunc) is called:

  • Storage fills up to the maximum value (flushSize).
  • The interval during which the data is accumulated (flushInterval) passes

The accumulator provides 2 methods:

  • AddAsync - adding data without waiting for execution
  • AddSync - adding data with a wait for execution

Example

package main

import (
	"context"
	"fmt"
	"strings"
	"sync"
	"time"

	goaccum "github.com/nar10z/go-accumulator"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()

	const (
		countSync  = 4
		countAsync = 3
	)

	accumulator := goaccum.New[string](3, time.Second, func(events []string) error {
		fmt.Printf("Start flush %d events:\n", len(events))
		for _, e := range events {
			fmt.Printf(" - %s\n", e)
		}
		fmt.Printf("Finish\n%s\n", strings.Repeat("-", 20))
		return nil
	})

	var wg sync.WaitGroup
	wg.Add(countSync + countAsync)

	go func() {
		for i := 0; i < countAsync; i++ {
			err := accumulator.AddAsync(ctx, fmt.Sprintf("async #%d", i))
			if err != nil {
				fmt.Printf("failed add event: %v\n", err)
			}
			wg.Done()
		}
	}()

	go func() {
		for i := 0; i < countSync; i++ {
			i := i
			go func() {
				err := accumulator.AddSync(ctx, fmt.Sprintf("sync #%d", i))
				if err != nil {
					fmt.Printf("failed add event: %v\n", err)
				}
				wg.Done()
			}()
		}
	}()

	wg.Wait()

	accumulator.Stop()
}

output:
Start flush 3 events:
 - sync #3
 - async #0
 - async #1
Finish
--------------------
Start flush 3 events:
 - async #2
 - sync #0
 - sync #1
Finish
--------------------
Start flush 1 events:
 - sync #2
Finish
--------------------

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSendToClose ...
	ErrSendToClose = errors.New("send to close accumulator")
)

Functions

This section is empty.

Types

type Accumulator

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

func New

func New[T any](
	flushSize uint,
	flushInterval time.Duration,
	flushFunc FlushExec[T],
) *Accumulator[T]

New creates a new data Accumulator

func (*Accumulator[T]) AddAsync

func (a *Accumulator[T]) AddAsync(ctx context.Context, event T) error

func (*Accumulator[T]) AddSync

func (a *Accumulator[T]) AddSync(ctx context.Context, event T) error

func (*Accumulator[T]) Stop

func (a *Accumulator[T]) Stop()

type FlushExec

type FlushExec[T any] func(events []T) error

FlushExec a function to call when an action needs to be performed

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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