Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit e5a5f31

Browse files
committed
Reset rate limiting every 24 hours
1 parent ed8fbf2 commit e5a5f31

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

status.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ import (
3131

3232
// Status contains info about the sketches running on the device
3333
type Status struct {
34-
id string
35-
mqttClient mqtt.Client
36-
Sketches map[string]*SketchStatus `json:"sketches"`
37-
messagesSent int
34+
id string
35+
mqttClient mqtt.Client
36+
Sketches map[string]*SketchStatus `json:"sketches"`
37+
messagesSent int
38+
firstMessageAt time.Time
3839
}
3940

4041
// SketchBinding represents a pair (SketchName,SketchId)
@@ -121,7 +122,18 @@ func (s *Status) Raw(topic, msg string) {
121122
if s.mqttClient == nil {
122123
return
123124
}
125+
126+
if s.messagesSent < 10 {
127+
// first 10 messages are virtually free
128+
s.firstMessageAt = time.Now()
129+
}
130+
124131
if s.messagesSent > 1000 {
132+
// if started more than one day ago, reset the counter
133+
if time.Since(s.firstMessageAt) > 24*time.Hour {
134+
s.messagesSent = 0
135+
}
136+
125137
fmt.Println("rate limiting: " + strconv.Itoa(s.messagesSent))
126138
introducedDelay := time.Duration(s.messagesSent/1000) * time.Second
127139
if introducedDelay > 20*time.Second {

0 commit comments

Comments
 (0)