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

Commit f20891b

Browse files
authored
Merge pull request #43 from arduino/reset_rate_limiting
Reset rate limiting every 24 hours
2 parents ed8fbf2 + f2cb82b commit f20891b

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const (
4444
)
4545

4646
var (
47-
version = "x.x.x"
47+
version = "0.0.0-dev"
4848
debugMqtt = false
4949
)
5050

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 {

updater/updater.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,19 @@ func (u *Updater) update() error {
232232
return err
233233
}
234234

235+
// this is the version publically available (GA)
235236
v1, _ := semver.Make(u.Info.Version)
237+
// this is the version of the actual binary at start
236238
v2, _ := semver.Make(u.CurrentVersion)
237239

240+
if len(v2.Pre) > 0 {
241+
log.Println("Prerelease versions:")
242+
for _, pre := range v2.Pre {
243+
if pre.String() == "dev" {
244+
return errors.New("dev version")
245+
}
246+
}
247+
}
238248
if v1.Compare(v2) <= 0 {
239249
return errors.New("already at latest version")
240250
}

0 commit comments

Comments
 (0)