mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-28 05:46:36 +08:00
699e95f12c
Result: {"status":"keep","total_issues":56,"golint_canonicalheader":8,"golint_errname":1,"golint_errorlint":12,"golint_forcetypeassert":3,"golint_gosec":0,"golint_intrange":3,"golint_modernize":5,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":3,"golint_recvcheck":7,"golint_usestdlibvars":3,"golint_wastedassign":7,"golint_total":55,"eslint_problems":1,"eslint_errors":0,"eslint_warnings":1,"tsc_errors":0,"measure_s":47}
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package batchwriter
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultQueueSize = 10_000
|
|
defaultMaxBatchSize = 1_000
|
|
defaultMinBatchSize = 50
|
|
defaultFlushEvery = time.Second
|
|
)
|
|
|
|
// Config controls queue capacity and flush thresholds for a Writer instance.
|
|
type Config struct {
|
|
// Name identifies the writer in logs and diagnostics. Optional.
|
|
Name string
|
|
|
|
// QueueSize is the buffered channel capacity.
|
|
QueueSize int
|
|
|
|
// MaxBatchSize triggers a flush when the in-memory batch reaches this count.
|
|
MaxBatchSize int
|
|
|
|
// MinBatchSize is the minimum in-memory batch size for time-based flushes.
|
|
// Zero disables the threshold and preserves legacy interval flush behavior.
|
|
// When set, interval flushes below this size are skipped unless MaxFlushWait elapses.
|
|
MinBatchSize int
|
|
|
|
// FlushInterval is how often the worker checks whether a time-based flush should run.
|
|
FlushInterval time.Duration
|
|
|
|
// MaxFlushWait forces a flush of any non-empty batch once the oldest item has waited
|
|
// this long, even if MinBatchSize has not been reached. Zero disables the force path.
|
|
MaxFlushWait time.Duration
|
|
}
|
|
|
|
// DefaultConfig returns production-friendly defaults aligned with audit log batching.
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
QueueSize: defaultQueueSize,
|
|
MaxBatchSize: defaultMaxBatchSize,
|
|
MinBatchSize: defaultMinBatchSize,
|
|
FlushInterval: defaultFlushEvery,
|
|
}
|
|
}
|
|
|
|
func (c Config) validate() error {
|
|
if c.QueueSize <= 0 {
|
|
return errors.New("batchwriter: queue size must be positive")
|
|
}
|
|
if c.MaxBatchSize <= 0 {
|
|
return errors.New("batchwriter: max batch size must be positive")
|
|
}
|
|
if c.MinBatchSize < 0 {
|
|
return errors.New("batchwriter: min batch size must be non-negative")
|
|
}
|
|
if c.FlushInterval <= 0 {
|
|
return errors.New("batchwriter: flush interval must be positive")
|
|
}
|
|
if c.MaxFlushWait < 0 {
|
|
return errors.New("batchwriter: max flush wait must be non-negative")
|
|
}
|
|
return nil
|
|
}
|