feat(chat): implement sequence pre-allocation, versioned sync, and push worker
Introduce several performance and synchronization enhancements: - Implement `SeqBufferManager` to allow sequence number pre-allocation via Redis Lua scripts, reducing atomic increment overhead. - Add `PushWorker` to handle asynchronous message pushing using Redis Streams. - Implement incremental conversation synchronization via `ConversationVersionLog` to allow clients to fetch only recent changes. - Add support for Gzip compression in WebSocket communications to reduce bandwidth usage. - Update dependency injection and configuration to support these new components.
This commit is contained in:
@@ -34,6 +34,9 @@ type Config struct {
|
||||
SetupSecret string `mapstructure:"setup_secret"`
|
||||
WebSocket WSConfig `mapstructure:"websocket"`
|
||||
Runner RunnerConfig `mapstructure:"runner"`
|
||||
SeqBuffer SeqBufferConfig `mapstructure:"seq_buffer"`
|
||||
VersionLog VersionLogConfig `mapstructure:"version_log"`
|
||||
PushWorker PushWorkerConfig `mapstructure:"push_worker"`
|
||||
}
|
||||
|
||||
// Load 加载配置文件
|
||||
@@ -191,6 +194,25 @@ func Load(configPath string) (*Config, error) {
|
||||
viper.SetDefault("runner.cluster.cap_prefix", "runner:cap:")
|
||||
viper.SetDefault("runner.cluster.registry_ttl", 90)
|
||||
viper.SetDefault("runner.cluster.heartbeat_interval", 30)
|
||||
// SeqBuffer 默认值
|
||||
viper.SetDefault("seq_buffer.enabled", false)
|
||||
viper.SetDefault("seq_buffer.private_buffer_size", 50)
|
||||
viper.SetDefault("seq_buffer.group_buffer_size", 200)
|
||||
viper.SetDefault("seq_buffer.lock_timeout_ms", 5000)
|
||||
viper.SetDefault("seq_buffer.max_retries", 3)
|
||||
// VersionLog 默认值
|
||||
viper.SetDefault("version_log.enabled", false)
|
||||
viper.SetDefault("version_log.sync_limit", 100)
|
||||
viper.SetDefault("version_log.max_sync_gap", 1000)
|
||||
// PushWorker 默认值
|
||||
viper.SetDefault("push_worker.enabled", false)
|
||||
viper.SetDefault("push_worker.stream", "msg_push")
|
||||
viper.SetDefault("push_worker.group", "push_worker")
|
||||
viper.SetDefault("push_worker.batch_size", 50)
|
||||
viper.SetDefault("push_worker.poll_timeout_ms", 2000)
|
||||
viper.SetDefault("push_worker.max_retries", 3)
|
||||
viper.SetDefault("push_worker.max_stream_len", 100000)
|
||||
viper.SetDefault("push_worker.idle_timeout_ms", 30000)
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read config: %w", err)
|
||||
|
||||
13
internal/config/push_worker.go
Normal file
13
internal/config/push_worker.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
// PushWorkerConfig 推送 Worker 配置(Redis Stream 异步推送)
|
||||
type PushWorkerConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Stream string `mapstructure:"stream"` // Redis Stream 名称,默认 "msg_push"
|
||||
Group string `mapstructure:"group"` // Consumer Group 名称,默认 "push_worker"
|
||||
BatchSize int `mapstructure:"batch_size"` // XREADGROUP 每次读取条数,默认 50
|
||||
PollTimeoutMs int `mapstructure:"poll_timeout_ms"` // XREADGROUP BLOCK 超时 ms,默认 2000
|
||||
MaxRetries int `mapstructure:"max_retries"` // 消息最大重试次数,默认 3
|
||||
MaxStreamLen int64 `mapstructure:"max_stream_len"` // Stream MAXLEN,默认 100000
|
||||
IdleTimeoutMs int64 `mapstructure:"idle_timeout_ms"` // XPENDING 空闲超时 ms,默认 30000
|
||||
}
|
||||
10
internal/config/seq_buffer.go
Normal file
10
internal/config/seq_buffer.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
// SeqBufferConfig seq 预分配配置
|
||||
type SeqBufferConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
PrivateBufferSize int `mapstructure:"private_buffer_size"` // 私聊预分配步长(默认 50)
|
||||
GroupBufferSize int `mapstructure:"group_buffer_size"` // 群聊预分配步长(默认 200)
|
||||
LockTimeoutMs int `mapstructure:"lock_timeout_ms"` // 分布式锁超时毫秒(默认 5000)
|
||||
MaxRetries int `mapstructure:"max_retries"` // 冷启动重试次数(默认 3)
|
||||
}
|
||||
8
internal/config/version_log.go
Normal file
8
internal/config/version_log.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
// VersionLogConfig 版本日志增量同步配置
|
||||
type VersionLogConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
SyncLimit int `mapstructure:"sync_limit"` // 单次同步最大返回条数
|
||||
MaxSyncGap int64 `mapstructure:"max_sync_gap"` // 超过此版本差距建议全量同步
|
||||
}
|
||||
Reference in New Issue
Block a user