-
Notifications
You must be signed in to change notification settings - Fork 13
/
expiry.go
78 lines (71 loc) · 1.84 KB
/
expiry.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
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package asset
import (
"container/list"
"github.com/bitmark-inc/bitmarkd/constants"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"time"
)
// to control expiry
type expiry struct {
assetIndex transactionrecord.AssetIndex // item to remove
expires time.Time // remove the record after this time
}
// expiry loop
func (state *expiryData) Run(args interface{}, shutdown <-chan struct{}) {
log := state.log
l := list.New()
delay := time.After(time.Minute)
loop:
for {
log.Info("waiting…")
select {
case <-shutdown:
break loop
case assetIndex := <-state.queue:
log.Infof("received: asset index: %s", assetIndex)
l.PushBack(expiry{
assetIndex: assetIndex,
expires: time.Now().Add(constants.AssetTimeout),
})
case <-delay:
inner_loop:
for {
e := l.Front()
if nil == e {
delay = time.After(time.Minute)
break inner_loop
}
item := e.Value.(expiry)
d := time.Since(item.expires)
if d < 0 {
delay = time.After(-d)
break inner_loop
}
l.Remove(e)
globalData.Lock()
cache, ok := globalData.cache[item.assetIndex]
if ok {
switch cache.state {
case pendingState:
cache.state = expiringState
item.expires = time.Now().Add(constants.AssetTimeout)
l.PushBack(item)
case expiringState:
log.Infof("expired: asset index: %s", item.assetIndex)
delete(globalData.cache, item.assetIndex)
case verifiedState:
// the item just dropped from expiry queue
// but still exists in the map
default:
log.Criticalf("expired: invalid cache state: %d for: %s", cache.state, item.assetIndex)
}
}
globalData.Unlock()
}
}
}
log.Info("finished")
}