github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/timeutil/time_source.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package timeutil
    12  
    13  import "time"
    14  
    15  // TimeSource is used to interact with clocks and timers. Generally exposed for
    16  // testing.
    17  type TimeSource interface {
    18  	Now() time.Time
    19  	Since(t time.Time) time.Duration
    20  	NewTimer() TimerI
    21  	NewTicker(duration time.Duration) TickerI
    22  }
    23  
    24  // TimerI is an interface wrapping Timer.
    25  type TimerI interface {
    26  
    27  	// Reset will set the timer to notify on Ch() after duration.
    28  	Reset(duration time.Duration)
    29  
    30  	// Stop must only be called one time per timer.
    31  	Stop() bool
    32  
    33  	// Ch returns the channel which will be notified when the timer reaches its
    34  	// time.
    35  	Ch() <-chan time.Time
    36  
    37  	// MarkRead should be called when a value is read from the Ch() channel.
    38  	// If MarkRead is not called, the resetting the timer is less efficient.
    39  	MarkRead()
    40  }
    41  
    42  // TickerI is an interface wrapping Ticker.
    43  type TickerI interface {
    44  	// Reset stops a ticker and resets its period to the specified duration. The
    45  	// next tick will arrive after the new period elapses.
    46  	Reset(duration time.Duration)
    47  
    48  	// Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does
    49  	// not close the channel, to prevent a concurrent goroutine reading from the
    50  	// channel from seeing an erroneous "tick".
    51  	Stop()
    52  
    53  	// Ch returns the channel on which the ticks are delivered.
    54  	Ch() <-chan time.Time
    55  }
    56  
    57  // DefaultTimeSource is a TimeSource using the system clock.
    58  type DefaultTimeSource struct{}
    59  
    60  var _ TimeSource = DefaultTimeSource{}
    61  
    62  // Now returns timeutil.Now().
    63  func (DefaultTimeSource) Now() time.Time {
    64  	return Now()
    65  }
    66  
    67  // Since implements TimeSource interface
    68  func (DefaultTimeSource) Since(t time.Time) time.Duration {
    69  	return Since(t)
    70  }
    71  
    72  // NewTimer returns a TimerI wrapping *Timer.
    73  func (DefaultTimeSource) NewTimer() TimerI {
    74  	return (*timer)(NewTimer())
    75  }
    76  
    77  // NewTicker creates a new ticker.
    78  func (DefaultTimeSource) NewTicker(duration time.Duration) TickerI {
    79  	return (*ticker)(time.NewTicker(duration))
    80  }
    81  
    82  type timer Timer
    83  
    84  var _ TimerI = (*timer)(nil)
    85  
    86  func (t *timer) Reset(duration time.Duration) {
    87  	(*Timer)(t).Reset(duration)
    88  }
    89  
    90  func (t *timer) Stop() bool {
    91  	return (*Timer)(t).Stop()
    92  }
    93  
    94  func (t *timer) Ch() <-chan time.Time {
    95  	return t.C
    96  }
    97  
    98  func (t *timer) MarkRead() {
    99  	t.Read = true
   100  }
   101  
   102  type ticker time.Ticker
   103  
   104  var _ TickerI = (*ticker)(nil)
   105  
   106  // Reset is part of the TickerI interface.
   107  func (t *ticker) Reset(duration time.Duration) {
   108  	(*time.Ticker)(t).Reset(duration)
   109  }
   110  
   111  // Stop is part of the TickerI interface.
   112  func (t *ticker) Stop() {
   113  	(*time.Ticker)(t).Stop()
   114  }
   115  
   116  // Ch is part of the TickerI interface.
   117  func (t *ticker) Ch() <-chan time.Time {
   118  	return (*time.Ticker)(t).C
   119  }