-
Notifications
You must be signed in to change notification settings - Fork 3
/
accord.go
54 lines (47 loc) · 1.01 KB
/
accord.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
package accord
import (
"errors"
"sync"
)
var (
// ErrAcquired error is returned if the resource is already acquired.
ErrAcquired = errors.New("accord: acquired")
// ErrDone error is returned if the resource is already marked done.
ErrDone = errors.New("accord: done")
// ErrClosed error is returned by the handle if closed.
ErrClosed = errors.New("accord: closed")
)
type metadata struct {
kv map[string]string
mu sync.RWMutex
}
// Snap returns a snapshot of metadata.
func (m *metadata) Snap() map[string]string {
m.mu.RLock()
defer m.mu.RUnlock()
snap := make(map[string]string, len(m.kv))
for k, v := range m.kv {
snap[k] = v
}
return snap
}
// Set sets key.
func (m *metadata) Set(k, v string) {
m.mu.Lock()
if m.kv == nil {
m.kv = make(map[string]string)
}
m.kv[k] = v
m.mu.Unlock()
}
// Merge updates metadata from kv.
func (m *metadata) Update(kv map[string]string) {
m.mu.Lock()
if m.kv == nil {
m.kv = make(map[string]string)
}
for k, v := range kv {
m.kv[k] = v
}
m.mu.Unlock()
}