Documentation
¶
Index ¶
- Constants
- Variables
- func ClassifyError(err error) (retryable bool, reason string)
- func FingerprintSQL(sqlText string) string
- func FingerprintText(text string) string
- func NewBatchFlowWithMock(ctx context.Context, config PipelineConfig) (*BatchFlow, *MockExecutor)
- func NewBatchFlowWithMockDriver(ctx context.Context, config PipelineConfig, sqlDriver SQLDriver) (*BatchFlow, *MockExecutor)
- func OperationFingerprint(parts ...string) string
- func RegisterErrorClassifier(classifier ErrorClassifier) func()
- type AdaptiveTuner
- type AdaptiveTuningConfig
- type BackpressureConfig
- type BackpressureMode
- type Batch
- type BatchError
- type BatchEvent
- type BatchExecutor
- type BatchFlow
- func NewBatchFlow(ctx context.Context, buffSize uint32, flushSize uint32, ...) *BatchFlow
- func NewBatchFlowWithConfig(ctx context.Context, config BatchFlowConfig) (*BatchFlow, error)
- func NewMySQLBatchFlow(ctx context.Context, db *sql.DB, config PipelineConfig) *BatchFlow
- func NewPostgreSQLBatchFlow(ctx context.Context, db *sql.DB, config PipelineConfig) *BatchFlow
- func NewRedisBatchFlow(ctx context.Context, db *redisV9.Client, config PipelineConfig) *BatchFlow
- func NewRedisBatchFlowWithDriver(ctx context.Context, db *redisV9.Client, config PipelineConfig, ...) *BatchFlow
- func NewSQLBatchFlowWithDriver(ctx context.Context, db *sql.DB, config PipelineConfig, driver SQLDriver) *BatchFlow
- func NewSQLiteBatchFlow(ctx context.Context, db *sql.DB, config PipelineConfig) *BatchFlow
- type BatchFlowConfig
- type BatchFlowMetricsReporter
- type BatchProcessor
- type CoalesceResult
- type CoalesceStrategy
- type Coalescer
- type CoalescerFunc
- type ConcurrencyCapable
- type Config
- type ConfigError
- type ConflictStrategy
- type CopyFromClient
- type CopyFromExecutor
- type ErrorClassifier
- type ErrorClassifierFunc
- type Flow
- type KeyCoalescer
- type KeyFunc
- type MemoryLimitConfig
- type MetricsCapable
- type MetricsReporter
- type MockDriver
- type MockExecutor
- type MySQLDriver
- type NoopMetricsReporter
- func (*NoopMetricsReporter) DecInflight()
- func (*NoopMetricsReporter) IncError(string, string)
- func (*NoopMetricsReporter) IncInflight()
- func (*NoopMetricsReporter) ObserveBatchAssemble(time.Duration)
- func (*NoopMetricsReporter) ObserveBatchSize(int)
- func (*NoopMetricsReporter) ObserveEnqueueLatency(time.Duration)
- func (*NoopMetricsReporter) ObserveExecuteDuration(string, int, time.Duration, string)
- func (*NoopMetricsReporter) SetConcurrency(int)
- func (*NoopMetricsReporter) SetQueueLength(int)
- type ObservabilityConfig
- type Observer
- type ObserverFunc
- type OperationMetricsReporter
- type OperationPreview
- type OperationPreviewer
- type Operations
- type PipelineConfig
- type PipelineMetricsReporter
- type PostgreSQLDriver
- type Record
- type Redactor
- type RedactorFunc
- type RedisBatchProcessor
- func (rp *RedisBatchProcessor) ExecuteOperations(ctx context.Context, operations Operations) error
- func (rp *RedisBatchProcessor) GenerateOperationPreview(ctx context.Context, schema SchemaInterface, data []map[string]any) (Operations, OperationPreview, error)
- func (rp *RedisBatchProcessor) GenerateOperations(ctx context.Context, schema SchemaInterface, data []map[string]any) (operations Operations, err error)
- func (rp *RedisBatchProcessor) WithTimeout(timeout time.Duration) *RedisBatchProcessor
- type RedisCmd
- type RedisDriver
- type RedisPipelineDriver
- type Request
- func (r *Request) Columns() map[string]any
- func (r *Request) GetBool(colName string) (bool, error)
- func (r *Request) GetFloat64(colName string) (float64, error)
- func (r *Request) GetInt32(colName string) (int32, error)
- func (r *Request) GetInt64(colName string) (int64, error)
- func (r *Request) GetOrderedValues() []any
- func (r *Request) GetString(colName string) (string, error)
- func (r *Request) GetTime(colName string) (time.Time, error)
- func (r *Request) Schema() SchemaInterface
- func (r *Request) Set(colName string, value any) *Request
- func (r *Request) SetBool(colName string, value bool) *Request
- func (r *Request) SetBytes(colName string, value []byte) *Request
- func (r *Request) SetFloat32(colName string, value float32) *Request
- func (r *Request) SetFloat64(colName string, value float64) *Request
- func (r *Request) SetInt(colName string, value int) *Request
- func (r *Request) SetInt8(colName string, value int8) *Request
- func (r *Request) SetInt16(colName string, value int16) *Request
- func (r *Request) SetInt32(colName string, value int32) *Request
- func (r *Request) SetInt64(colName string, value int64) *Request
- func (r *Request) SetNull(colName string) *Request
- func (r *Request) SetString(colName string, value string) *Request
- func (r *Request) SetTime(colName string, value time.Time) *Request
- func (r *Request) SetUint(colName string, value uint) *Request
- func (r *Request) SetUint8(colName string, value uint8) *Request
- func (r *Request) SetUint16(colName string, value uint16) *Request
- func (r *Request) SetUint32(colName string, value uint32) *Request
- func (r *Request) SetUint64(colName string, value uint64) *Request
- func (r *Request) Validate() error
- type RetryConfig
- type RuntimeConfig
- type RuntimeEngine
- type SQLBatchProcessor
- func (bp *SQLBatchProcessor) ExecuteOperations(ctx context.Context, operations Operations) error
- func (bp *SQLBatchProcessor) GenerateOperationPreview(ctx context.Context, schema SchemaInterface, data []map[string]any) (Operations, OperationPreview, error)
- func (bp *SQLBatchProcessor) GenerateOperations(ctx context.Context, schema SchemaInterface, data []map[string]any) (operations Operations, err error)
- func (bp *SQLBatchProcessor) GenerateSQLPreview(ctx context.Context, schema *SQLSchema, data []map[string]any) (SQLPreview, error)
- func (bp *SQLBatchProcessor) WithTimeout(timeout time.Duration) *SQLBatchProcessor
- type SQLDedupStats
- type SQLDriver
- type SQLError
- type SQLMetricsReporter
- type SQLOperationConfig
- type SQLPreview
- type SQLSchema
- type SQLStage
- type SQLiteDriver
- type Sampler
- type SamplerFunc
- type Schema
- type SchemaInterface
- type ShardKeyFunc
- type ShardRoutingPolicy
- type ThrottledBatchExecutor
- func NewRedisThrottledBatchExecutor(client *redis.Client) *ThrottledBatchExecutor
- func NewRedisThrottledBatchExecutorWithDriver(client *redis.Client, driver RedisDriver) *ThrottledBatchExecutor
- func NewSQLThrottledBatchExecutorWithDriver(db *sql.DB, driver SQLDriver) *ThrottledBatchExecutor
- func NewThrottledBatchExecutor(processor BatchProcessor) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) ExecuteBatch(ctx context.Context, schema SchemaInterface, data []map[string]any) error
- func (e *ThrottledBatchExecutor) MetricsReporter() MetricsReporter
- func (e *ThrottledBatchExecutor) Observer() Observer
- func (e *ThrottledBatchExecutor) WithCoalescer(coalescer Coalescer) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) WithConcurrencyLimit(limit int) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) WithMetricsReporter(metricsReporter MetricsReporter) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) WithObservability(config ObservabilityConfig) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) WithObserver(observer Observer) *ThrottledBatchExecutor
- func (e *ThrottledBatchExecutor) WithRetryConfig(cfg RetryConfig) *ThrottledBatchExecutor
- type TimeOutCapable
- type TuningRecommendation
- type TuningSignal
Examples ¶
Constants ¶
const ( ErrorReasonUnknown = "unknown" ErrorReasonContextCanceled = "context_canceled" ErrorReasonContextDeadline = "context_deadline" ErrorReasonDuplicateKey = "duplicate_key" ErrorReasonDeadlock = "deadlock" ErrorReasonLockTimeout = "lock_timeout" ErrorReasonTimeout = "timeout" ErrorReasonConnection = "connection" ErrorReasonIO = "io" ErrorReasonSyntax = "syntax" ErrorReasonNonRetryable = "non_retryable" )
const ( BackendSQL = "sql" BackendRedis = "redis" BackendCustom = "custom" OperationInsert = "insert" OperationUpsert = "upsert" OperationCommand = "command" OperationCustom = "custom" BatchStageValidate = "validate" BatchStageGenerate = "generate" BatchStageExecute = "execute" BatchStageRetry = "retry" BatchStageFinal = "final" )
Variables ¶
var ( // ErrEmptyRequest 空请求错误 ErrEmptyRequest = errors.New("empty request") // ErrContextCanceled 上下文被取消错误 ErrContextCanceled = errors.New("context canceled") // ErrInvalidSchema 无效的 schema 错误 ErrInvalidSchema = errors.New("invalid schema") // ErrMissingColumn 缺少列错误 ErrMissingColumn = errors.New("missing required column") // ErrInvalidColumnType 无效的列类型错误 ErrInvalidColumnType = errors.New("invalid column type") // ErrEmptyBatch 空批次错误 ErrEmptyBatch = errors.New("empty batch") // ErrEmptySchemaName 空表名错误 ErrEmptySchemaName = errors.New("empty schema name") // ErrBackpressure 表示队列达到反压阈值且策略拒绝或等待超时 ErrBackpressure = errors.New("batchflow backpressure limit reached") )
var ConflictIgnoreOperationConfig = SQLOperationConfig{ ConflictStrategy: ConflictIgnore, }
var ConflictReplaceOperationConfig = SQLOperationConfig{ ConflictStrategy: ConflictReplace, }
var ConflictUpdateOperationConfig = SQLOperationConfig{ ConflictStrategy: ConflictUpdate, }
var DefaultMySQLDriver = NewMySQLDriver()
var DefaultOperationConfig = SQLOperationConfig{ ConflictStrategy: ConflictIgnore, }
var DefaultPostgreSQLDriver = NewPostgreSQLDriver()
var DefaultRedisPipelineDriver = NewRedisPipelineDriver()
var DefaultSQLiteDriver = NewSQLiteDriver()
var ErrCopyFromUnsupportedOperation = errors.New("copy from supports append-only SQL schemas only")
var ErrMemoryLimitExceeded = errors.New("batchflow memory limit exceeded")
Functions ¶
func ClassifyError ¶
ClassifyError returns a low-cardinality retry decision and reason label for logs and metrics. It unwraps BatchError and SQLError before classifying the underlying cause.
func FingerprintSQL ¶
func FingerprintText ¶
func NewBatchFlowWithMock ¶
func NewBatchFlowWithMock(ctx context.Context, config PipelineConfig) (*BatchFlow, *MockExecutor)
NewBatchFlowWithMock 使用模拟执行器创建 BatchFlow 实例(用于测试) 内部架构:BatchFlow -> MockExecutor(直接实现BatchExecutor,无真实数据库操作) 适用于单元测试,不依赖真实数据库连接
func NewBatchFlowWithMockDriver ¶
func NewBatchFlowWithMockDriver(ctx context.Context, config PipelineConfig, sqlDriver SQLDriver) (*BatchFlow, *MockExecutor)
NewBatchFlowWithMockDriver 使用模拟执行器创建 BatchFlow 实例(测试特定SQLDriver) 内部架构:BatchFlow -> MockExecutor(模拟ThrottledBatchExecutor行为,测试SQLDriver逻辑) 适用于测试自定义SQLDriver的SQL生成逻辑
func OperationFingerprint ¶
func RegisterErrorClassifier ¶
func RegisterErrorClassifier(classifier ErrorClassifier) func()
RegisterErrorClassifier adds a custom classifier after built-in structured classifiers and before the string fallback. The returned function unregisters it.
Types ¶
type AdaptiveTuner ¶
type AdaptiveTuner struct {
// contains filtered or unexported fields
}
AdaptiveTuner is a stateless policy engine.
func NewAdaptiveTuner ¶
func NewAdaptiveTuner(cfg AdaptiveTuningConfig) *AdaptiveTuner
func (*AdaptiveTuner) Tune ¶
func (t *AdaptiveTuner) Tune(s TuningSignal) TuningRecommendation
type AdaptiveTuningConfig ¶
type AdaptiveTuningConfig struct {
Enabled bool
MinFlushSize uint32
MaxFlushSize uint32
MinFlushInterval time.Duration
MaxFlushInterval time.Duration
ScaleUpQueueDepth int
ScaleDownQueueDepth int
TargetLatency time.Duration
}
AdaptiveTuningConfig controls runtime auto tuning behavior.
type BackpressureConfig ¶
type BackpressureConfig struct {
Enabled bool
Mode BackpressureMode
HighWatermark int
CheckInterval time.Duration
Timeout time.Duration
}
BackpressureConfig controls queue pressure.
type BackpressureMode ¶
type BackpressureMode uint8
BackpressureMode controls queue pressure behavior.
const ( BackpressureBlock BackpressureMode = iota BackpressureReject BackpressureTimeout )
type Batch ¶
type Batch = []Record
Batch is the backend-neutral batch payload passed through BatchFlow.
type BatchError ¶
type BatchError struct {
Stage string
Backend string
Schema string
BatchSize int
Fingerprint string
Attributes map[string]any
Cause error
}
BatchError wraps backend-neutral batch failures with safe diagnostic metadata.
func (*BatchError) Error ¶
func (e *BatchError) Error() string
func (*BatchError) Unwrap ¶
func (e *BatchError) Unwrap() error
type BatchEvent ¶
type BatchExecutor ¶
type BatchExecutor interface {
// ExecuteBatch 执行批量操作
ExecuteBatch(ctx context.Context, schema SchemaInterface, data []map[string]any) error
}
BatchExecutor 批量执行器接口 - 所有数据库驱动的统一入口
type BatchFlow ¶
type BatchFlow struct {
// contains filtered or unexported fields
}
BatchFlow 批量处理管道 核心组件,整合 go-pipeline 和 BatchExecutor,提供统一的批量处理接口
架构层次: Application -> BatchFlow -> gopipeline -> BatchExecutor -> Database
支持的 BatchExecutor 实现: - SQL 数据库:ThrottledBatchExecutor + SQLBatchProcessor + SQLDriver - NoSQL 数据库:ThrottledBatchExecutor + RedisBatchProcessor + RedisDriver(直接生成/执行命令) - 测试环境:MockExecutor(直接实现 BatchExecutor) 可选能力: - WithConcurrencyLimit:通过信号量限制 ExecuteBatch 并发,避免攒批后同时冲击数据库(limit <= 0 等价于不限流)
func NewBatchFlow ¶
func NewBatchFlow(ctx context.Context, buffSize uint32, flushSize uint32, flushInterval time.Duration, executor BatchExecutor) *BatchFlow
NewBatchFlow 创建 BatchFlow 实例 这是最底层的构造函数,接受任何实现了BatchExecutor接口的执行器 通常不直接使用,而是通过具体数据库的工厂方法创建
func NewBatchFlowWithConfig ¶
func NewBatchFlowWithConfig(ctx context.Context, config BatchFlowConfig) (*BatchFlow, error)
func NewMySQLBatchFlow ¶
NewMySQLBatchFlow 创建MySQL BatchFlow实例(使用默认Driver)
内部架构:BatchFlow -> ThrottledBatchExecutor -> SQLBatchProcessor -> MySQLDriver -> MySQL
这是推荐的使用方式,使用MySQL优化的默认配置
func NewPostgreSQLBatchFlow ¶
NewPostgreSQLBatchFlow 创建PostgreSQL BatchFlow实例(使用默认Driver)
func NewRedisBatchFlow ¶
NewRedisBatchFlow 创建Redis BatchFlow实例
内部架构(NoSQL):BatchFlow -> ThrottledBatchExecutor -> RedisBatchProcessor -> RedisDriver -> Redis 说明:NoSQL 路径不使用 SQL 抽象层,直接生成并执行 Redis 命令;仍可启用 WithConcurrencyLimit 控制批次并发。
func NewRedisBatchFlowWithDriver ¶
func NewRedisBatchFlowWithDriver(ctx context.Context, db *redisV9.Client, config PipelineConfig, driver RedisDriver) *BatchFlow
func NewSQLBatchFlowWithDriver ¶
func NewSQLBatchFlowWithDriver(ctx context.Context, db *sql.DB, config PipelineConfig, driver SQLDriver) *BatchFlow
NewSQLBatchFlow 创建SQL BatchFlow实例(使用自定义Driver)
func NewSQLiteBatchFlow ¶
NewSQLiteBatchFlow 创建SQLite BatchFlow实例(使用默认Driver)
func (*BatchFlow) Close ¶
Close 停止接收新请求,触发最终 flush,并等待后台 pipeline 退出。 它是幂等的;首次调用会关闭内部数据通道,后续调用仅等待同一个退出结果。
Example ¶
package main
import (
"context"
"time"
"github.com/rushairer/batchflow/v2"
)
func main() {
ctx := context.Background()
flow, _ := batchflow.NewBatchFlowWithMock(ctx, batchflow.PipelineConfig{
BufferSize: 16,
FlushSize: 100,
FlushInterval: time.Second,
})
schema := batchflow.NewSQLSchema("users", batchflow.ConflictIgnoreOperationConfig, "id", "name")
req := batchflow.NewRequest(schema).
SetUint64("id", 1).
SetString("name", "alice")
if err := flow.Submit(ctx, req); err != nil {
panic(err)
}
if err := flow.Close(); err != nil {
panic(err)
}
}
Output:
func (*BatchFlow) Done ¶
func (b *BatchFlow) Done() <-chan struct{}
Done 返回一个在 BatchFlow 后台 goroutine 退出时关闭的信号。
Example ¶
package main
import (
"context"
"time"
"github.com/rushairer/batchflow/v2"
)
func main() {
ctx := context.Background()
flow, _ := batchflow.NewBatchFlowWithMock(ctx, batchflow.PipelineConfig{
BufferSize: 16,
FlushSize: 100,
FlushInterval: time.Second,
})
stopped := make(chan struct{})
go func() {
<-flow.Done()
close(stopped)
}()
if err := flow.Close(); err != nil {
panic(err)
}
<-stopped
}
Output:
type BatchFlowConfig ¶
type BatchFlowConfig struct {
Pipeline PipelineConfig
Executor BatchExecutor
}
BatchFlowConfig is the v2 constructor config for a fully assembled BatchFlow.
func DefaultBatchFlowConfig ¶
func DefaultBatchFlowConfig(executor BatchExecutor) BatchFlowConfig
type BatchFlowMetricsReporter ¶
type BatchFlowMetricsReporter interface {
// IncSubmitRejected 记录 Submit 被拒绝的次数与原因。
IncSubmitRejected(reason string)
// ObservePipelineFlushSize 记录一次 pipeline flush 收到的请求数。
ObservePipelineFlushSize(n int)
// ObserveSchemaGroupsPerFlush 记录一次 flush 被拆成的 schema 组数。
ObserveSchemaGroupsPerFlush(n int)
}
BatchFlowMetricsReporter 是 BatchFlow 自身流程指标的可选扩展接口。 这些指标不属于执行器层,也不依赖 go-pipeline 的原生 MetricsHook, 仅在实现方显式选择时才会上报。
type BatchProcessor ¶
type BatchProcessor interface {
// GenerateOperations 生成批量操作
GenerateOperations(ctx context.Context, schema SchemaInterface, data []map[string]any) (operations Operations, err error)
// ExecuteOperations 执行批量操作
ExecuteOperations(ctx context.Context, operations Operations) error
}
BatchProcessor 批量处理器接口 - SQL数据库的核心处理逻辑
type CoalesceResult ¶
type CoalesceResult struct {
Batch Batch
InputItems int
OutputItems int
DeduplicatedItems int
MergedItems int
}
CoalesceResult contains the coalesced batch and low-cardinality statistics.
func NewCoalesceResult ¶
func NewCoalesceResult(batch Batch) CoalesceResult
type CoalesceStrategy ¶
type CoalesceStrategy string
CoalesceStrategy controls how duplicate keys are handled inside one batch.
const ( CoalesceDisabled CoalesceStrategy = "disabled" CoalesceKeepFirst CoalesceStrategy = "keep_first" CoalesceKeepLast CoalesceStrategy = "keep_last" CoalesceMergePresentFields CoalesceStrategy = "merge_present_fields" )
type Coalescer ¶
type Coalescer interface {
Coalesce(ctx context.Context, schema SchemaInterface, batch Batch) (CoalesceResult, error)
}
Coalescer merges or removes duplicate items before operation generation.
type CoalescerFunc ¶
type CoalescerFunc func(context.Context, SchemaInterface, Batch) (CoalesceResult, error)
func (CoalescerFunc) Coalesce ¶
func (f CoalescerFunc) Coalesce(ctx context.Context, schema SchemaInterface, batch Batch) (CoalesceResult, error)
type ConcurrencyCapable ¶
type Config ¶
type Config struct {
Pipeline PipelineConfig
Runtime RuntimeConfig
Executor BatchExecutor
}
Config is the stable public constructor config for the v2 module.
func DefaultConfig ¶
func DefaultConfig(executor BatchExecutor) Config
type ConfigError ¶
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
func (*ConfigError) Unwrap ¶
func (e *ConfigError) Unwrap() error
type ConflictStrategy ¶
type ConflictStrategy uint8
ConflictStrategy 冲突处理策略
const ( ConflictIgnore ConflictStrategy = iota ConflictReplace ConflictUpdate )
type CopyFromClient ¶
type CopyFromClient interface {
CopyFrom(ctx context.Context, table string, columns []string, rows [][]any) (int64, error)
}
CopyFromClient is the minimal adapter surface required by CopyFromExecutor. It intentionally avoids importing pgx in the root module.
type CopyFromExecutor ¶
type CopyFromExecutor struct {
// contains filtered or unexported fields
}
CopyFromExecutor is a zero-SQL-generation backend for append-only ingestion.
func NewCopyFromExecutor ¶
func NewCopyFromExecutor(client CopyFromClient) *CopyFromExecutor
func (*CopyFromExecutor) ExecuteBatch ¶
func (e *CopyFromExecutor) ExecuteBatch(ctx context.Context, schema SchemaInterface, data []map[string]any) error
func (*CopyFromExecutor) WithTimeout ¶
func (e *CopyFromExecutor) WithTimeout(timeout time.Duration) *CopyFromExecutor
type ErrorClassifier ¶
ErrorClassifier recognizes backend-specific errors and returns a low-cardinality reason.
type ErrorClassifierFunc ¶
type KeyCoalescer ¶
type KeyCoalescer struct {
Strategy CoalesceStrategy
KeyColumns []string
KeyFunc KeyFunc
}
KeyCoalescer is a general key-based coalescer for custom backends.
func NewKeyCoalescer ¶
func NewKeyCoalescer(strategy CoalesceStrategy, keyColumns ...string) *KeyCoalescer
NewKeyCoalescer creates a key-based coalescer using schema field names.
func (*KeyCoalescer) Coalesce ¶
func (c *KeyCoalescer) Coalesce(ctx context.Context, schema SchemaInterface, batch Batch) (CoalesceResult, error)
func (*KeyCoalescer) WithKeyFunc ¶
func (c *KeyCoalescer) WithKeyFunc(fn KeyFunc) *KeyCoalescer
WithKeyFunc overrides column-based key construction.
type KeyFunc ¶
type KeyFunc func(schema SchemaInterface, record Record) (key string, ok bool)
KeyFunc returns a stable duplicate key for a record.
type MemoryLimitConfig ¶
type MemoryLimitConfig struct {
Enabled bool
MaxQueueBytes int64
AvgRequestBytes int64
Mode BackpressureMode
CheckInterval time.Duration
Timeout time.Duration
}
MemoryLimitConfig provides OOM protection for runtime queue.
type MetricsCapable ¶
type MetricsCapable[T any] interface { // WithMetricsReporter 设置性能监控报告器(返回实现者类型以支持链式) WithMetricsReporter(MetricsReporter) T // MetricsReporter 返回当前性能监控报告器(可能为 nil) MetricsReporter() MetricsReporter }
Metrics 相关接口设计说明
- BatchExecutor:仅负责“执行”职责,保持最小接口。
- MetricsCapable[T](泛型):提供“读 + 写”的度量能力,方法返回自类型 T,便于在具体类型或已实例化接口上进行链式配置。 注意:泛型接口在运行时类型断言时必须使用具体的类型实参(如 MetricsCapable[*ThrottledBatchExecutor])。
- 兼容性与运行时探测: 在 BatchFlow 等仅持有 BatchExecutor 的位置,无法统一断言到 MetricsCapable[T], 因此使用一个非泛型只读探测接口(即仅调用 MetricsReporter())来判断是否已有 Reporter; 若返回 nil,则在本地使用 Noop 兜底,不强制写回(写回需要具体类型 T)。
这允许: - 在需要链式的调用点:以具体类型或已实例化能力接口使用 Set/With 风格链式; - 在框架内部通用路径:通过只读探测保证安全、零开销的观测兜底。
MetricsCapable 扩展接口:支持性能监控报告器(自类型泛型)
type MetricsReporter ¶
type MetricsReporter interface {
// 阶段耗时
ObserveEnqueueLatency(d time.Duration) // Submit -> 入队
ObserveBatchAssemble(d time.Duration) // 攒批/组装
// ObserveExecuteDuration reports execution duration for a schema/table batch.
// The first argument is historically named table in implementations; non-SQL
// backends should pass the schema name.
ObserveExecuteDuration(table string, n int, d time.Duration, status string)
// 其他观测
ObserveBatchSize(n int)
IncError(table string, typ string)
SetConcurrency(n int)
SetQueueLength(n int)
// 在途批次数(不限流也可观察执行压力)
IncInflight()
DecInflight()
}
MetricsReporter 统一指标接口(默认 Noop 实现,避免启用前引入开销)
type MockDriver ¶
type MockDriver struct {
// contains filtered or unexported fields
}
func NewMockDriver ¶
func NewMockDriver(databaseType string) *MockDriver
type MockExecutor ¶
type MockExecutor struct {
ExecutedBatches [][]map[string]any
// contains filtered or unexported fields
}
Executor 模拟批量执行器(用于测试)
func NewMockExecutorWithDriver ¶
func NewMockExecutorWithDriver(driver SQLDriver) *MockExecutor
NewMockExecutorWithDriver 创建模拟批量执行器(使用自定义Driver)
func (*MockExecutor) ExecuteBatch ¶
func (e *MockExecutor) ExecuteBatch(ctx context.Context, schema SchemaInterface, data []map[string]any) error
ExecuteBatch 模拟执行批量操作
func (*MockExecutor) SnapshotExecutedBatches ¶
func (e *MockExecutor) SnapshotExecutedBatches() [][]map[string]any
SnapshotExecutedBatches 返回一次性快照,避免并发读写竞态
func (*MockExecutor) SnapshotResults ¶
func (e *MockExecutor) SnapshotResults() map[string]map[string]int64
SnapshotResults 返回只读快照(拷贝),用于测试收尾输出或断言
type MySQLDriver ¶
type MySQLDriver struct {
// contains filtered or unexported fields
}
func NewMySQLDriver ¶
func NewMySQLDriver() *MySQLDriver
type NoopMetricsReporter ¶
type NoopMetricsReporter struct{}
NoopMetricsReporter 默认关闭时的无操作实现(零开销路径)
func NewNoopMetricsReporter ¶
func NewNoopMetricsReporter() *NoopMetricsReporter
func (*NoopMetricsReporter) DecInflight ¶
func (*NoopMetricsReporter) DecInflight()
func (*NoopMetricsReporter) IncError ¶
func (*NoopMetricsReporter) IncError(string, string)
func (*NoopMetricsReporter) IncInflight ¶
func (*NoopMetricsReporter) IncInflight()
func (*NoopMetricsReporter) ObserveBatchAssemble ¶
func (*NoopMetricsReporter) ObserveBatchAssemble(time.Duration)
func (*NoopMetricsReporter) ObserveBatchSize ¶
func (*NoopMetricsReporter) ObserveBatchSize(int)
func (*NoopMetricsReporter) ObserveEnqueueLatency ¶
func (*NoopMetricsReporter) ObserveEnqueueLatency(time.Duration)
func (*NoopMetricsReporter) ObserveExecuteDuration ¶
func (*NoopMetricsReporter) SetConcurrency ¶
func (*NoopMetricsReporter) SetConcurrency(int)
func (*NoopMetricsReporter) SetQueueLength ¶
func (*NoopMetricsReporter) SetQueueLength(int)
type ObservabilityConfig ¶
type Observer ¶
type Observer interface {
OnBatchEvent(ctx context.Context, event BatchEvent)
}
type ObserverFunc ¶
type ObserverFunc func(context.Context, BatchEvent)
func (ObserverFunc) OnBatchEvent ¶
func (f ObserverFunc) OnBatchEvent(ctx context.Context, event BatchEvent)
type OperationMetricsReporter ¶
type OperationMetricsReporter interface {
ObserveOperationGenerated(preview OperationPreview)
IncOperationError(schema string, backend string, stage string, reason string)
}
OperationMetricsReporter is the preferred backend-neutral extension for generated operation diagnostics. Implementations should keep labels low-cardinality and never use raw payloads as labels.
type OperationPreview ¶
type OperationPreview struct {
Backend string
Operation string
Schema string
InputItems int
OutputItems int
ArgCount int
Fingerprint string
Attributes map[string]any
}
OperationPreview is a backend-neutral dry-run summary of generated work. Attributes must stay small and low-cardinality. Raw payloads, SQL args, Redis keys, API bodies, and other sensitive values should not be stored here.
type OperationPreviewer ¶
type OperationPreviewer interface {
GenerateOperationPreview(ctx context.Context, schema SchemaInterface, data []map[string]any) (Operations, OperationPreview, error)
}
OperationPreviewer lets processors generate operations and a diagnostic preview together. Custom processors can implement this interface to participate in logs, metrics, and tracing.
type Operations ¶
type Operations []any
type PipelineConfig ¶
type PipelineConfig struct {
BufferSize uint32
FlushSize uint32
FlushInterval time.Duration
MaxConcurrentFlushes uint32
DrainOnCancel bool
DrainGracePeriod time.Duration
FinalFlushOnCloseTimeout time.Duration
// 可选重试配置(零值=关闭,向后兼容)
Retry RetryConfig
// 可选超时配置(零值=关闭,向后兼容)
Timeout time.Duration
// 可选指标上报器(零值=关闭,向后兼容)
MetricsReporter MetricsReporter
// 可选通用观测配置(结构化日志、采样、脱敏、trace hook)
Observability ObservabilityConfig
// 可选并发限制(零值=无限制,向后兼容)
ConcurrencyLimit int
// 可选批内合并/去重策略。SQL 默认仍使用 SQLOperationConfig 的 conflict-key 合并。
Coalescer Coalescer
}
PipelineConfig 管道配置
func DefaultPipelineConfig ¶
func DefaultPipelineConfig() PipelineConfig
func (PipelineConfig) Validate ¶
func (c PipelineConfig) Validate() error
type PipelineMetricsReporter ¶
type PipelineMetricsReporter interface {
// 出队/取用等待时长(元素在队列中的等待时间)
// 当前由 BatchFlow 在入队成功后自采样并在 flush 开始时上报,
// 不依赖 go-pipeline 的 MetricsHook。
ObserveDequeueLatency(d time.Duration)
// 管道处理总耗时(与执行器/数据库层的 ObserveExecuteDuration 区分)
ObserveProcessDuration(d time.Duration, status string)
// 丢弃/拒绝计数(如队列满、背压拒绝等)
IncDropped(reason string)
}
PipelineMetricsReporter 是对 go-pipeline v2.2.0 WithMetrics 的可选扩展接口。 - 若实现该接口,框架将把管道级指标事件(通过 pipeline.WithMetrics)桥接到以下方法; - 若未实现,则回退到现有 MetricsReporter 的近似指标或直接忽略(保持向后兼容)。
type PostgreSQLDriver ¶
type PostgreSQLDriver struct {
// contains filtered or unexported fields
}
func NewPostgreSQLDriver ¶
func NewPostgreSQLDriver() *PostgreSQLDriver
type RedactorFunc ¶
type RedisBatchProcessor ¶
type RedisBatchProcessor struct {
// contains filtered or unexported fields
}
RedisBatchProcessor Redis批量处理器 实现 BatchProcessor 接口,专注于Redis的核心处理逻辑
func NewRedisBatchProcessor ¶
func NewRedisBatchProcessor(client *redis.Client, driver RedisDriver) *RedisBatchProcessor
NewRedisBatchProcessor 创建Redis批量处理器 参数: - client: Redis客户端连接 - driver: Redis操作生成器
func (*RedisBatchProcessor) ExecuteOperations ¶
func (rp *RedisBatchProcessor) ExecuteOperations(ctx context.Context, operations Operations) error
Redis 执行与快速退出: - 在设置了 rp.timeout 时,使用 context.WithTimeoutCause 限定执行时限。 - 大批量 operations 时,在循环内检查 ctx(可每次或每 N 次)以快速响应取消/超时,避免无谓迭代开销。 - Pipeline 在本函数内构建并执行,不跨越函数生命周期,defer cancel() 安全。
func (*RedisBatchProcessor) GenerateOperationPreview ¶
func (rp *RedisBatchProcessor) GenerateOperationPreview(ctx context.Context, schema SchemaInterface, data []map[string]any) (Operations, OperationPreview, error)
func (*RedisBatchProcessor) GenerateOperations ¶
func (rp *RedisBatchProcessor) GenerateOperations(ctx context.Context, schema SchemaInterface, data []map[string]any) (operations Operations, err error)
GenerateOperations 执行批量操作
func (*RedisBatchProcessor) WithTimeout ¶
func (rp *RedisBatchProcessor) WithTimeout(timeout time.Duration) *RedisBatchProcessor
type RedisDriver ¶
type RedisPipelineDriver ¶
type RedisPipelineDriver struct{}
func NewRedisPipelineDriver ¶
func NewRedisPipelineDriver() *RedisPipelineDriver
func (*RedisPipelineDriver) GenerateCmds ¶
func (d *RedisPipelineDriver) GenerateCmds(ctx context.Context, schema SchemaInterface, data []map[string]any) ([]RedisCmd, error)
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
用来存储请求的数据的各种字段信息和对应的schema
func NewRequest ¶
func NewRequest(schema SchemaInterface) *Request
func (*Request) GetOrderedValues ¶
GetOrderedValues 按照 schema 中定义的列顺序返回值
type RetryConfig ¶
type RetryConfig struct {
Enabled bool
MaxAttempts int // 总尝试次数(含首轮),建议 2~3
BackoffBase time.Duration // 退避基值(指数退避起点)
MaxBackoff time.Duration // 最大退避时长(上限)
// 自定义错误分类(可选);返回是否可重试与原因标签
Classifier func(error) (retryable bool, reason string)
}
RetryConfig 可选重试配置(零值关闭)
type RuntimeConfig ¶
type RuntimeConfig struct {
ShardCount uint32
Routing ShardRoutingPolicy
ShardKeyFunc ShardKeyFunc
Backpressure BackpressureConfig
MemoryLimit MemoryLimitConfig
Adaptive AdaptiveTuningConfig
}
RuntimeConfig contains runtime controls separate from pipeline and executor settings.
func DefaultRuntimeConfig ¶
func DefaultRuntimeConfig() RuntimeConfig
type RuntimeEngine ¶
type RuntimeEngine struct {
// contains filtered or unexported fields
}
RuntimeEngine owns sharding, routing, backpressure and memory protection.
func NewRuntimeEngine ¶
func NewRuntimeEngine(ctx context.Context, cfg Config) (*RuntimeEngine, error)
func (*RuntimeEngine) Close ¶
func (e *RuntimeEngine) Close() error
func (*RuntimeEngine) Done ¶
func (e *RuntimeEngine) Done() <-chan struct{}
func (*RuntimeEngine) ErrorChan ¶
func (e *RuntimeEngine) ErrorChan(size int) <-chan error
func (*RuntimeEngine) Submit ¶
func (e *RuntimeEngine) Submit(ctx context.Context, req *Request) error
func (*RuntimeEngine) Wait ¶
func (e *RuntimeEngine) Wait() error
type SQLBatchProcessor ¶
type SQLBatchProcessor struct {
// contains filtered or unexported fields
}
SQLBatchProcessor SQL数据库批量处理器 实现 BatchProcessor 接口,专注于SQL数据库的核心处理逻辑
func NewSQLBatchProcessor ¶
func NewSQLBatchProcessor(db *sql.DB, driver SQLDriver) *SQLBatchProcessor
NewSQLBatchProcessor 创建SQL批量处理器 参数: - db: 数据库连接(用户管理连接池) - driver: 数据库特定的SQL生成器
func (*SQLBatchProcessor) ExecuteOperations ¶
func (bp *SQLBatchProcessor) ExecuteOperations(ctx context.Context, operations Operations) error
SQL 执行语义:
- 在设置了 bp.timeout 时,使用 context.WithTimeoutCause 派生子 ctx(具体 cause 如 "execute batch timeout")。
- 当子 ctx 达到超时时,驱动通常返回 context.DeadlineExceeded;本处理器会读取 context.Cause(ctx) 并原样返回该 cause, 以便上层执行器的重试分类器可以区分“处理器内部超时”,按需实施重试与退避。
- 安全性:在执行前校验空 operations,避免越界;不持久化/返回子 ctx,defer cancel() 安全。
func (*SQLBatchProcessor) GenerateOperationPreview ¶
func (bp *SQLBatchProcessor) GenerateOperationPreview(ctx context.Context, schema SchemaInterface, data []map[string]any) (Operations, OperationPreview, error)
func (*SQLBatchProcessor) GenerateOperations ¶
func (bp *SQLBatchProcessor) GenerateOperations(ctx context.Context, schema SchemaInterface, data []map[string]any) (operations Operations, err error)
func (*SQLBatchProcessor) GenerateSQLPreview ¶
func (bp *SQLBatchProcessor) GenerateSQLPreview(ctx context.Context, schema *SQLSchema, data []map[string]any) (SQLPreview, error)
func (*SQLBatchProcessor) WithTimeout ¶
func (bp *SQLBatchProcessor) WithTimeout(timeout time.Duration) *SQLBatchProcessor
type SQLDedupStats ¶
SQLDedupStats describes client-side duplicate conflict-key handling before SQL generation.
type SQLDriver ¶
type SQLDriver interface {
GenerateInsertSQL(ctx context.Context, schema *SQLSchema, data []map[string]any) (sql string, args []any, err error)
}
SQLDriver 数据库特定的SQL生成器接口
type SQLError ¶
type SQLError struct {
Stage SQLStage
Table string
BatchSize int
ConflictStrategy ConflictStrategy
ConflictColumns []string
UpdateColumns []string
SQLFingerprint string
ArgsCount int
Cause error
}
SQLError wraps SQL batch failures with safe diagnostic metadata.
type SQLMetricsReporter ¶
type SQLMetricsReporter interface {
ObserveSQLGenerated(table string, inputRows, outputRows, argsCount int)
ObserveSQLDeduplicated(table string, strategy ConflictStrategy, deduplicatedRows, mergedRows int)
IncSQLError(table string, stage SQLStage, reason string)
}
SQLMetricsReporter is an optional SQL-only detail extension kept for SQL diagnostics and backward-compatible Prometheus examples. Backend-neutral integrations should prefer OperationMetricsReporter. Implementations must keep labels low-cardinality; do not use raw SQL as a label.
type SQLOperationConfig ¶
type SQLOperationConfig struct {
ConflictStrategy ConflictStrategy
// ConflictColumns defines the conflict target used by upsert-style writes.
// When empty, drivers fall back to the first schema column for backward compatibility.
ConflictColumns []string
// UpdateColumns limits columns updated by ConflictUpdate. When empty,
// drivers update all non-conflict columns.
UpdateColumns []string
// DeduplicateByConflictColumns controls client-side merge of duplicate
// conflict keys before generating SQL. The default is true; use
// WithDeduplicateByConflictColumns(false) to disable it.
DeduplicateByConflictColumns bool
// contains filtered or unexported fields
}
操作配置
func (SQLOperationConfig) WithConflictColumns ¶
func (c SQLOperationConfig) WithConflictColumns(cols ...string) SQLOperationConfig
func (SQLOperationConfig) WithDeduplicateByConflictColumns ¶
func (c SQLOperationConfig) WithDeduplicateByConflictColumns(enabled bool) SQLOperationConfig
func (SQLOperationConfig) WithUpdateColumns ¶
func (c SQLOperationConfig) WithUpdateColumns(cols ...string) SQLOperationConfig
type SQLPreview ¶
type SQLPreview struct {
Table string
SQL string
Args []any
ArgsCount int
Columns []string
ConflictStrategy ConflictStrategy
ConflictColumns []string
UpdateColumns []string
DedupStats SQLDedupStats
Fingerprint string
}
SQLPreview is a dry-run view of the final SQL operation that would be executed.
Args contains raw values and may include sensitive data. Prefer ArgsCount and Fingerprint for logs and metrics unless the caller explicitly needs full inspection.
func GenerateSQLPreview ¶
func GenerateSQLPreview(ctx context.Context, driver SQLDriver, schema *SQLSchema, data []map[string]any) (SQLPreview, error)
GenerateSQLPreview builds the same final SQL and args as SQLBatchProcessor without executing it.
func (SQLPreview) OperationPreview ¶
func (p SQLPreview) OperationPreview() OperationPreview
type SQLSchema ¶
type SQLSchema struct {
*Schema
// contains filtered or unexported fields
}
func NewSQLSchema ¶
func NewSQLSchema(name string, operationConfig SQLOperationConfig, columns ...string) *SQLSchema
func (*SQLSchema) OperationConfig ¶
type SQLiteDriver ¶
type SQLiteDriver struct {
// contains filtered or unexported fields
}
func NewSQLiteDriver ¶
func NewSQLiteDriver() *SQLiteDriver
type Sampler ¶
type Sampler interface {
ShouldSample(event BatchEvent) bool
}
func NewErrorAndSlowSampler ¶
func NewRatioSampler ¶
type SamplerFunc ¶
type SamplerFunc func(BatchEvent) bool
func (SamplerFunc) ShouldSample ¶
func (f SamplerFunc) ShouldSample(event BatchEvent) bool
type SchemaInterface ¶
type ShardKeyFunc ¶
ShardKeyFunc returns routing key for a request.
type ShardRoutingPolicy ¶
type ShardRoutingPolicy uint8
ShardRoutingPolicy controls how a sharded BatchFlow chooses a target shard.
const ( ShardRoutingHash ShardRoutingPolicy = iota ShardRoutingRoundRobin ShardRoutingLeastLoaded )
type ThrottledBatchExecutor ¶
type ThrottledBatchExecutor struct {
// contains filtered or unexported fields
}
ThrottledBatchExecutor 通用批量执行器 架构:ThrottledBatchExecutor -> BatchProcessor -> backend driver/client
设计优势: - 代码复用:SQL、Redis、自定义后端共享执行、限流、重试和观测逻辑 - 职责分离:执行控制与具体处理逻辑分离 - 易于扩展:新增后端只需实现 BatchProcessor,可选实现 OperationPreviewer
func NewRedisThrottledBatchExecutor ¶
func NewRedisThrottledBatchExecutor(client *redis.Client) *ThrottledBatchExecutor
func NewRedisThrottledBatchExecutorWithDriver ¶
func NewRedisThrottledBatchExecutorWithDriver(client *redis.Client, driver RedisDriver) *ThrottledBatchExecutor
func NewSQLThrottledBatchExecutorWithDriver ¶
func NewSQLThrottledBatchExecutorWithDriver(db *sql.DB, driver SQLDriver) *ThrottledBatchExecutor
NewThrottledBatchExecutorWithDriver 创建SQL数据库执行器(推荐方式) 内部使用 SQLBatchProcessor + SQLDriver 组合
func NewThrottledBatchExecutor ¶
func NewThrottledBatchExecutor(processor BatchProcessor) *ThrottledBatchExecutor
NewThrottledBatchExecutor 创建通用执行器(使用自定义BatchProcessor)
func (*ThrottledBatchExecutor) ExecuteBatch ¶
func (e *ThrottledBatchExecutor) ExecuteBatch(ctx context.Context, schema SchemaInterface, data []map[string]any) error
ExecuteBatch 执行批量操作
func (*ThrottledBatchExecutor) MetricsReporter ¶
func (e *ThrottledBatchExecutor) MetricsReporter() MetricsReporter
MetricsReporter 获取指标报告器
func (*ThrottledBatchExecutor) Observer ¶
func (e *ThrottledBatchExecutor) Observer() Observer
func (*ThrottledBatchExecutor) WithCoalescer ¶
func (e *ThrottledBatchExecutor) WithCoalescer(coalescer Coalescer) *ThrottledBatchExecutor
func (*ThrottledBatchExecutor) WithConcurrencyLimit ¶
func (e *ThrottledBatchExecutor) WithConcurrencyLimit(limit int) *ThrottledBatchExecutor
WithConcurrencyLimit 设置并发上限(limit <= 0 表示不启用限流)
func (*ThrottledBatchExecutor) WithMetricsReporter ¶
func (e *ThrottledBatchExecutor) WithMetricsReporter(metricsReporter MetricsReporter) *ThrottledBatchExecutor
WithMetricsReporter 设置指标报告器
func (*ThrottledBatchExecutor) WithObservability ¶
func (e *ThrottledBatchExecutor) WithObservability(config ObservabilityConfig) *ThrottledBatchExecutor
func (*ThrottledBatchExecutor) WithObserver ¶
func (e *ThrottledBatchExecutor) WithObserver(observer Observer) *ThrottledBatchExecutor
func (*ThrottledBatchExecutor) WithRetryConfig ¶
func (e *ThrottledBatchExecutor) WithRetryConfig(cfg RetryConfig) *ThrottledBatchExecutor
WithRetryConfig 启用/配置重试(仅对 ThrottledBatchExecutor 可用)
type TimeOutCapable ¶
TimeOutCapable 扩展接口:支持超时设置(自类型泛型)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
test
|
|
|
integration
command
|
|
|
sqlite/tools/benchmark
command
|
|
|
sqlite/tools/clear-test
command
|
|
|
sqlite/tools/config-analysis
command
|
|
|
sqlite/tools/path-compatibility
command
|