Add memory flow store (#3386)

This commit is contained in:
Maycon Santos
2025-02-25 15:23:43 +00:00
committed by GitHub
parent 1e534cecf6
commit 175674749f
3 changed files with 164 additions and 18 deletions
+79
View File
@@ -0,0 +1,79 @@
package flowstore
import (
"context"
"io"
"sync"
log "github.com/sirupsen/logrus"
)
type Event struct {
ID string
FlowID string
}
type Store interface {
io.Closer
// stores a flow event
StoreEvent(flowEvent Event)
// returns all stored events
GetEvents() []*Event
}
func New(ctx context.Context) Store {
ctx, cancel := context.WithCancel(ctx)
store := &memory{
events: make(map[string]*Event),
rcvChan: make(chan *Event, 100),
ctx: ctx,
cancel: cancel,
}
go store.startReceiver()
return store
}
type memory struct {
mux sync.Mutex
events map[string]*Event
rcvChan chan *Event
ctx context.Context
cancel context.CancelFunc
}
func (m *memory) startReceiver() {
for {
select {
case <-m.ctx.Done():
log.Info("flow memory store receiver stopped")
return
case event := <-m.rcvChan:
m.mux.Lock()
m.events[event.ID] = event
m.mux.Unlock()
}
}
}
func (m *memory) StoreEvent(flowEvent Event) {
select {
case m.rcvChan <- &flowEvent:
default:
log.Warn("flow memory store receiver is busy")
}
}
func (m *memory) Close() error {
m.cancel()
return nil
}
func (m *memory) GetEvents() []*Event {
m.mux.Lock()
defer m.mux.Unlock()
events := make([]*Event, 0, len(m.events))
for _, event := range m.events {
events = append(events, event)
}
return events
}
+28
View File
@@ -0,0 +1,28 @@
package flowstore_test
import (
"context"
"testing"
"github.com/netbirdio/netbird/client/internal/flowstore"
)
func TestStore(t *testing.T) {
store := flowstore.New(context.Background())
t.Cleanup(func() {
store.Close()
})
event := flowstore.Event{
ID: "1",
FlowID: "1",
}
store.StoreEvent(event)
allEvents := store.GetEvents()
for _, e := range allEvents {
if e.ID != event.ID {
t.Errorf("expected event ID %s, got %s", event.ID, e.ID)
}
}
}