/
timed_version.go
169 lines (140 loc) · 3.69 KB
/
timed_version.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"fmt"
"sync"
"time"
"go.uber.org/atomic"
"github.com/akhmadali22/protocol/livekit"
)
const tickBits uint64 = 13
const tickMask uint64 = (1 << tickBits) - 1
var epoch = time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC).UnixMicro()
type TimedVersionGenerator interface {
New() *TimedVersion
Next() TimedVersion
}
func timedVersionComponents(v uint64) (ts int64, ticks int32) {
return int64(v>>tickBits) + epoch, int32(v & tickMask)
}
func timedVersionFromComponents(ts int64, ticks int32) TimedVersion {
if ts < epoch {
ts = epoch
}
return TimedVersion{v: *atomic.NewUint64((uint64(ts-epoch) << tickBits) | uint64(ticks))}
}
type timedVersionGenerator struct {
mu sync.Mutex
ts int64
ticks uint64
}
func NewDefaultTimedVersionGenerator() TimedVersionGenerator {
return new(timedVersionGenerator)
}
func (g *timedVersionGenerator) New() *TimedVersion {
v := g.Next()
return &v
}
func (g *timedVersionGenerator) Next() TimedVersion {
ts := time.Now().UnixMicro()
g.mu.Lock()
defer g.mu.Unlock()
for {
if ts < g.ts {
ts = g.ts
}
if g.ts == ts {
// if incrementing the ticks would overflow the version sleep for a
// microsecond then try again.
if g.ticks == tickMask {
time.Sleep(time.Microsecond)
ts = time.Now().UnixMicro()
continue
}
g.ticks++
} else {
g.ts = ts
g.ticks = 0
}
return timedVersionFromComponents(g.ts, int32(g.ticks))
}
}
type TimedVersion struct {
v atomic.Uint64
}
func NewTimedVersionFromProto(proto *livekit.TimedVersion) *TimedVersion {
v := timedVersionFromComponents(proto.GetUnixMicro(), proto.GetTicks())
return &v
}
func NewTimedVersionFromTime(t time.Time) *TimedVersion {
v := timedVersionFromComponents(t.UnixMicro(), 0)
return &v
}
func TimedVersionFromProto(proto *livekit.TimedVersion) TimedVersion {
return timedVersionFromComponents(proto.GetUnixMicro(), proto.GetTicks())
}
func TimedVersionFromTime(t time.Time) TimedVersion {
return timedVersionFromComponents(t.UnixMicro(), 0)
}
func (t *TimedVersion) Update(other *TimedVersion) bool {
ov := other.v.Load()
for {
prev := t.v.Load()
if ov <= prev {
return false
}
if t.v.CompareAndSwap(prev, ov) {
return true
}
}
}
func (t *TimedVersion) Store(other *TimedVersion) {
t.v.Store(other.v.Load())
}
func (t *TimedVersion) Load() TimedVersion {
return TimedVersion{v: *atomic.NewUint64(t.v.Load())}
}
func (t *TimedVersion) After(other *TimedVersion) bool {
return t.v.Load() > other.v.Load()
}
func (t *TimedVersion) Compare(other *TimedVersion) int {
ov := other.v.Load()
v := t.v.Load()
if v < ov {
return -1
}
if v == ov {
return 0
}
return 1
}
func (t *TimedVersion) IsZero() bool {
return t.v.Load() == 0
}
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
ts, ticks := timedVersionComponents(t.v.Load())
return &livekit.TimedVersion{
UnixMicro: ts,
Ticks: ticks,
}
}
func (t *TimedVersion) Time() time.Time {
ts, _ := timedVersionComponents(t.v.Load())
return time.UnixMicro(ts)
}
func (t *TimedVersion) String() string {
ts, ticks := timedVersionComponents(t.v.Load())
return fmt.Sprintf("%d.%d", ts, ticks)
}