mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
ca6c20ebd9
Introduce internal/db/batchwriter as a reusable generic buffered writer for per-domain ClickHouse flush pipelines, with unit tests and default batch tuning aligned with audit log ingestion. Add clickhouse-batchwriter agent skill and cross-references in AGENTS.md and database-migration. Business layers are not wired yet.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package batchwriter
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultQueueSize = 10_000
|
|
defaultMaxBatchSize = 1_000
|
|
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
|
|
|
|
// FlushInterval triggers a time-based flush even when the batch is smaller.
|
|
FlushInterval time.Duration
|
|
}
|
|
|
|
// DefaultConfig returns production-friendly defaults aligned with audit log batching.
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
QueueSize: defaultQueueSize,
|
|
MaxBatchSize: defaultMaxBatchSize,
|
|
FlushInterval: defaultFlushEvery,
|
|
}
|
|
}
|
|
|
|
func (c Config) validate() error {
|
|
if c.QueueSize <= 0 {
|
|
return fmt.Errorf("batchwriter: queue size must be positive")
|
|
}
|
|
if c.MaxBatchSize <= 0 {
|
|
return fmt.Errorf("batchwriter: max batch size must be positive")
|
|
}
|
|
if c.FlushInterval <= 0 {
|
|
return fmt.Errorf("batchwriter: flush interval must be positive")
|
|
}
|
|
return nil
|
|
} |