Documentation
¶
Index ¶
- Variables
- type BatchQuery
- func (q *BatchQuery) Close() error
- func (q *BatchQuery) Error() error
- func (q *BatchQuery) Fetch(key any) (any, error)
- func (q *BatchQuery) FetchContext(key any, ctx context.Context) (any, error)
- func (q *BatchQuery) FetchDeadline(key any, deadline time.Time) (any, error)
- func (q *BatchQuery) FetchTimeout(key any, timeout time.Duration) (any, error)
- func (q *BatchQuery) ForceClose() error
- type Batcher
- type Config
- type DummyMetrics
- func (DummyMetrics) Batch()
- func (DummyMetrics) BatchFail()
- func (DummyMetrics) BatchOK(_ time.Duration)
- func (DummyMetrics) BufferIn(_ string)
- func (DummyMetrics) BufferOut()
- func (DummyMetrics) Fail()
- func (DummyMetrics) Fetch()
- func (DummyMetrics) Interrupt()
- func (DummyMetrics) NotFound()
- func (DummyMetrics) OK(_ time.Duration)
- func (DummyMetrics) Timeout()
- type Logger
- type MetricsWriter
- type Status
Constants ¶
This section is empty.
Variables ¶
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) 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 ¶
FetchContext add single request to current batch with context.
func (*BatchQuery) FetchDeadline ¶
FetchDeadline add single request to current batch using given deadline.
func (*BatchQuery) FetchTimeout ¶
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.
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 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.