Skip to content
Snippets Groups Projects
tries.go 724 B
Newer Older
  • Learn to ignore specific revisions
  • ale's avatar
    ale committed
    package backoff
    
    import "time"
    
    /*
    
    WithMaxRetries creates a wrapper around another BackOff, which will
    
    ale's avatar
    ale committed
    return Stop if NextBackOff() has been called too many times since
    the last time Reset() was called
    
    Note: Implementation is not thread-safe.
    */
    
    func WithMaxRetries(b BackOff, max uint64) BackOff {
    
    ale's avatar
    ale committed
    	return &backOffTries{delegate: b, maxTries: max}
    }
    
    type backOffTries struct {
    	delegate BackOff
    	maxTries uint64
    	numTries uint64
    }
    
    func (b *backOffTries) NextBackOff() time.Duration {
    
    	if b.maxTries == 0 {
    		return Stop
    	}
    
    ale's avatar
    ale committed
    	if b.maxTries > 0 {
    		if b.maxTries <= b.numTries {
    			return Stop
    		}
    		b.numTries++
    	}
    	return b.delegate.NextBackOff()
    }
    
    func (b *backOffTries) Reset() {
    	b.numTries = 0
    	b.delegate.Reset()
    }