github.com/sap/cf-mta-plugin@v2.6.3+incompatible/clients/baseclient/client_util.go (about)

     1  package baseclient
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
     7  )
     8  
     9  // CallWithRetry executes callback with retry
    10  func CallWithRetry(callback func() (interface{}, error), maxRetriesCount int, retryInterval time.Duration) (interface{}, error) {
    11  	for index := 0; index < maxRetriesCount; index++ {
    12  		resp, err := callback()
    13  		if !shouldRetry(err) {
    14  			return resp, err
    15  		}
    16  		ui.Warn("Error occurred: %s. Retrying after: %s.", err.Error(), retryInterval)
    17  		time.Sleep(retryInterval)
    18  	}
    19  	return callback()
    20  }
    21  
    22  func shouldRetry(err error) bool {
    23  	if err == nil {
    24  		return false
    25  	}
    26  	ae, ok := err.(*ClientError)
    27  	if ok {
    28  		httpCode := ae.Code
    29  		httpCodeMajorDigit := httpCode / 100
    30  		return httpCodeMajorDigit != 2
    31  	}
    32  	return true
    33  }