summaryrefslogtreecommitdiff
path: root/vendor/github.com/eclipse/paho.mqtt.golang/backoff.go
blob: 8ee06f6961c59eca777015f59e7955f1f1869113 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2021 IBM Corp and others.
 *
 * 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.
 *
 * Contributors:
 *    Matt Brittan
 *    Daichi Tomaru
 */

package mqtt

import (
	"sync"
	"time"
)

// Controller for sleep with backoff when the client attempts reconnection
// It has statuses for each situations cause reconnection.
type backoffController struct {
	sync.RWMutex
	statusMap map[string]*backoffStatus
}

type backoffStatus struct {
	lastSleepPeriod time.Duration
	lastErrorTime   time.Time
}

func newBackoffController() *backoffController {
	return &backoffController{
		statusMap: map[string]*backoffStatus{},
	}
}

// Calculate next sleep period from the specified parameters.
// Returned values are next sleep period and whether the error situation is continual.
// If connection errors continuouslly occurs, its sleep period is exponentially increased.
// Also if there is a lot of time between last and this error, sleep period is initialized.
func (b *backoffController) getBackoffSleepTime(
	situation string, initSleepPeriod time.Duration, maxSleepPeriod time.Duration, processTime time.Duration, skipFirst bool,
) (time.Duration, bool) {
	// Decide first sleep time if the situation is not continual. 
	var firstProcess = func(status *backoffStatus, init time.Duration, skip bool) (time.Duration, bool) {
		if skip {
			status.lastSleepPeriod = 0
			return 0, false
		}
		status.lastSleepPeriod = init
		return init, false
	}

	// Prioritize maxSleep.
	if initSleepPeriod > maxSleepPeriod {
		initSleepPeriod = maxSleepPeriod
	}
	b.Lock()
	defer b.Unlock()

	status, exist := b.statusMap[situation]
	if !exist {
		b.statusMap[situation] = &backoffStatus{initSleepPeriod, time.Now()}
		return firstProcess(b.statusMap[situation], initSleepPeriod, skipFirst)
	}

	oldTime := status.lastErrorTime
	status.lastErrorTime = time.Now()

	// When there is a lot of time between last and this error, sleep period is initialized.
	if status.lastErrorTime.Sub(oldTime) > (processTime * 2 + status.lastSleepPeriod) {
		return firstProcess(status, initSleepPeriod, skipFirst)
	}

	if status.lastSleepPeriod == 0 {
		status.lastSleepPeriod = initSleepPeriod
		return initSleepPeriod, true
	}

	if nextSleepPeriod := status.lastSleepPeriod * 2; nextSleepPeriod <= maxSleepPeriod {
		status.lastSleepPeriod = nextSleepPeriod
	} else {
		status.lastSleepPeriod = maxSleepPeriod
	}

	return status.lastSleepPeriod, true
}

// Execute sleep the time returned from getBackoffSleepTime.
func (b *backoffController) sleepWithBackoff(
	situation string, initSleepPeriod time.Duration, maxSleepPeriod time.Duration, processTime time.Duration, skipFirst bool,
) (time.Duration, bool) {
	sleep, isFirst := b.getBackoffSleepTime(situation, initSleepPeriod, maxSleepPeriod, processTime, skipFirst)
	if sleep != 0 {
		time.Sleep(sleep)
	}
	return sleep, isFirst
}