github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/poller.go (about)

     1  package broker
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"k8s.io/apimachinery/pkg/util/wait"
     8  )
     9  
    10  type Poller interface {
    11  	Invoke(logic func() (bool, error)) error
    12  }
    13  
    14  type DefaultPoller struct {
    15  	PollInterval time.Duration
    16  	PollTimeout  time.Duration
    17  }
    18  
    19  func NewDefaultPoller() Poller {
    20  	return &DefaultPoller{
    21  		PollInterval: 2 * time.Second,
    22  		PollTimeout:  5 * time.Second,
    23  	}
    24  }
    25  
    26  func (p *DefaultPoller) Invoke(logic func() (bool, error)) error {
    27  	return wait.PollImmediate(p.PollInterval, p.PollTimeout, func() (bool, error) {
    28  		return logic()
    29  	})
    30  }
    31  
    32  type PassthroughPoller struct {
    33  }
    34  
    35  func NewPassthroughPoller() Poller {
    36  	return &PassthroughPoller{}
    37  }
    38  
    39  func (p *PassthroughPoller) Invoke(logic func() (bool, error)) error {
    40  	success, err := logic()
    41  	if !success && err == nil {
    42  		return fmt.Errorf("unsuccessful poll logic invocation")
    43  	}
    44  	return err
    45  }