package mclock
import "github.com/ethereum/go-ethereum/common/mclock"
Package mclock is a wrapper for a monotonic clock source
Index ¶
- type AbsTime
- func Now() AbsTime
- func (t AbsTime) Add(d time.Duration) AbsTime
- func (t AbsTime) Sub(t2 AbsTime) time.Duration
- type Alarm
- func NewAlarm(clock Clock) *Alarm
- func (e *Alarm) C() <-chan struct{}
- func (e *Alarm) Schedule(time AbsTime)
- func (e *Alarm) Stop()
- type ChanTimer
- type Clock
- type Simulated
- func (s *Simulated) ActiveTimers() int
- func (s *Simulated) After(d time.Duration) <-chan AbsTime
- func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer
- func (s *Simulated) NewTimer(d time.Duration) ChanTimer
- func (s *Simulated) Now() AbsTime
- func (s *Simulated) Run(d time.Duration)
- func (s *Simulated) Sleep(d time.Duration)
- func (s *Simulated) WaitForTimers(n int)
- type System
- func (c System) After(d time.Duration) <-chan AbsTime
- func (c System) AfterFunc(d time.Duration, f func()) Timer
- func (c System) NewTimer(d time.Duration) ChanTimer
- func (c System) Now() AbsTime
- func (c System) Sleep(d time.Duration)
- type Timer
Types ¶
type AbsTime ¶
type AbsTime int64
AbsTime represents absolute monotonic time.
func Now ¶
func Now() AbsTime
Now returns the current absolute monotonic time.
func (AbsTime) Add ¶
Add returns t + d as absolute time.
func (AbsTime) Sub ¶
Sub returns t - t2 as a duration.
type Alarm ¶
type Alarm struct {
// contains filtered or unexported fields
}
Alarm sends timed notifications on a channel. This is very similar to a regular timer, but is easier to use in code that needs to re-schedule the same timer over and over.
When scheduling an Alarm, the channel returned by C() will receive a value no later than the scheduled time. An Alarm can be reused after it has fired and can also be canceled by calling Stop.
func NewAlarm ¶
NewAlarm creates an Alarm.
func (*Alarm) C ¶
func (e *Alarm) C() <-chan struct{}
C returns the alarm notification channel. This channel remains identical for the entire lifetime of the alarm, and is never closed.
func (*Alarm) Schedule ¶
Schedule sets the alarm to fire no later than the given time. If the alarm was already scheduled but has not fired yet, it may fire earlier than the newly-scheduled time.
func (*Alarm) Stop ¶
func (e *Alarm) Stop()
Stop cancels the alarm and drains the channel. This method is not safe for concurrent use.
type ChanTimer ¶
type ChanTimer interface { Timer // The channel returned by C receives a value when the timer expires. C() <-chan AbsTime // Reset reschedules the timer with a new timeout. // It should be invoked only on stopped or expired timers with drained channels. Reset(time.Duration) }
ChanTimer is a cancellable event created by NewTimer.
type Clock ¶
type Clock interface { Now() AbsTime Sleep(time.Duration) NewTimer(time.Duration) ChanTimer After(time.Duration) <-chan AbsTime AfterFunc(d time.Duration, f func()) Timer }
The Clock interface makes it possible to replace the monotonic system clock with a simulated clock.
type Simulated ¶
type Simulated struct {
// contains filtered or unexported fields
}
Simulated implements a virtual Clock for reproducible time-sensitive tests. It simulates a scheduler on a virtual timescale where actual processing takes zero time.
The virtual clock doesn't advance on its own, call Run to advance it and execute timers. Since there is no way to influence the Go scheduler, testing timeout behaviour involving goroutines needs special care. A good way to test such timeouts is as follows: First perform the action that is supposed to time out. Ensure that the timer you want to test is created. Then run the clock until after the timeout. Finally observe the effect of the timeout using a channel or semaphore.
func (*Simulated) ActiveTimers ¶
ActiveTimers returns the number of timers that haven't fired.
func (*Simulated) After ¶
After returns a channel which receives the current time after the clock has advanced by d.
func (*Simulated) AfterFunc ¶
AfterFunc runs fn after the clock has advanced by d. Unlike with the system clock, fn runs on the goroutine that calls Run.
func (*Simulated) NewTimer ¶
NewTimer creates a timer which fires when the clock has advanced by d.
func (*Simulated) Now ¶
Now returns the current virtual time.
func (*Simulated) Run ¶
Run moves the clock by the given duration, executing all timers before that duration.
func (*Simulated) Sleep ¶
Sleep blocks until the clock has advanced by d.
func (*Simulated) WaitForTimers ¶
WaitForTimers waits until the clock has at least n scheduled timers.
type System ¶
type System struct{}
System implements Clock using the system clock.
func (System) After ¶
After returns a channel which receives the current time after d has elapsed.
func (System) AfterFunc ¶
AfterFunc runs f on a new goroutine after the duration has elapsed.
func (System) NewTimer ¶
NewTimer creates a timer which can be rescheduled.
func (System) Now ¶
Now returns the current monotonic time.
func (System) Sleep ¶
Sleep blocks for the given duration.
type Timer ¶
type Timer interface { // Stop cancels the timer. It returns false if the timer has already // expired or been stopped. Stop() bool }
Timer is a cancellable event created by AfterFunc.
Source Files ¶
alarm.go mclock.go simclock.go
- Version
- v1.15.11 (latest)
- Published
- May 5, 2025
- Platform
- linux/amd64
- Imports
- 4 packages
- Last checked
- 1 day ago –
Tools for package owners.