Skip to content

Commit eb350bd

Browse files
authored
Randomize wait in case of rate limiting (#4)
1 parent b9e7fc8 commit eb350bd

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

business/tsextractor/tsextractor.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
"sync"
2424
"time"
2525

26+
"crypto/rand"
27+
"math/big"
28+
2629
"github.com/arduino/aws-s3-integration/internal/csv"
2730
"github.com/arduino/aws-s3-integration/internal/iot"
2831
"github.com/arduino/aws-s3-integration/internal/s3"
@@ -121,6 +124,17 @@ func (a *TsExtractor) ExportTSToS3(
121124
return nil
122125
}
123126

127+
func randomRateLimitingSleep() {
128+
// Random sleep to avoid rate limiting (1s + random(0-500ms))
129+
n, err := rand.Int(rand.Reader, big.NewInt(500))
130+
if err != nil {
131+
fmt.Println("Error:", err)
132+
return
133+
}
134+
randomSleep := n.Int64() + 1000
135+
time.Sleep(time.Duration(randomSleep) * time.Millisecond)
136+
}
137+
124138
func (a *TsExtractor) populateNumericTSDataIntoS3(
125139
ctx context.Context,
126140
from time.Time,
@@ -144,7 +158,7 @@ func (a *TsExtractor) populateNumericTSDataIntoS3(
144158
} else {
145159
// This is due to a rate limit on the IoT API, we need to wait a bit before retrying
146160
a.logger.Infof("Rate limit reached for thing %s. Waiting 1 second before retrying.\n", thingID)
147-
time.Sleep(1 * time.Second)
161+
randomRateLimitingSleep()
148162
}
149163
}
150164
if err != nil {
@@ -205,6 +219,9 @@ func extractPropertyNameAndType(thing iotclient.ArduinoThing, propertyID string)
205219
break
206220
}
207221
}
222+
if propertyType == "STATUS" {
223+
propertyType = "BOOLEAN"
224+
}
208225
return propertyName, propertyType
209226
}
210227

@@ -243,7 +260,7 @@ func (a *TsExtractor) populateStringTSDataIntoS3(
243260
} else {
244261
// This is due to a rate limit on the IoT API, we need to wait a bit before retrying
245262
a.logger.Infof("Rate limit reached for thing %s. Waiting 1 second before retrying.\n", thingID)
246-
time.Sleep(1 * time.Second)
263+
randomRateLimitingSleep()
247264
}
248265
}
249266
if err != nil {
@@ -303,7 +320,7 @@ func (a *TsExtractor) populateRawTSDataIntoS3(
303320
} else {
304321
// This is due to a rate limit on the IoT API, we need to wait a bit before retrying
305322
a.logger.Infof("Rate limit reached for thing %s. Waiting 1 second before retrying.\n", thingID)
306-
time.Sleep(1 * time.Second)
323+
randomRateLimitingSleep()
307324
}
308325
}
309326
if err != nil {
Binary file not shown.

0 commit comments

Comments
 (0)