log stream improvement
This commit is contained in:
@@ -14,8 +14,6 @@ import (
|
||||
const (
|
||||
// MetaEventsStream is the Redis stream name for metadata events
|
||||
MetaEventsStream = "meta:events"
|
||||
// StreamMaxLen is the maximum stream length (approximate)
|
||||
StreamMaxLen = 10000
|
||||
)
|
||||
|
||||
// MetaPublisher subscribes to Redis keyspace notifications
|
||||
@@ -49,6 +47,16 @@ func (p *MetaPublisher) Start() error {
|
||||
p.running = true
|
||||
p.mu.Unlock()
|
||||
|
||||
// Clear the stream on startup - consumers will re-bootstrap from Redis keys
|
||||
// This ensures the stream only contains events from the current session
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := p.client.Del(ctx, MetaEventsStream).Err(); err != nil {
|
||||
log.Printf("[MetaPublisher] Warning: failed to clear stream on startup: %v", err)
|
||||
} else {
|
||||
log.Printf("[MetaPublisher] Cleared %s stream for fresh start", MetaEventsStream)
|
||||
}
|
||||
|
||||
p.wg.Add(1)
|
||||
go p.subscribeLoop()
|
||||
|
||||
@@ -125,11 +133,9 @@ func (p *MetaPublisher) publishEvent(channel, operation string) {
|
||||
"ts": fmt.Sprintf("%d", time.Now().UnixMilli()),
|
||||
}
|
||||
|
||||
// Add to stream
|
||||
// Add to stream (no MaxLen - stream is cleared on meta-core restart)
|
||||
_, err := p.client.XAdd(p.ctx, &redis.XAddArgs{
|
||||
Stream: MetaEventsStream,
|
||||
MaxLen: StreamMaxLen,
|
||||
Approx: true,
|
||||
Values: fields,
|
||||
}).Result()
|
||||
|
||||
|
||||
@@ -633,7 +633,8 @@ func (c *Client) GetPrefix() string {
|
||||
}
|
||||
|
||||
// XAdd publishes an event to a Redis Stream
|
||||
// Uses XADD with MAXLEN ~ for approximate trimming
|
||||
// If maxLen > 0, uses XADD with MAXLEN ~ for approximate trimming
|
||||
// If maxLen <= 0, no trimming is applied (stream grows unbounded)
|
||||
func (c *Client) XAdd(stream string, maxLen int64, fields map[string]interface{}) (string, error) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
@@ -645,14 +646,18 @@ func (c *Client) XAdd(stream string, maxLen int64, fields map[string]interface{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Build XADD arguments with approximate MAXLEN
|
||||
// Build XADD arguments
|
||||
args := &redis.XAddArgs{
|
||||
Stream: stream,
|
||||
MaxLen: maxLen,
|
||||
Approx: true,
|
||||
Values: fields,
|
||||
}
|
||||
|
||||
// Only set MaxLen if positive (0 means no limit)
|
||||
if maxLen > 0 {
|
||||
args.MaxLen = maxLen
|
||||
args.Approx = true
|
||||
}
|
||||
|
||||
id, err := c.client.XAdd(ctx, args).Result()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("xadd failed: %w", err)
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
const (
|
||||
// EventsStream is the Redis Stream name for file events
|
||||
EventsStream = "file:events"
|
||||
// StreamMaxLen is the approximate max length for the stream
|
||||
StreamMaxLen = 10000
|
||||
)
|
||||
|
||||
// Dispatcher sends file events to Redis Stream
|
||||
@@ -70,8 +68,8 @@ func (d *Dispatcher) dispatchToStream(event FileEvent) {
|
||||
fields["watcherId"] = event.WatcherID
|
||||
}
|
||||
|
||||
// Publish to stream
|
||||
_, err := client.XAdd(EventsStream, StreamMaxLen, fields)
|
||||
// Publish to stream (no MaxLen - stream is cleared on reset)
|
||||
_, err := client.XAdd(EventsStream, 0, fields)
|
||||
if err != nil {
|
||||
log.Printf("[Dispatcher] Failed to publish to stream: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user