ollie/cmd/olliesrv/internal/agent/feed.go

47 lines
1.1 KiB
Go

// feed.go — Feed value storage with dedup.
//
// Feed holds the current feed value for BlockOnce-style reads. Writes
// store data and compute a short hash. Readers compare hashes to dedup;
// only changed values are returned.
package agent
import (
"crypto/sha256"
"encoding/hex"
"sync"
)
// Feed holds the current feed value. Writes store data and signal.
// Dedup is handled on the read side via base comparison (BlockOnce pattern).
type Feed struct {
mu sync.Mutex
data []byte
hash string // short hash of current data
}
// Set stores new data and returns true (always accepts).
func (f *Feed) Set(data []byte) {
f.mu.Lock()
f.data = append(f.data[:0], data...)
h := sha256.Sum256(data)
f.hash = hex.EncodeToString(h[:8])
f.mu.Unlock()
}
// Get returns the current feed data.
func (f *Feed) Get() []byte {
f.mu.Lock()
defer f.mu.Unlock()
out := make([]byte, len(f.data))
copy(out, f.data)
return out
}
// Hash returns a short hash of the current data (used as base for dedup).
func (f *Feed) Hash() string {
f.mu.Lock()
defer f.mu.Unlock()
return f.hash
}