github.com/searKing/golang/go@v1.2.117/time/delay.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time
     6  
     7  // Deprecated: Use BackOff instead.
     8  //
     9  //import (
    10  //	"math/rand"
    11  //	"time"
    12  //)
    13  //
    14  //const DefaultBaseDuration = 5 * time.Millisecond
    15  //const DefaultMaxDuration = 1 * time.Second
    16  //
    17  //const ZeroDuration = 0
    18  //
    19  //type DelayHandler interface {
    20  //	Delay(attempt int, cap, base, last time.Duration) (delay time.Duration)
    21  //}
    22  //
    23  //// The HandlerFunc type is an adapter to allow the use of
    24  //// ordinary functions as HTTP handlers. If f is a function
    25  //// with the appropriate signature, HandlerFunc(f) is a
    26  //// Handler that calls f.
    27  //type DelayHandlerFunc func(attempt int, cap, base, last time.Duration) (delay time.Duration)
    28  //
    29  //// ServeHTTP calls f(w, r).
    30  //func (f DelayHandlerFunc) Delay(attempt int, cap, base, last time.Duration) (delay time.Duration) {
    31  //	return f(attempt, cap, base, last)
    32  //}
    33  //
    34  //// Exponential backoff is an algorithm that uses feedback to multiplicatively decrease the rate of some process,
    35  //// in order to gradually find an acceptable rate
    36  //// see https://cloud.google.com/iot/docs/how-tos/exponential-backoff
    37  //// see https://amazonaws-china.com/cn/blogs/architecture/exponential-backoff-and-jitter/
    38  //
    39  //// Work(calls) of Competing Clients(less is better)
    40  //// None > Exponential > DecorrelatedJitter > EqualJitter > FullJitter
    41  //// Looking at the amount of client work, the number of calls is approximately the same for “Full” and “Equal” jitter,
    42  //// and higher for “Decorrelated”. Both cut down work substantially relative to both the no-jitter approaches.
    43  //
    44  //// Completion Time(ms) of Competing Clients(less is better)
    45  //// Exponential > EqualJitter > FullJitter > DecorrelatedJitter > None
    46  //
    47  //// none backed-off
    48  //// sleep = min(cap, base)
    49  //func NoneBackOffDelayHandler(_ int, cap, base, _ time.Duration) time.Duration {
    50  //	delay := base
    51  //	if delay > cap {
    52  //		delay = cap
    53  //	}
    54  //	return delay
    55  //}
    56  //
    57  //// exponentially backed-off
    58  //// sleep = min(cap, base * 2 ** attempt)
    59  //func ExponentialDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration {
    60  //	delay := base * (2 << attempt)
    61  //	if delay > cap {
    62  //		delay = cap
    63  //	}
    64  //	return delay
    65  //}
    66  //
    67  //// exponentially backed-off with full jitter
    68  //// sleep = random_between(0, min(cap, base * 2 ** attempt))
    69  //func FullJitterDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration {
    70  //	delay := base * (2 << attempt)
    71  //	if delay > cap {
    72  //		delay = cap
    73  //	}
    74  //	return time.Duration(rand.Float64() * float64(delay))
    75  //}
    76  //
    77  //// exponentially backed-off with equal jitter
    78  //// temp = min(cap, base * 2 ** attempt)
    79  //// sleep = temp/2 + random_between(temp/2, min(cap, base * 2 ** attempt))
    80  //func EqualJitterDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration {
    81  //	temp := base * (2 << attempt)
    82  //	if temp > cap {
    83  //		temp = cap
    84  //	}
    85  //	delay := temp / 2
    86  //	return delay + time.Duration(float64(delay)+rand.Float64()*float64(delay))
    87  //}
    88  //
    89  //// exponentially backed-off with decorrelated jitter
    90  //// sleep = min(cap, random_between(base, sleep * 3))
    91  //func DecorrelatedJitterDelayHandler(_ int, cap, base, last time.Duration) time.Duration {
    92  //	delay := base + time.Duration(rand.Float64()*float64(last*3-base))
    93  //	if delay > cap {
    94  //		delay = cap
    95  //	}
    96  //	return delay
    97  //}
    98  //
    99  //func NewDelay(base, cap time.Duration, h DelayHandler) *Delay {
   100  //	return &Delay{
   101  //		Base:    base,
   102  //		Cap:     cap,
   103  //		Handler: h,
   104  //	}
   105  //}
   106  //
   107  //func NewDefaultExponentialDelay() *Delay {
   108  //	return &Delay{
   109  //		Base:    DefaultBaseDuration,
   110  //		Cap:     DefaultMaxDuration,
   111  //		Handler: DelayHandlerFunc(ExponentialDelayHandler),
   112  //	}
   113  //}
   114  //
   115  //type Delay struct {
   116  //	attempt int
   117  //	delay   time.Duration
   118  //	Base    time.Duration
   119  //	Cap     time.Duration
   120  //	Handler DelayHandler
   121  //}
   122  //
   123  //func (d *Delay) Update() {
   124  //	defer func() { d.attempt++ }()
   125  //	h := d.Handler
   126  //	if h == nil {
   127  //		h = DelayHandlerFunc(NoneBackOffDelayHandler)
   128  //	}
   129  //
   130  //	d.delay = h.Delay(d.attempt, d.Cap, d.Base, d.delay)
   131  //	if max := d.Cap; d.delay > max {
   132  //		d.delay = max
   133  //	}
   134  //}
   135  //
   136  //func (d *Delay) Sleep() {
   137  //	d.Update()
   138  //	time.Sleep(d.delay)
   139  //}
   140  //
   141  //func (d *Delay) Delay() <-chan time.Time {
   142  //	d.Update()
   143  //	return After(d.delay)
   144  //}
   145  //
   146  //func (d *Delay) DelayFunc(f func()) *Timer {
   147  //	d.Update()
   148  //	return AfterFunc(d.delay, f)
   149  //}
   150  //
   151  //// Reset to initial state.
   152  //func (d *Delay) Reset() {
   153  //	d.delay = ZeroDuration
   154  //	d.attempt = 0
   155  //}
   156  //
   157  //// Gets duration to wait before retrying the operation or {@link #STOP} to
   158  //// indicate that no retries should be made.
   159  //func (d *Delay) NextBackOff() (backoff time.Duration, ok bool) {
   160  //	d.Update()
   161  //	return d.delay, ok
   162  //}