-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchronos.go
256 lines (220 loc) · 5.82 KB
/
chronos.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// MIT License
// Copyright (c) 2020 tanzy2018
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package chronos
import (
"bytes"
"encoding/json"
"errors"
"io"
"sort"
"strings"
"sync/atomic"
"time"
)
// Chronos ... chronos,a test time comsume interval.
type Chronos interface {
// WriteTo ..
io.WriterTo
// Reset .. reset the chronos timer.
Reset()
// Add .. add a new time.Time with key,if existed then replace.
Add(key string) error
// Link .. link key with the histrory key.
Link(key string, fromKey string) error
// Consume .. the time interval between key and its link or the the init key.
Consume(key string) (time.Duration, bool)
// ConsumeFrom .. the time interval between key and specified key.
ConsumeFrom(key string, fromKey string) (time.Duration, bool)
}
// Ichronos ...
type Ichronos struct {
initKey string
c int32
m map[string]*timer
}
type timer struct {
key string
linkKey string
tm time.Time
ic *Ichronos
}
var globalChronos = New()
// errors
var (
ErrKeyDuplicated = errors.New("key is existed,duplicated")
ErrKeyNotfound = errors.New("key not found")
ErrDisorderedKey = errors.New("key and fromKey is disordered")
)
var zeroDuration = time.Duration(0)
var initKey = "chronos-init"
// New ... generate a new chronos.
func New() Chronos {
c := &Ichronos{
initKey: wrapInitKey(),
m: make(map[string]*timer),
}
atomic.StoreInt32(&c.c, 0)
c.m[c.initKey] = &timer{
key: c.initKey,
linkKey: c.initKey,
tm: time.Now(),
ic: c,
}
return c
}
// WriteTo ..
func (ic *Ichronos) WriteTo(w io.Writer) (n int64, err error) {
ic.lock()
tmStrings := make([]string, 0, len(ic.m))
for _, tm := range ic.m {
tmStrings = append(tmStrings, strings.Trim(tm.String(), "\n"))
}
ic.unlock()
sort.Strings(tmStrings)
var buf bytes.Buffer
for _, str := range tmStrings {
buf.WriteString(str)
buf.WriteByte('\n')
}
return buf.WriteTo(w)
}
// Reset .. reset the chronos timer.
func (ic *Ichronos) Reset() {
ic.lock()
defer ic.unlock()
ic.initKey = wrapInitKey()
ic.m = make(map[string]*timer)
ic.m[ic.initKey] = &timer{
key: ic.initKey,
linkKey: ic.initKey,
tm: time.Now(),
ic: ic,
}
}
// Add .. add a new time.Time with key,if existed then replace.
func (ic *Ichronos) Add(key string) error {
tm := time.Now()
ic.lock()
defer ic.unlock()
if _, ok := ic.m[key]; ok {
return ErrKeyDuplicated
}
ic.m[key] = &timer{
key: key,
linkKey: ic.initKey,
tm: tm,
ic: ic,
}
return nil
}
// Link .. link key with the histrory key.
func (ic *Ichronos) Link(key string, fromKey string) error {
ic.lock()
defer ic.unlock()
var tm0, tm1 *timer
var ok bool
if tm0, ok = ic.m[key]; !ok {
return ErrKeyNotfound
}
if tm1, ok = ic.m[fromKey]; !ok {
return ErrKeyNotfound
}
if tm0.tm.Before(tm1.tm) {
return ErrDisorderedKey
}
tm0.linkKey = fromKey
ic.m[key] = tm0
return nil
}
// Consume .. the time interval between key and its link or the the init key.
func (ic *Ichronos) Consume(key string) (time.Duration, bool) {
ic.lock()
defer ic.unlock()
tm, ok := ic.m[key]
if !ok {
return zeroDuration, false
}
return tm.tm.Sub(ic.m[tm.linkKey].tm), true
}
// ConsumeFrom .. the time interval between key and specified key.
func (ic *Ichronos) ConsumeFrom(key string, fromkey string) (time.Duration, bool) {
ic.lock()
defer ic.unlock()
tm0, ok := ic.m[key]
if !ok {
return zeroDuration, false
}
tm1, ok := ic.m[fromkey]
if !ok {
return zeroDuration, false
}
if tm0.tm.Before(tm1.tm) {
return zeroDuration, false
}
return tm0.tm.Sub(tm1.tm), true
}
func (ic *Ichronos) lock() {
for {
if atomic.CompareAndSwapInt32(&ic.c, 0, 1) {
return
}
}
}
func (ic *Ichronos) unlock() {
for {
if atomic.CompareAndSwapInt32(&ic.c, 1, 0) {
return
}
}
}
func wrapInitKey() string {
return initKey + "-" + RandomString(4)
}
// WriteTo ..
func WriteTo(w io.Writer) (int64, error) {
return globalChronos.WriteTo(w)
}
// Reset .. reset the chronos timer.
func Reset() {
globalChronos.Reset()
}
// Add .. add a new time.Time with key,if existed then replace.
func Add(key string) error {
return globalChronos.Add(key)
}
// Link .. link key with the histrory key.
func Link(key string, fromKey string) error {
return globalChronos.Link(key, fromKey)
}
// Consume .. the time interval between key and its link or the the init key.
func Consume(key string) (time.Duration, bool) {
return globalChronos.Consume(key)
}
// ConsumeFrom .. the time interval between key and specified key.
func ConsumeFrom(key string, fromKey string) (time.Duration, bool) {
return globalChronos.ConsumeFrom(key, fromKey)
}
func (t *timer) String() string {
m := make(map[string]interface{})
m["from"] = t.linkKey
m["to"] = t.key
m["consume"] = t.tm.Sub(t.ic.m[t.linkKey].tm)
m["unit"] = "ns"
out, _ := json.Marshal(m)
return ToString(out)
}