-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.go
126 lines (107 loc) · 2.8 KB
/
worker.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
package main
import (
"github.com/Sirupsen/logrus"
"net/http"
//"time"
)
type WorkRequest struct {
Request *http.Request
//Type string (mkdir, create)
//Name string (dir, file)
}
type Worker struct {
ID int
Work chan WorkRequest
WorkerQueue chan chan WorkRequest
QuitChan chan bool
}
// A buffered channel that we can send work requests on. 100?
var WorkQueue = make(chan WorkRequest, 10000)
func Collector(req *http.Request) error {
// Now, we take the http request and make a WorkRequest out of them.
logrus.Debug("Collector Started", req.URL)
work := WorkRequest{Request: req}
// Push the work onto the queue.
WorkQueue <- work
logrus.Debug("Work request queued")
return nil
}
// NewWorker creates, and returns a new Worker object. Its only argument
// is a channel that the worker can add itself to whenever it is done its
// work.
func NewWorker(id int, workerQueue chan chan WorkRequest) Worker {
// Create, and return the worker.
worker := Worker{
ID: id,
Work: make(chan WorkRequest),
WorkerQueue: workerQueue,
QuitChan: make(chan bool)}
return worker
}
// This function "starts" the worker by starting a goroutine, that is
// an infinite "for-select" loop.
func (w *Worker) Start() {
go func() {
for {
// Add ourselves into the worker queue.
w.WorkerQueue <- w.Work
select {
case work := <-w.Work:
// Receive a work request.
logrus.Debug("Worker received ", w.ID)
client := &http.Client{}
resp, err := client.Do(work.Request)
if err != nil {
logrus.Error("Error Client.do", err)
} else {
defer resp.Body.Close()
}
// Handle errors
/*if resp.StatusCode == 401 {
return errors.New(ERROR_NOT_AUTHORIZED)
}
if err != nil {
logrus.Errorf("CreateDir %v", err)
return err
}*/
logrus.Debug("Worker Hello ", w.ID)
case <-w.QuitChan:
// We have been asked to stop.
logrus.Debug("Worker stoppping ", w.ID)
return
}
}
}()
}
// Stop tells the worker to stop listening for work requests.
//
// Note that the worker will only stop *after* it has finished its work.
func (w *Worker) Stop() {
go func() {
w.QuitChan <- true
}()
}
var WorkerQueue chan chan WorkRequest
func StartDispatcher(nworkers int) {
// First, initialize the channel we are going to but the workers' work channels into.
WorkerQueue = make(chan chan WorkRequest, nworkers)
// Now, create all of our workers.
for i := 0; i < nworkers; i++ {
logrus.Debug("Starting worker ", i+1)
worker := NewWorker(i+1, WorkerQueue)
worker.Start()
}
go func() {
for {
select {
case work := <-WorkQueue:
logrus.Debug("Received work request")
go func() {
worker := <-WorkerQueue
logrus.Debug("Dispatching work request")
worker <- work
}()
}
}
}()
}