-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ring buffer on the handlers instead
- Loading branch information
1 parent
e57ded1
commit 29166d9
Showing
5 changed files
with
101 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package events | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/flanksource/postq" | ||
) | ||
|
||
type EventRing struct { | ||
size int | ||
events map[string][]map[string]any | ||
mu *sync.RWMutex | ||
} | ||
|
||
func NewEventRing(size int) *EventRing { | ||
return &EventRing{ | ||
size: size, | ||
events: make(map[string][]map[string]any), | ||
mu: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (t *EventRing) Add(event postq.Event, env map[string]any) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
|
||
if _, ok := t.events[event.Name]; !ok { | ||
t.events[event.Name] = make([]map[string]any, 0) | ||
} | ||
|
||
t.events[event.Name] = append(t.events[event.Name], map[string]any{ | ||
"event": event, | ||
"env": env, | ||
}) | ||
|
||
if len(t.events[event.Name]) > t.size { | ||
t.events[event.Name] = t.events[event.Name][1:] | ||
} | ||
} | ||
|
||
func (t *EventRing) Get() map[string][]map[string]any { | ||
return t.events | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters