/
queue.go
203 lines (180 loc) · 4.35 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
// Package queue defines the wait queue interface.
package queue
import (
"encoding/json"
"errors"
"net/url"
"sync"
"time"
"github.com/fanyang01/crawler/urlx"
"golang.org/x/net/context"
)
// ErrPushClosed is a general queue error.
var ErrPushClosed = errors.New("queue: can not push on closed queue")
// Item is the item in wait queue.
type Item struct {
URL *url.URL
Next time.Time // next time to crawl
// optional for implementation
Score int `json:",omitempty"`
Ctx context.Context `json:"-"`
}
type jsonWrapper struct {
URL urlx.URL
Next time.Time
Score int `json:",omitempty"`
}
func (w jsonWrapper) To(item *Item) {
item.URL = w.URL.URL
item.Next = w.Next
item.Score = w.Score
}
func (w *jsonWrapper) From(item *Item) {
w.URL.URL = item.URL
w.Next = item.Next
w.Score = item.Score
}
func (item *Item) MarshalJSON() ([]byte, error) {
w := &jsonWrapper{}
w.From(item)
return json.Marshal(w)
}
func (item *Item) UnmarshalJSON(b []byte) error {
w := &jsonWrapper{}
if err := json.Unmarshal(b, w); err != nil {
return err
}
w.To(item)
return nil
}
var freelist = &sync.Pool{
New: func() interface{} { return new(Item) },
}
// NewItem allocates an Item object.
func NewItem() *Item {
item := freelist.Get().(*Item)
item.Ctx = context.TODO()
return item
}
// Free deallocates an Item object.
func (item *Item) Free() {
item.URL = nil
item.Next = time.Time{}
item.Score = 0
item.Ctx = nil
freelist.Put(item)
}
// WaitQueue should be a priority queue, using Item.Next and optional
// Item.Score as priority. It can be operated in two modes: channel mode
// and method mode, but only one mode will be used for a single instance.
type WaitQueue interface {
// Channel returns three channels expected to be used in select
// statement. It's implementation's responsibility to close the in
// channel and error channel when Close method is called. Callers
// should be responsible for closing the out channel.
// Channel should be goroutine-safe.
Channel() (in chan<- *Item, out <-chan *Item, err <-chan error)
// Push adds an item to queue.
Push(*Item) error
// Pop removes an item from queue.
Pop() (*Item, error)
// Close closes queue. Close will be called even an error has been
// returned in above operations.
Close() error
}
// Interface is a helper interface. WithChannel can generate a Channel
// method for implementations.
type Interface interface {
Push(*Item) error
Pop() (*Item, error)
Close() error
}
type channel struct {
Interface
quit chan struct{}
mu sync.Mutex
in chan *Item
out chan *Item
err chan error
}
// Channel implements WaitQueue.
func (q *channel) Channel() (in chan<- *Item, out <-chan *Item, err <-chan error) {
q.mu.Lock()
defer q.mu.Unlock()
if q.in != nil {
return q.in, q.out, q.err
}
q.in = make(chan *Item, 1024)
q.err = make(chan error, 1)
// Small output buffer size means that we pop an item only when it's requested.
q.out = make(chan *Item, 1)
go func() {
for item := range q.in {
if err := q.Push(item); err != nil {
q.sendErr(err)
return
}
}
}()
go func() {
for {
item, err := q.Pop()
if err != nil {
q.sendErr(err)
return
}
if item != nil {
q.out <- item
continue
}
return
}
}()
return q.in, q.out, q.err
}
func (q *channel) sendErr(err error) {
select {
case q.err <- err:
case <-q.quit:
}
}
// Channel implements WaitQueue.
func (q *channel) Close() error {
close(q.quit)
return q.Interface.Close()
}
// WithChannel provides a wrapper for those who don't implement a Channel
// method.
func WithChannel(q Interface) WaitQueue {
return &channel{
Interface: q,
quit: make(chan struct{}),
}
}
// Heap implements heap.Interface.
type Heap struct {
S []*Item
}
// Less compares compound priority of items.
func (q *Heap) Less(i, j int) bool {
if q.S[i].Next.Before(q.S[j].Next) {
return true
}
ti := q.S[i].Next.Round(time.Microsecond)
tj := q.S[j].Next.Round(time.Microsecond)
if ti.After(tj) {
return false
}
// ti = tj
return q.S[i].Score > q.S[j].Score
}
func (q *Heap) Top() *Item { return q.S[0] }
func (q *Heap) Len() int { return len(q.S) }
func (q *Heap) Swap(i, j int) { q.S[i], q.S[j] = q.S[j], q.S[i] }
func (q *Heap) Push(x interface{}) { q.S = append(q.S, x.(*Item)) }
func (q *Heap) Pop() interface{} {
n := len(q.S)
v := q.S[n-1]
q.S = q.S[0 : n-1]
return v
}