-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.go
230 lines (208 loc) · 5.39 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
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
package libtorrent
import (
"sort"
"time"
"github.com/anacrolix/torrent"
)
var ActiveCount = 3
var QueueTimeout = (30 * time.Minute).Nanoseconds()
var queue map[*torrent.Torrent]int64
type Int64Slice []int64
func (p Int64Slice) Len() int { return len(p) }
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Int64Slice) Sort() { sort.Sort(p) }
// priority start torrent. downloading torrent goes first, seeding second.
func queueStart(t *torrent.Torrent) bool {
delete(queue, t)
if len(active) < ActiveCount {
return startTorrent(t)
}
// build active torrent array with activate time
q := make(map[int64]*torrent.Torrent)
var l []int64
for _, m := range torrents {
if _, ok := active[m]; ok {
fs := filestorage[m.InfoHash()]
v := fs.ActivateDate
q[v] = m
l = append(l, v)
}
}
sort.Sort(Int64Slice(l)) // older torrent will be removed first
now := time.Now().UnixNano()
if !pendingCompleted(t) { // t is downloading
// try to find seeding torrent
for _, v := range l {
m := q[v]
// m is seeding?
if pendingCompleted(m) {
stopTorrent(m)
queue[m] = now
return startTorrent(t)
}
}
// ok all torrents are downloading, remove first downloading torrent
for _, v := range l {
m := q[v]
stopTorrent(m)
queue[m] = now
return startTorrent(t)
}
} else { // t is seeding
// try to find first seeding torrent to replace with
for _, v := range l {
m := q[v]
if pendingCompleted(m) { // seeding
stopTorrent(m)
queue[m] = now
return startTorrent(t)
}
}
}
// seems like t seeding, and have no slots, just queue and downloading.
// try to find first active torrent to replace with. if 't' seeding downloading will be resumed after 't' removed by timeout.
for _, v := range l {
m := q[v]
stopTorrent(m)
queue[m] = now
return startTorrent(t)
}
// len(l) can't be == 0 should never be here
stopTorrent(t)
queue[t] = now
return true
}
func queueEngine(t *torrent.Torrent) {
timeout := time.Duration(QueueTimeout) * time.Nanosecond
for {
b1 := t.BytesCompleted()
// in case if user set file to download on the same torrent, we need to receive Completed again.
torrentstorageLock.Lock()
ts := torrentstorage[t.InfoHash()]
ts.next.Clear()
torrentstorageLock.Unlock()
select {
case <-time.After(timeout):
case <-ts.next.LockedChan(&mu):
mu.Lock()
fs := filestorage[t.InfoHash()]
// we will be first who knows torrent is complete, and moved from active (downloading) state.
if fs.CompletedDate == 0 {
now := time.Now().UnixNano()
fs.CompletedDate = now
fs.DownloadingTime = fs.DownloadingTime + (now - fs.ActivateDate)
fs.ActivateDate = now // seeding time now
}
mu.Unlock()
case <-t.Wait():
return
}
timeout = time.Duration(QueueTimeout) * time.Nanosecond
mu.Lock()
if pendingCompleted(t) { // seeding
if queueNext(t) {
// we been removed, stop queue engine
mu.Unlock()
return
} else {
// we not been removed
if len(queue) != 0 {
// queue full, some one soon be available, check every minute
timeout = 1 * time.Minute
}
}
} else { // downloading
// check stalled, and rotate if it does
b2 := t.BytesCompleted()
if b1 == b2 {
if queueNext(t) {
// we been removed, stop queue engine
mu.Unlock()
return
} else {
// we not been removed
if len(queue) != 0 {
// queue full, some one soon be available, check every minute
timeout = 1 * time.Minute
}
}
}
}
mu.Unlock()
}
}
// 30 min seeding, download complete, 30 min stole torrent.
func queueNext(t *torrent.Torrent) bool {
now := time.Now().UnixNano()
// build active torrent array with activate time
q := make(map[int64]*torrent.Torrent)
var l []int64
for m, v := range queue {
// queue all || keep torrent resting for 30 mins
if len(active) < ActiveCount || v+QueueTimeout <= now {
q[v] = m
l = append(l, v)
}
}
sort.Sort(Int64Slice(l)) // older first
// check for downloading queue torrents
for _, v := range l {
m := q[v]
if !pendingCompleted(m) {
if startTorrent(m) {
delete(queue, m)
if t != nil {
stopTorrent(t)
queue[t] = now
}
return true
}
// unable to start torrent, here no place to report an error. keep looping.
}
}
// check for seeding queue
for _, v := range l {
m := q[v]
if pendingCompleted(m) {
if startTorrent(m) {
delete(queue, m)
if t != nil {
stopTorrent(t)
queue[t] = now
}
return true
}
// unable to start torrent, here no place to report an error. keep looping.
}
}
if t != nil {
// is 't' seeding torrent? if here any downloading, queue it, regardless on timeout
if pendingCompleted(t) {
// load all torrents, we will take most oldest added to the queue
q := make(map[int64]*torrent.Torrent)
var l []int64
// add all from queue
for m, v := range queue {
q[v] = m
l = append(l, v)
}
sort.Sort(Int64Slice(l)) // older first
for _, v := range l {
m := q[v]
// m - downloading in queue?
if !pendingCompleted(m) {
if startTorrent(m) {
delete(queue, m)
stopTorrent(t)
queue[t] = now
return true
}
// unable to start torrent, here no place to report an error. keep looping.
}
}
}
}
// queue is empty, change nothing
return false
}