github.com/profzone/eden-framework@v1.0.10/pkg/client/postgre/retry.go (about)

     1  package confpostgres
     2  
     3  import (
     4  	"github.com/profzone/envconfig"
     5  	"time"
     6  
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  type Retry struct {
    11  	Repeats  int
    12  	Interval envconfig.Duration
    13  }
    14  
    15  func (r *Retry) SetDefaults() {
    16  	if r.Repeats == 0 {
    17  		r.Repeats = 3
    18  	}
    19  	if r.Interval == 0 {
    20  		r.Interval = envconfig.Duration(10 * time.Second)
    21  	}
    22  }
    23  
    24  func (r Retry) Do(exec func() error) (err error) {
    25  	if r.Repeats <= 0 {
    26  		err = exec()
    27  		return
    28  	}
    29  	for i := 0; i < r.Repeats; i++ {
    30  		err = exec()
    31  		if err != nil {
    32  			logrus.Warningf("retry in seconds [%d]", r.Interval)
    33  			time.Sleep(time.Duration(r.Interval))
    34  		}
    35  	}
    36  	return
    37  }