github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/utils/backoff.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"time"
     8  )
     9  
    10  var backoffTimeouts = []time.Duration{50 * time.Millisecond, 100 * time.Millisecond, 200 * time.Millisecond, 200 * time.Millisecond, 400 * time.Millisecond, 400 * time.Millisecond}
    11  
    12  // ProgressiveRetry executes a BackoffOperation and waits an increasing time before retrying the operation.
    13  func ProgressiveRetry(operation func() error) error {
    14  	var err error
    15  
    16  	for attempts := 0; attempts < len(backoffTimeouts); attempts++ {
    17  		err = operation()
    18  		if err == nil {
    19  			return nil
    20  		}
    21  
    22  		time.Sleep(backoffTimeouts[attempts])
    23  	}
    24  
    25  	return err
    26  }