batch_query

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 6 Imported by: 1

README

Batch Query

Batch query collects single requests to batches and process that allows to reduce pressure to some resources (network, database, ...).

Usage

Example of usage allows in demo application.

Metrics

Check Prometheus implementation of metrics writer.

Config params

See source code

Modules

Currently, supports modules:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoConfig     = errors.New("no config provided")
	ErrNoWorkers    = errors.New("no workers available")
	ErrNoBatcher    = errors.New("no batcher provided")
	ErrBadIntervals = errors.New("bad intervals: timeout less that collect")
	ErrQueryNil     = errors.New("query not initialized")
	ErrQueryClosed  = errors.New("query closed")
	ErrNotFound     = errors.New("record not found")
	ErrInterrupt    = errors.New("interrupt")
	ErrTimeout      = errors.New("timeout")
)

Functions

This section is empty.

Types

type BatchQuery

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

BatchQuery is an implementation of query that collects single request to resource (database, network, ...) to batches and thus reduces pressure to resource.

func New

func New(conf *Config) (*BatchQuery, error)

New makes new query instance and initialize it according config params.

func (*BatchQuery) Close

func (q *BatchQuery) Close() error

Close gracefully stops the query.

func (*BatchQuery) Error

func (q *BatchQuery) Error() error

func (*BatchQuery) Fetch

func (q *BatchQuery) Fetch(key any) (any, error)

Fetch add single request to current batch using default timeout interval.

func (*BatchQuery) FetchContext

func (q *BatchQuery) FetchContext(key any, ctx context.Context) (any, error)

FetchContext add single request to current batch with context.

func (*BatchQuery) FetchDeadline

func (q *BatchQuery) FetchDeadline(key any, deadline time.Time) (any, error)

FetchDeadline add single request to current batch using given deadline.

func (*BatchQuery) FetchTimeout

func (q *BatchQuery) FetchTimeout(key any, timeout time.Duration) (any, error)

FetchTimeout add single request to current batch using given timeout interval.

func (*BatchQuery) ForceClose

func (q *BatchQuery) ForceClose() error

ForceClose closes the query and immediately flush all batches.

type Batcher

type Batcher interface {
	// Batch processes collected batch.
	Batch(dst []any, keys []any, ctx context.Context) ([]any, error)
	// MatchKey checks if key corresponds to val.
	MatchKey(key, val any) bool
}

Batcher describes object to process batches.

type Config

type Config struct {
	// Max capacity of one batch.
	// If query collects that amount of requests before reach CollectInterval then batch will process with reason
	// 'size reach'.
	// If this param omit defaultBatchSize (64) will use instead.
	BatchSize uint64
	// How long collect requests before process the batch.
	// Timer starts by first request incoming and stops after process the batch. After reach that interval betch will
	// process even contains only one request.
	// If this param omit defaultCollectInterval (1 second) will use instead.
	CollectInterval time.Duration
	// How long request may wait collecting and processing. Must be greater that CollectInterval.
	TimeoutInterval time.Duration
	// Internal workers count to process batches.
	Workers uint
	// Internal buffer size to collect batches.
	// If this param omit defaultBuffer (16) will use instead.
	Buffer uint64
	// Batch processor.
	// Mandatory param.
	Batcher Batcher

	// Metrics writer handler.
	MetricsWriter MetricsWriter

	// Logger handler.
	Logger Logger
}

Config describes query properties and behavior.

func (*Config) Copy

func (c *Config) Copy() *Config

type DummyMetrics

type DummyMetrics struct{}

DummyMetrics is a stub metrics writer handler that uses by default and does nothing. Need just to reduce checks in code.

func (DummyMetrics) Batch

func (DummyMetrics) Batch()

func (DummyMetrics) BatchFail

func (DummyMetrics) BatchFail()

func (DummyMetrics) BatchOK

func (DummyMetrics) BatchOK(_ time.Duration)

func (DummyMetrics) BufferIn

func (DummyMetrics) BufferIn(_ string)

func (DummyMetrics) BufferOut

func (DummyMetrics) BufferOut()

func (DummyMetrics) Fail

func (DummyMetrics) Fail()

func (DummyMetrics) Fetch

func (DummyMetrics) Fetch()

func (DummyMetrics) Interrupt

func (DummyMetrics) Interrupt()

func (DummyMetrics) NotFound

func (DummyMetrics) NotFound()

func (DummyMetrics) OK

func (DummyMetrics) OK(_ time.Duration)

func (DummyMetrics) Timeout

func (DummyMetrics) Timeout()

type Logger

type Logger interface {
	Printf(format string, v ...any)
	Print(v ...any)
	Println(v ...any)
}

Logger is an interface of logger interface. Prints verbose messages.

type MetricsWriter

type MetricsWriter interface {
	// Fetch registers income single request.
	Fetch()
	// OK register successful processing of single request.
	OK(duration time.Duration)
	// NotFound registers single request failed due to empty response.
	NotFound()
	// Timeout registers single request filed due to timeout error.
	Timeout()
	// Interrupt registers single request failed due to interrupt signal.
	Interrupt()
	// Fail registers any other error encountered.
	Fail()
	// Batch register processing start of batch.
	Batch()
	// BatchOK register successful processing of batch.
	BatchOK(duration time.Duration)
	// BatchFail registers failed batch processing.
	BatchFail()
	// BufferIn registers incoming of new batch to internal buffer.
	BufferIn(reason string)
	// BufferOut registers outcoming of batch from internal buffer.
	BufferOut()
}

MetricsWriter is an interface of query metrics handler. See example of implementations https://github.com/koykov/metrics_writers/tree/master/batch_query.

type Status

type Status uint32
const (
	StatusNil Status = iota
	StatusFail
	StatusActive
	StatusThrottle
	StatusClose
)

Directories

Path Synopsis
metrics
log module
prometheus module
mods
aerospike module
redis module
sql module

Jump to

Keyboard shortcuts

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