-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.go
200 lines (182 loc) · 3.56 KB
/
queue.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
package chunkinfo
import (
"bytes"
"context"
"github.com/FavorLabs/favorX/pkg/boson"
"sync"
)
type Pull = uint32
const (
UnPull Pull = iota
Pulling
Pulled
)
const (
PullMax = 200
PullingMax = 10
PullerMax = 1000
)
// queue
type queue struct {
sync.RWMutex
UnPull []*[]byte
Pulling []*[]byte
Pulled []*[]byte
}
// newQueue
func (ci *ChunkInfo) newQueue(rootCid string) {
q := &queue{
UnPull: make([]*[]byte, 0, PullerMax),
Pulling: make([]*[]byte, 0, PullingMax),
Pulled: make([]*[]byte, 0, PullMax),
}
ci.queues.Store(rootCid, q)
}
// len
func (q *queue) len(pull Pull) int {
q.RLock()
defer q.RUnlock()
qu := q.getPull(pull)
return len(qu)
}
// peek
//func (q *queue) peek(pull Pull) *[]byte {
// q.RLock()
// defer q.RLock()
// qu := q.getPull(pull)
// return qu[0]
//}
// pop
func (q *queue) pop(pull Pull) *[]byte {
q.Lock()
defer q.Unlock()
qu := q.getPull(pull)
v := qu[0]
qu = qu[1:]
q.updatePull(pull, qu)
return v
}
// popNode
func (q *queue) popNode(pull Pull, overlay []byte) {
q.Lock()
defer q.Unlock()
qu := q.getPull(pull)
for i := 0; i < len(qu); i++ {
f := *qu[i]
if bytes.Equal(f, overlay) {
qu = append(qu[:i], qu[i+1:]...)
}
}
q.updatePull(pull, qu)
}
// push
func (q *queue) push(pull Pull, overlay []byte) {
q.Lock()
defer q.Unlock()
qu := q.getPull(pull)
qu = append(qu, &overlay)
q.updatePull(pull, qu)
}
// getPull
func (q *queue) getPull(pull Pull) []*[]byte {
switch pull {
case UnPull:
return q.UnPull
case Pulling:
return q.Pulling
case Pulled:
return q.Pulled
}
return make([]*[]byte, 0)
}
// updatePull
func (q *queue) updatePull(pull Pull, queue []*[]byte) {
switch pull {
case UnPull:
q.UnPull = queue
case Pulling:
q.Pulling = queue
case Pulled:
q.Pulled = queue
}
}
// getQueue
func (ci *ChunkInfo) getQueue(rootCid string) *queue {
var que *queue
if q, ok := ci.queues.Load(rootCid); ok {
que = q.(*queue)
}
return que
}
// isExists
func (q *queue) isExists(pull Pull, overlay []byte) bool {
q.RLock()
defer q.RUnlock()
pullNodes := q.getPull(pull)
for _, pn := range pullNodes {
f := *pn
if bytes.Equal(f, overlay) {
return true
}
}
return false
}
// queueProcess
func (ci *ChunkInfo) queueProcess(ctx context.Context, rootCid boson.Address) {
ci.queuesLk.Lock()
defer ci.queuesLk.Unlock()
if !ci.getPendingFinder(rootCid) {
return
}
q := ci.getQueue(rootCid.String())
if q == nil {
return
}
// pulled + pulling >= pullMax
if q.len(Pulled)+q.len(Pulling) >= PullMax {
return
}
pullingLen := q.len(Pulling)
if pullingLen >= PullingMax {
return
}
n := PullingMax - pullingLen
if n > q.len(UnPull) {
n = q.len(UnPull)
}
for i := 0; i < n; i++ {
unNode := q.pop(UnPull)
q.push(Pulling, *unNode)
overlay := boson.NewAddress(*unNode)
ci.timeoutTrigger.updateTimeOutTrigger(rootCid.Bytes(), *unNode)
ciReq := ci.createChunkInfoReq(rootCid, overlay, ci.addr)
go func() {
err := ci.sendDatas(ctx, overlay, streamChunkInfoReqName, ciReq)
if err != nil {
ci.logger.Errorf("[chunk info] send error :%v", err)
}
}()
}
}
func (ci *ChunkInfo) updateQueue(rootCid, overlay boson.Address, chunkInfo map[string][]byte) {
q := ci.getQueue(rootCid.String())
if q == nil {
return
}
for over := range chunkInfo {
o := boson.MustParseHexAddress(over)
n := o.Bytes()
if o.Equal(ci.addr) {
continue
}
if q.len(UnPull) >= PullerMax {
break
}
if q.isExists(Pulled, n) || q.isExists(Pulling, n) || q.isExists(UnPull, n) {
continue
}
q.push(UnPull, n)
}
q.popNode(Pulling, overlay.Bytes())
q.push(Pulled, overlay.Bytes())
}