Debian Codegraph @

autopaho/examples/backoff/backoff.go

raw symbols SCIP
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package main
17
18 import (
19 "fmt"
20 "math"
21
22 "github.com/eclipse/paho.golang/autopaho"
23 )
24
25 func main() {
26 // used to calculate the number of iterations
27 const interationBase = 1
28
29 backoff := autopaho.DefaultExponentialBackoff()
30
31 minBackoff := int64(math.MaxInt64)
32 maxBackoff := int64(0)
33 iterationsTotal := 0
34
35 zeroAttmeptBackoffTime := backoff(0).Milliseconds()
36 fmt.Printf("Backoff for attempt '0' : %d\n", zeroAttmeptBackoffTime)
37
38 for i := 0; i < 22; i++ {
39 iterations := interationBase << i
40 for j := 0; j < iterations; j++ {
41 iterationsTotal++
42 backoffTime := backoff(iterationsTotal).Milliseconds()
43 if backoffTime < minBackoff {
44 minBackoff = backoffTime
45 }
46 if backoffTime > maxBackoff {
47 maxBackoff = backoffTime
48 }
49 }
50
51 fmt.Printf("After % 8d iterations, min: %d, max: % 7d\n", iterationsTotal, minBackoff, maxBackoff)
52 }
53 }