/
caches.go
133 lines (118 loc) · 2.97 KB
/
caches.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
package channels
import (
"sync"
"sync/atomic"
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
)
type readIndexFn func(datatransfer.ChannelID) (int64, error)
type cacheKey struct {
evt datatransfer.EventCode
chid datatransfer.ChannelID
}
type blockIndexCache struct {
lk sync.RWMutex
values map[cacheKey]*int64
}
func newBlockIndexCache() *blockIndexCache {
return &blockIndexCache{
values: make(map[cacheKey]*int64),
}
}
func (bic *blockIndexCache) getValue(evt datatransfer.EventCode, chid datatransfer.ChannelID, readFromOriginal readIndexFn) (*int64, error) {
idxKey := cacheKey{evt, chid}
bic.lk.RLock()
value := bic.values[idxKey]
bic.lk.RUnlock()
if value != nil {
return value, nil
}
bic.lk.Lock()
defer bic.lk.Unlock()
value = bic.values[idxKey]
if value != nil {
return value, nil
}
newValue, err := readFromOriginal(chid)
if err != nil {
return nil, err
}
bic.values[idxKey] = &newValue
return &newValue, nil
}
func (bic *blockIndexCache) updateIfGreater(evt datatransfer.EventCode, chid datatransfer.ChannelID, newIndex int64, readFromOriginal readIndexFn) (bool, error) {
value, err := bic.getValue(evt, chid, readFromOriginal)
if err != nil {
return false, err
}
for {
currentIndex := atomic.LoadInt64(value)
if newIndex <= currentIndex {
return false, nil
}
if atomic.CompareAndSwapInt64(value, currentIndex, newIndex) {
return true, nil
}
}
}
type progressState struct {
dataLimit uint64
progress *uint64
}
type readProgressFn func(datatransfer.ChannelID) (dataLimit uint64, progress uint64, err error)
type progressCache struct {
lk sync.RWMutex
values map[datatransfer.ChannelID]progressState
}
func newProgressCache() *progressCache {
return &progressCache{
values: make(map[datatransfer.ChannelID]progressState),
}
}
func (pc *progressCache) getValue(chid datatransfer.ChannelID, readProgress readProgressFn) (progressState, error) {
pc.lk.RLock()
value, ok := pc.values[chid]
pc.lk.RUnlock()
if ok {
return value, nil
}
pc.lk.Lock()
defer pc.lk.Unlock()
value, ok = pc.values[chid]
if ok {
return value, nil
}
dataLimit, progress, err := readProgress(chid)
if err != nil {
return progressState{}, err
}
newValue := progressState{
dataLimit: dataLimit,
progress: &progress,
}
pc.values[chid] = newValue
return newValue, nil
}
func (pc *progressCache) progress(chid datatransfer.ChannelID, additionalData uint64, readFromOriginal readProgressFn) (bool, error) {
state, err := pc.getValue(chid, readFromOriginal)
if err != nil {
return false, err
}
total := atomic.AddUint64(state.progress, additionalData)
return state.dataLimit != 0 && total >= state.dataLimit, nil
}
func (pc *progressCache) setDataLimit(chid datatransfer.ChannelID, newLimit uint64) {
pc.lk.RLock()
value, ok := pc.values[chid]
pc.lk.RUnlock()
if !ok {
return
}
pc.lk.Lock()
defer pc.lk.Unlock()
value, ok = pc.values[chid]
if !ok {
return
}
value.dataLimit = newLimit
pc.values[chid] = value
}