-
Notifications
You must be signed in to change notification settings - Fork 9
/
pending.go
109 lines (95 loc) · 2.38 KB
/
pending.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
package routetab
import (
"sync"
"time"
"github.com/gauss-project/aurorafs/pkg/boson"
)
var (
PendingTimeout = time.Second * 5
pendingInterval = time.Millisecond * 500
)
type PendCallResItem struct {
Src boson.Address
CreateTime time.Time
ResCh chan struct{}
}
type PendingCallResArray []*PendCallResItem
type pendCallResTab struct {
items sync.Map
reqLog sync.Map
}
func newPendCallResTab() *pendCallResTab {
return &pendCallResTab{
items: sync.Map{},
reqLog: sync.Map{},
}
}
func (pend *pendCallResTab) Delete(target, next boson.Address) {
key := target.ByteString()
pend.items.Delete(key)
pend.reqLog.Delete(key + next.ByteString())
}
func (pend *pendCallResTab) Add(target, src, next boson.Address, resCh chan struct{}) (has bool) {
pending := &PendCallResItem{
Src: src,
CreateTime: time.Now(),
ResCh: resCh,
}
key := target.ByteString()
res, ok := pend.items.Load(key)
if ok {
list := res.(PendingCallResArray)
list = append(list, pending)
pend.items.Store(key, list)
} else {
pend.items.Store(key, PendingCallResArray{pending})
}
// If a find route already exists, no forwarding is required
reqKey := key + next.ByteString()
_, has = pend.reqLog.Load(reqKey)
if !has {
pend.reqLog.Store(reqKey, time.Now())
}
return
}
func (pend *pendCallResTab) Get(target, next boson.Address) PendingCallResArray {
key := target.ByteString()
res, ok := pend.items.Load(key)
if ok {
pend.items.Delete(key)
pend.reqLog.Delete(key + next.ByteString())
return res.(PendingCallResArray)
}
return nil
}
func (pend *pendCallResTab) GcReqLog(expire time.Duration) {
pend.reqLog.Range(func(key, value interface{}) bool {
t := value.(time.Time)
if time.Since(t).Milliseconds() >= expire.Milliseconds() {
pend.reqLog.Delete(key)
}
return true
})
}
func (pend *pendCallResTab) GcResItems(expire time.Duration) {
pend.items.Range(func(key, value interface{}) bool {
item := value.(PendingCallResArray)
if time.Since(item[0].CreateTime).Milliseconds() < expire.Milliseconds() {
// If the first one doesn't expire, then the next ones don't expire
return true
}
expireK := 0
for k, v := range item {
if time.Since(v.CreateTime).Milliseconds() < expire.Milliseconds() {
expireK = k
break
}
}
if expireK == 0 {
pend.items.Delete(key)
} else {
pend.items.Store(key, item[expireK:])
}
return true
})
}