github.com/sap/cf-mta-plugin@v2.6.3+incompatible/clients/cfrestclient/resilient/resilient_rest_cloud_foundry_client_extended.go (about)

     1  package resilient
     2  
     3  import (
     4  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/cfrestclient"
     5  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
     6  	"time"
     7  )
     8  
     9  type ResilientCloudFoundryRestClient struct {
    10  	CloudFoundryRestClient cfrestclient.CloudFoundryOperationsExtended
    11  	MaxRetriesCount        int
    12  	RetryInterval          time.Duration
    13  }
    14  
    15  func NewResilientCloudFoundryClient(cloudFoundryRestClient cfrestclient.CloudFoundryOperationsExtended, maxRetriesCount int, retryIntervalInSeconds int) cfrestclient.CloudFoundryOperationsExtended {
    16  	return &ResilientCloudFoundryRestClient{cloudFoundryRestClient, maxRetriesCount, time.Second * time.Duration(retryIntervalInSeconds)}
    17  }
    18  
    19  func (c ResilientCloudFoundryRestClient) GetSharedDomains() ([]models.SharedDomain, error) {
    20  	sharedDomains, err := c.CloudFoundryRestClient.GetSharedDomains()
    21  	for shouldRetry(c.MaxRetriesCount, err) {
    22  		sharedDomains, err = c.CloudFoundryRestClient.GetSharedDomains()
    23  		c.MaxRetriesCount--
    24  		time.Sleep(c.RetryInterval)
    25  	}
    26  	return sharedDomains, err
    27  }
    28  
    29  func shouldRetry(retries int, err error) bool {
    30  	return err != nil && retries > 0
    31  }