Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send a slice of values to callback function instead of references #3745

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions provider/mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func NewAlerts(ctx context.Context, m types.Marker, intervalGC time.Duration, al
logger: log.With(l, "component", "provider"),
callback: alertCallback,
}
a.alerts.SetGCCallback(func(alerts []*types.Alert) {
a.alerts.SetGCCallback(func(alerts []types.Alert) {
for _, alert := range alerts {
// As we don't persist alerts, we no longer consider them after
// they are resolved. Alerts waiting for resolved notifications are
// held in memory in aggregation groups redundantly.
m.Delete(alert.Fingerprint())
a.callback.PostDelete(alert)
a.callback.PostDelete(&alert)
}

a.mtx.Lock()
Expand Down
20 changes: 15 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ var ErrNotFound = errors.New("alert not found")
type Alerts struct {
sync.Mutex
c map[model.Fingerprint]*types.Alert
cb func([]*types.Alert)
cb func([]types.Alert)
}

// NewAlerts returns a new Alerts struct.
func NewAlerts() *Alerts {
a := &Alerts{
c: make(map[model.Fingerprint]*types.Alert),
cb: func(_ []*types.Alert) {},
cb: func(_ []types.Alert) {},
}

return a
}

// SetGCCallback sets a GC callback to be executed after each GC.
func (a *Alerts) SetGCCallback(cb func([]*types.Alert)) {
func (a *Alerts) SetGCCallback(cb func([]types.Alert)) {
a.Lock()
defer a.Unlock()

Expand All @@ -71,11 +71,21 @@ func (a *Alerts) Run(ctx context.Context, interval time.Duration) {

func (a *Alerts) gc() {
a.Lock()
var resolved []*types.Alert
var resolved []types.Alert
for fp, alert := range a.c {
if alert.Resolved() {
delete(a.c, fp)
resolved = append(resolved, alert)
resolved = append(resolved, types.Alert{
Alert: model.Alert{
Labels: alert.Labels.Clone(),
Annotations: alert.Annotations.Clone(),
StartsAt: alert.StartsAt,
EndsAt: alert.EndsAt,
GeneratorURL: alert.GeneratorURL,
},
UpdatedAt: alert.UpdatedAt,
Timeout: alert.Timeout,
})
}
}
a.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestGC(t *testing.T) {
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
s.SetGCCallback(func(a []*types.Alert) {
s.SetGCCallback(func(a []types.Alert) {
n += len(a)
if n >= len(resolved) {
cancel()
Expand Down
Loading