Skip to content

Commit

Permalink
fix: limit number to rand.Intn (influxdata#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed Nov 15, 2022
1 parent 6f2f8f9 commit cae80f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [unreleased]
### Bug fixes
- [#363](https://github.com/influxdata/influxdb-client-go/pull/363) Generated server stubs return also error message from InfluxDB 1.x forward compatible API.
- [#364](https://github.com/influxdata/influxdb-client-go/pull/364) Fixed panic when retrying over a long period without a server connection.

## 2.12.0 [2022-10-27]
### Features
Expand Down
6 changes: 5 additions & 1 deletion internal/write/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ func isIgnorableError(error *http2.Error) bool {
func (w *Service) computeRetryDelay(attempts uint) uint {
minDelay := int(w.writeOptions.RetryInterval() * pow(w.writeOptions.ExponentialBase(), attempts))
maxDelay := int(w.writeOptions.RetryInterval() * pow(w.writeOptions.ExponentialBase(), attempts+1))
retryDelay := uint(rand.Intn(maxDelay-minDelay) + minDelay)
diff := maxDelay - minDelay
if diff <= 0 { //check overflows
return w.writeOptions.MaxRetryInterval()
}
retryDelay := uint(rand.Intn(diff) + minDelay)
if retryDelay > w.writeOptions.MaxRetryInterval() {
retryDelay = w.writeOptions.MaxRetryInterval()
}
Expand Down
5 changes: 4 additions & 1 deletion internal/write/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ func TestComputeRetryDelay(t *testing.T) {
assertBetween(t, srv.computeRetryDelay(2), 20_000, 40_000)
assertBetween(t, srv.computeRetryDelay(3), 40_000, 80_000)
assertBetween(t, srv.computeRetryDelay(4), 80_000, 125_000)
assert.EqualValues(t, 125_000, srv.computeRetryDelay(5))

for i := uint(5); i < 200; i++ { //test also limiting higher values
assert.EqualValues(t, 125_000, srv.computeRetryDelay(i))
}
}

func TestErrorCallback(t *testing.T) {
Expand Down

0 comments on commit cae80f4

Please sign in to comment.