github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/globalclock/interface.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package globalclock
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  )
    11  
    12  var (
    13  	// ErrConcurrentUpdate is returned by Updater.Advance when the
    14  	// clock value has been changed since the last read.
    15  	ErrConcurrentUpdate = errors.New("clock was updated concurrently, retry")
    16  
    17  	// ErrTimeout is returned by Updater.Advance if the attempt to
    18  	// update the global clock timed out - in that case the advance
    19  	// should be tried again.
    20  	ErrTimeout = errors.New("clock update timed out, retry")
    21  )
    22  
    23  // Updater provides a means of updating the global clock time.
    24  type Updater interface {
    25  	// Advance adds the given duration to the global clock, ensuring
    26  	// that the clock has not been updated concurrently.
    27  	//
    28  	// Advance will return ErrConcurrentUpdate if another updater
    29  	// updates the clock concurrently. In this case, the updater
    30  	// will refresh its view of the clock, and the caller can
    31  	// attempt Advance later.
    32  	//
    33  	// If Advance returns any error other than ErrConcurrentUpdate or
    34  	// ErrTimeout the Updater should be considered invalid, and the
    35  	// caller should obtain a new Updater. Failing to do so could lead
    36  	// to non-monotonic time, since there is no way of knowing in
    37  	// general whether or not the database was updated.
    38  	Advance(d time.Duration) error
    39  }
    40  
    41  // IsConcurrentUpdate returns whether the specified error represents
    42  // ErrConcurrentUpdate (even if it's wrapped).
    43  func IsConcurrentUpdate(err error) bool {
    44  	return errors.Cause(err) == ErrConcurrentUpdate
    45  }
    46  
    47  // IsTimeout returns whether the specified error represents ErrTimeout
    48  // (even if it's wrapped).
    49  func IsTimeout(err error) bool {
    50  	return errors.Cause(err) == ErrTimeout
    51  }