-
Notifications
You must be signed in to change notification settings - Fork 34
/
goid.go
53 lines (40 loc) · 865 Bytes
/
goid.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
package goid
import (
"sync"
"github.com/machinefi/w3bstream/pkg/depends/kit/metax"
)
var Default = &Meta{}
type Meta struct{ m sync.Map }
func (m *Meta) Clear() {
// m.m.Delete(runtime.GoID())
}
func (m *Meta) Get() metax.Meta {
// if logID, ok := m.m.Load(runtime.GoID()); ok {
// return logID.(metax.Meta)
// }
return metax.Meta{}
}
func (m *Meta) Set(meta metax.Meta) {
// m.m.Store(runtime.GoID(), meta)
}
func (m *Meta) With(cb func(), metas ...metax.Meta) func() {
meta := metax.Meta{}
if len(metas) == 0 {
meta = m.Get()
} else {
meta = meta.Merge(metas...)
}
return func() {
m.Set(meta)
defer m.Clear()
cb()
}
}
func (m *Meta) All() map[int64]metax.Meta {
results := map[int64]metax.Meta{}
m.m.Range(func(key, value interface{}) bool {
results[key.(int64)] = value.(metax.Meta)
return true
})
return results
}