Add event-driven daemon architecture (Phase 1 foundation)
- Add fsnotify dependency for file watching - Create daemon_debouncer.go: batch rapid events (500ms window) - Create daemon_watcher.go: monitor JSONL and git refs changes - Create daemon_event_loop.go: event-driven sync loop - Add mutation channel to RPC server (create/update/close events) - Add BEADS_DAEMON_MODE env var (poll/events, default: poll) Phase 1 implementation: opt-in via BEADS_DAEMON_MODE=events Target: <500ms latency (vs 5000ms), ~60% CPU reduction Related: bd-49 (epic), bd-50, bd-51, bd-53, bd-54, bd-55, bd-56 Amp-Thread-ID: https://ampcode.com/threads/T-35a3d0d7-4e19-421d-8392-63755035036e Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
55
cmd/bd/daemon_debouncer.go
Normal file
55
cmd/bd/daemon_debouncer.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Debouncer batches rapid events into a single action after a quiet period.
|
||||
// Thread-safe for concurrent triggers.
|
||||
type Debouncer struct {
|
||||
mu sync.Mutex
|
||||
timer *time.Timer
|
||||
duration time.Duration
|
||||
action func()
|
||||
}
|
||||
|
||||
// NewDebouncer creates a new debouncer with the given duration and action.
|
||||
// The action will be called once after the duration has passed since the last trigger.
|
||||
func NewDebouncer(duration time.Duration, action func()) *Debouncer {
|
||||
return &Debouncer{
|
||||
duration: duration,
|
||||
action: action,
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger schedules the action to run after the debounce duration.
|
||||
// If called multiple times, the timer is reset each time, ensuring
|
||||
// the action only fires once after the last trigger.
|
||||
func (d *Debouncer) Trigger() {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
if d.timer != nil {
|
||||
d.timer.Stop()
|
||||
}
|
||||
|
||||
d.timer = time.AfterFunc(d.duration, func() {
|
||||
d.action()
|
||||
d.mu.Lock()
|
||||
d.timer = nil
|
||||
d.mu.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
// Cancel stops any pending debounced action.
|
||||
// Safe to call even if no action is pending.
|
||||
func (d *Debouncer) Cancel() {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
if d.timer != nil {
|
||||
d.timer.Stop()
|
||||
d.timer = nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user