+
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Queue struct {
Threads int
storage Storage
wake chan struct{}
mut sync.Mutex // guards wake
mut sync.Mutex // guards wake and running
running bool
}

// InMemoryQueueStorage is the default implementation of the Storage interface.
Expand Down Expand Up @@ -62,6 +63,7 @@ func New(threads int, s Storage) (*Queue, error) {
return &Queue{
Threads: threads,
storage: s,
running: true,
}, nil
}

Expand Down Expand Up @@ -122,11 +124,12 @@ func (q *Queue) Size() (int, error) {
// The given Storage must not be used directly while Run blocks.
func (q *Queue) Run(c *colly.Collector) error {
q.mut.Lock()
if q.wake != nil {
if q.wake != nil && q.running == true {
q.mut.Unlock()
panic("cannot call duplicate Queue.Run")
}
q.wake = make(chan struct{})
q.running = true
q.mut.Unlock()

requestc := make(chan *colly.Request)
Expand All @@ -139,6 +142,13 @@ func (q *Queue) Run(c *colly.Collector) error {
return <-errc
}

// Stop will stop the running queue
func (q *Queue) Stop() {
q.mut.Lock()
q.running = false
q.mut.Unlock()
}

func (q *Queue) loop(c *colly.Collector, requestc chan<- *colly.Request, complete <-chan struct{}, errc chan<- error) {
var active int
for {
Expand All @@ -147,7 +157,7 @@ func (q *Queue) loop(c *colly.Collector, requestc chan<- *colly.Request, complet
errc <- err
break
}
if size == 0 && active == 0 {
if size == 0 && active == 0 || !q.running {
// Terminate when
// 1. No active requests
// 2. Emtpy queue
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载