update-repos/internal/retry/linear.go
2023-12-06 14:24:08 +01:00

46 lines
974 B
Go

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2023 Thorsten Schubert <tschubert@bafh.org>
package retry
import "time"
type LinearBackoff struct {
initialDelay time.Duration
currentDelay time.Duration
maxRetries uint
currentRetries uint
}
func NewLinearBackoff(initialDelay time.Duration, maxRetries uint) *LinearBackoff {
return &LinearBackoff{
initialDelay: initialDelay,
currentDelay: initialDelay,
maxRetries: maxRetries,
}
}
func (s *LinearBackoff) NextDelay() time.Duration {
defer func() { s.currentDelay += s.initialDelay }()
return s.currentDelay
}
func (s *LinearBackoff) Next() bool {
if s.currentRetries >= s.maxRetries {
return false
}
s.currentRetries++
return true
}
func (s *LinearBackoff) Retries() (uint, uint) {
return s.currentRetries, s.maxRetries
}
func (s *LinearBackoff) Name() string {
return "linear"
}
func (s *LinearBackoff) Reset() {
s.currentDelay, s.currentRetries = s.initialDelay, 0
}