github.com/m3db/m3@v1.5.0/src/x/clock/types.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package clock implements facilities for working with wall clock time.
    22  package clock
    23  
    24  import (
    25  	"time"
    26  )
    27  
    28  // NowFn is the function supplied to determine "now".
    29  type NowFn func() time.Time
    30  
    31  // Options represents the options for the clock.
    32  type Options interface {
    33  	// SetNowFn sets the NowFn.
    34  	SetNowFn(value NowFn) Options
    35  
    36  	// NowFn returns the NowFn.
    37  	NowFn() NowFn
    38  
    39  	// SetMaxPositiveSkew sets the maximum positive clock skew
    40  	// with regard to a reference clock.
    41  	SetMaxPositiveSkew(value time.Duration) Options
    42  
    43  	// MaxPositiveSkew returns the maximum positive clock skew
    44  	// with regard to a reference clock.
    45  	MaxPositiveSkew() time.Duration
    46  
    47  	// SetMaxNegativeSkew sets the maximum negative clock skew
    48  	// with regard to a reference clock.
    49  	SetMaxNegativeSkew(value time.Duration) Options
    50  
    51  	// MaxNegativeSkew returns the maximum negative clock skew
    52  	// with regard to a reference clock.
    53  	MaxNegativeSkew() time.Duration
    54  }
    55  
    56  // ConditionFn specifies a predicate to check.
    57  type ConditionFn func() bool
    58  
    59  // WaitUntil returns true if the condition specified evaluated to
    60  // true before the timeout, false otherwise.
    61  func WaitUntil(fn ConditionFn, timeout time.Duration) bool {
    62  	deadline := time.Now().Add(timeout)
    63  	for time.Now().Before(deadline) {
    64  		if fn() {
    65  			return true
    66  		}
    67  		time.Sleep(100 * time.Millisecond)
    68  	}
    69  	return false
    70  }