File: monotonic.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package missinggo

import (
	"sync"
	"time"
)

// Monotonic time represents time since an arbitrary point in the past, where
// the concept of now is only ever moving in a positive direction.
type MonotonicTime struct {
	skewedStdTime time.Time
}

func (me MonotonicTime) Sub(other MonotonicTime) time.Duration {
	return me.skewedStdTime.Sub(other.skewedStdTime)
}

var (
	stdNowFunc    = time.Now
	monotonicMu   sync.Mutex
	lastStdNow    time.Time
	monotonicSkew time.Duration
)

func skewedStdNow() time.Time {
	monotonicMu.Lock()
	defer monotonicMu.Unlock()
	stdNow := stdNowFunc()
	if !lastStdNow.IsZero() && stdNow.Before(lastStdNow) {
		monotonicSkew += lastStdNow.Sub(stdNow)
	}
	lastStdNow = stdNow
	return stdNow.Add(monotonicSkew)
}

// Consecutive calls always produce the same or greater time than previous
// calls.
func MonotonicNow() MonotonicTime {
	return MonotonicTime{skewedStdNow()}
}

func MonotonicSince(since MonotonicTime) (ret time.Duration) {
	return skewedStdNow().Sub(since.skewedStdTime)
}