github.com/vmg/backoff@v1.0.0/example_test.go (about)

     1  package backoff
     2  
     3  import "log"
     4  
     5  func ExampleRetry() error {
     6  	operation := func() error {
     7  		// An operation that might fail.
     8  		return nil // or return errors.New("some error")
     9  	}
    10  
    11  	err := Retry(operation, NewExponentialBackOff())
    12  	if err != nil {
    13  		// Handle error.
    14  		return err
    15  	}
    16  
    17  	// Operation is successful.
    18  	return nil
    19  }
    20  
    21  func ExampleTicker() error {
    22  	operation := func() error {
    23  		// An operation that might fail
    24  		return nil // or return errors.New("some error")
    25  	}
    26  
    27  	b := NewExponentialBackOff()
    28  	ticker := NewTicker(b)
    29  
    30  	var err error
    31  
    32  	// Ticks will continue to arrive when the previous operation is still running,
    33  	// so operations that take a while to fail could run in quick succession.
    34  	for _ = range ticker.C {
    35  		if err = operation(); err != nil {
    36  			log.Println(err, "will retry...")
    37  			continue
    38  		}
    39  
    40  		ticker.Stop()
    41  		break
    42  	}
    43  
    44  	if err != nil {
    45  		// Operation has failed.
    46  		return err
    47  	}
    48  
    49  	// Operation is successful.
    50  	return nil
    51  }