Skip to content

Commit

Permalink
Add example for exponential backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
ViToni committed Apr 20, 2024
1 parent 543e5de commit 5230ed3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions autopaho/examples/backoff/backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package main

import (
"fmt"
"math"

"github.com/eclipse/paho.golang/autopaho"
)

func main() {
// used to calculate the number of iterations
const interationBase = 1

backoffStrategy := autopaho.DefaultExponentialBackoffStrategy()

backoff := backoffStrategy.Backoff()

minBackoff := int64(math.MaxInt64)
maxBackoff := int64(0)
iterationsTotal := 0
for i := 0; i < 22; i++ {
iterations := interationBase << i
for j := 0; j < iterations; j++ {
backoffTime := backoff.Next().Milliseconds()
if backoffTime < minBackoff {
minBackoff = backoffTime
}
if backoffTime > maxBackoff {
maxBackoff = backoffTime
}
}
iterationsTotal += iterations

fmt.Printf("After % 8d iterations, min: %d, max: % 7d\n", iterationsTotal, minBackoff, maxBackoff)
}
}

0 comments on commit 5230ed3

Please sign in to comment.