File: config.go

package info (click to toggle)
golang-github-canonical-go-dqlite 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 712 kB
  • sloc: sh: 380; makefile: 5
file content (44 lines) | stat: -rw-r--r-- 1,628 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package protocol

import (
	"time"

	"github.com/Rican7/retry/backoff"
	"github.com/Rican7/retry/strategy"
)

// Config holds various configuration parameters for a dqlite client.
type Config struct {
	Dial                  DialFunc      // Network dialer.
	DialTimeout           time.Duration // Timeout for establishing a network connection .
	AttemptTimeout        time.Duration // Timeout for each individual attempt to probe a server's leadership.
	BackoffFactor         time.Duration // Exponential backoff factor for retries.
	BackoffCap            time.Duration // Maximum connection retry backoff value,
	RetryLimit            uint          // Maximum number of retries, or 0 for unlimited.
	ConcurrentLeaderConns int64         // Maximum number of concurrent connections to other cluster members while probing for leadership.
	PermitShared          bool
}

// RetryStrategies returns a configuration for the retry package based on a Config.
func (config Config) RetryStrategies() (strategies []strategy.Strategy) {
	limit, factor, cap := config.RetryLimit, config.BackoffFactor, config.BackoffCap
	// Fix for change in behavior: https://github.com/Rican7/retry/pull/12
	if limit++; limit > 1 {
		strategies = append(strategies, strategy.Limit(limit))
	}
	backoffFunc := backoff.BinaryExponential(factor)
	strategies = append(strategies,
		func(attempt uint) bool {
			if attempt > 0 {
				duration := backoffFunc(attempt)
				// Duration might be negative in case of integer overflow.
				if !(0 < duration && duration <= cap) {
					duration = cap
				}
				time.Sleep(duration)
			}
			return true
		},
	)
	return
}