Skip to content

Commit 2fed214

Browse files
committed
Add whitelist of events not to rate limit
1 parent 6d878d4 commit 2fed214

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

eventstore/eventstore.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,35 @@ type EventDescription struct {
116116
Details map[string]interface{} `json:"details"`
117117
}
118118

119+
var whitelist = map[string]struct{}{
120+
"UnrecoverableDataCorruption": {},
121+
"CorruptFile": {},
122+
"ErasePartialOrCorruptRecording": {},
123+
"OffloadedRecording": {},
124+
"SavedNewConfig": {},
125+
"FileOffloadInterruptedByUser": {},
126+
"Rp2040GotNewConfig": {},
127+
}
128+
119129
// shouldBeRateLimited checks if that type of event is being made too often.
120130
// Each time an event is made, if an event of that same type was made in the last 3 minutes
121131
// a counter will be incremented. Otherwise the counter will be reset to 0.
122132
// If the counter is 5 or more the events will be rate limited.
123133
// When the counter reaches 5, an rate_limit event will be made
134+
// There is a whitelist of events that are not rate limited, these events are made when offloading
135+
// recordings so it is expected that there will be a lot of these events in a short amount of time.
124136
func (s *EventStore) shouldBeRateLimited(event *Event) bool {
125137
s.mux.Lock()
126138
defer s.mux.Unlock()
127139

128140
eventType := event.Description.Type
129141
eventTime := event.Timestamp
130142

143+
// Checking if the event type is in the whitelist
144+
if _, ok := whitelist[eventType]; ok {
145+
return false
146+
}
147+
131148
rl, ok := s.rateLimits[eventType]
132149

133150
// Checking if the event type is in the rate limit map

eventstore/eventstore_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,64 @@ func (s *Suite) TestRateLimitThenNoRateLimit() {
275275
}
276276
}
277277

278+
func (s *Suite) TestRateLimitWhiteList() {
279+
getNodegroupFunc = func() (string, error) {
280+
return "the_nodegroup", nil
281+
}
282+
283+
// Intervals between events.
284+
durations := []time.Duration{
285+
time.Minute,
286+
time.Minute,
287+
time.Minute,
288+
time.Minute,
289+
time.Minute,
290+
time.Minute,
291+
time.Minute,
292+
time.Minute,
293+
time.Minute,
294+
}
295+
296+
// Make event times
297+
eventTime := time.Now()
298+
times := []time.Time{eventTime}
299+
for _, d := range durations {
300+
eventTime = eventTime.Add(d)
301+
times = append(times, eventTime)
302+
}
303+
304+
// Make events
305+
description := EventDescription{Details: map[string]interface{}{"file": "abc"}, Type: "CorruptFile"}
306+
for _, t := range times {
307+
event := Event{
308+
Timestamp: t,
309+
Description: description,
310+
}
311+
s.NoError(s.store.Add(&event))
312+
}
313+
314+
// Test GetKeys
315+
keys, err := s.store.GetKeys()
316+
s.NoError(err, "error returned when getting all keys")
317+
// Check 10 events + 1 rate limit event
318+
s.Equal(len(durations)+1, len(keys), "error with number of keys returned")
319+
320+
// Check that there was a rate limit event
321+
rateLimitEvent := false
322+
for _, key := range keys {
323+
eventBytes, err := s.store.Get(key)
324+
s.NoError(err)
325+
event := &Event{}
326+
s.NoError(json.Unmarshal(eventBytes, event))
327+
if event.Description.Type == "rateLimit" {
328+
rateLimitEvent = true
329+
}
330+
}
331+
if rateLimitEvent {
332+
s.Fail("Rate limit event found")
333+
}
334+
}
335+
278336
func TestRun(t *testing.T) {
279337
suite.Run(t, new(Suite))
280338
}

0 commit comments

Comments
 (0)