-
Notifications
You must be signed in to change notification settings - Fork 286
/
storage.go
232 lines (216 loc) · 3.97 KB
/
storage.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package shuttle
import (
"github.com/sipt/shuttle/conn"
"github.com/sipt/shuttle/log"
"github.com/sipt/shuttle/proxy"
"github.com/sipt/shuttle/rule"
"sync"
"sync/atomic"
"time"
)
const (
RecordStatus = 1
RecordUp = 2
RecordDown = 3
RecordAppend = 4
RecordRemove = 5
RecordStatusActive = "Active"
RecordStatusCompleted = "Completed"
RecordStatusReject = "Reject"
RecordStatusFailed = "Failed"
)
type Pusher func(interface{})
var maxCount = 500
var boxChan chan *Box
var storage *LinkedList
var speed *Speed
var pusher Pusher = func(v interface{}) {} // init empty pusher
func init() {
boxChan = make(chan *Box, 64)
storage = &LinkedList{}
speed = &Speed{
Cancel: make(chan bool, 1),
}
speed.Start()
go func() {
var box *Box
for {
box = <-boxChan
switch box.Op {
case RecordAppend:
storage.Append(box.Value.(*Record))
default:
storage.Put(box.ID, box.Op, box.Value)
}
go func(box *Box) {
pusher(box)
}(box)
}
}()
//init traffic
conn.InitTrafficChannel(func(recordID int64, n int) {
boxChan <- &Box{recordID, RecordUp, n}
}, func(recordID int64, n int) {
boxChan <- &Box{recordID, RecordDown, n}
})
}
//注册推送
func RegisterPusher(p Pusher) {
pusher = p
}
func GetRecords() []Record {
return storage.List()
}
func ClearRecords() {
storage.Clear()
}
func GetRecord(id int64) *Record {
return storage.Get(id)
}
func CurrentSpeed() (int, int) {
return speed.UpSpeed, speed.DownSpeed
}
type Speed struct {
UpSpeed int
DownSpeed int
UpBytes int
DownBytes int
Cancel chan bool
status int32
}
func (s *Speed) Start() {
if atomic.CompareAndSwapInt32(&s.status, 0, 1) {
go func() {
t := time.NewTicker(time.Second)
for {
select {
case <-t.C:
s.UpSpeed, s.UpBytes = s.UpBytes, 0
s.DownSpeed, s.DownBytes = s.DownBytes, 0
case <-s.Cancel:
return
}
}
}()
}
}
type Box struct {
ID int64
Op int
Value interface{}
}
type Record struct {
ID int64
Protocol string
Created time.Time
Proxy *proxy.Server
Rule *rule.Rule
Status string
Up int
Down int
URL string
Dumped bool
}
type LinkedList struct {
sync.RWMutex
head, tail *node
count int
}
type node struct {
record *Record
next *node
sync.RWMutex
}
func (l *LinkedList) Get(id int64) *Record {
l.RLock()
index := l.head
for index != nil {
if index.record.ID == id {
l.RUnlock()
return index.record
}
index = index.next
}
l.RUnlock()
return nil
}
func (l *LinkedList) List() []Record {
if l.count == 0 {
return []Record{}
}
l.Lock()
list := make([]Record, l.count)
index := l.head
for i := range list {
list[i] = *index.record
index = index.next
}
l.Unlock()
return list
}
func (l *LinkedList) Append(r *Record) {
l.Lock()
if r.Proxy == nil || r.Rule == nil {
log.Logger.Infof("[Storage] ID:[%d] Policy:[nil] URL:[%s]", r.ID, r.URL)
} else {
log.Logger.Infof("[Storage] ID:[%d] Policy:[%s(%s,%s)] URL:[%s]", r.ID, r.Proxy.Name, r.Rule.Type, r.Rule.Value, r.URL)
}
if l.head == nil {
l.head = &node{record: r}
l.tail = l.head
} else {
l.tail.next = &node{record: r}
l.tail = l.tail.next
}
l.count ++
for l.count > maxCount {
go func(id int64) {
pusher(&Box{
Op: RecordRemove,
Value: id,
})
}(l.head.record.ID)
// 收缩
l.head.next, l.head = nil, l.head.next
l.count --
}
l.Unlock()
}
func (l *LinkedList) Put(id int64, op int, v interface{}) {
l.RLock()
index := l.head
for index != nil {
if index.record.ID == id {
index.Put(op, v)
break
}
index = index.next
}
l.RUnlock()
}
func (l *LinkedList) Clear() {
l.Lock()
l.head = nil
l.count = 0
l.Unlock()
}
func (n *node) Put(op int, v interface{}) {
n.Lock()
switch op {
case RecordStatus:
n.record.Status = v.(string)
case RecordUp:
s := v.(int)
n.record.Up += s
if speed != nil {
speed.UpBytes += s
}
case RecordDown:
s := v.(int)
n.record.Down += s
if speed != nil {
speed.DownBytes += s
}
}
n.Unlock()
}