You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openim-sdk-cpp/go/chao-sdk-core/internal/interaction/reconnect.go

32 lines
550 B

package interaction
import (
"time"
)
type ReconnectStrategy interface {
GetSleepInterval() time.Duration
Reset()
}
type ExponentialRetry struct {
attempts []int
index int
}
func NewExponentialRetry() *ExponentialRetry {
return &ExponentialRetry{
attempts: []int{1, 2, 4, 8, 16},
index: -1,
}
}
func (rs *ExponentialRetry) GetSleepInterval() time.Duration {
rs.index++
interval := rs.index % len(rs.attempts)
return time.Second * time.Duration(rs.attempts[interval])
}
func (rs *ExponentialRetry) Reset() {
rs.index = -1
}