-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathworker.go
More file actions
68 lines (61 loc) · 1.19 KB
/
Copy pathworker.go
File metadata and controls
68 lines (61 loc) · 1.19 KB
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
package res
import nats "github.com/nats-io/nats.go"
type requestWork struct {
msg *nats.Msg
rtype string
rname string
method string
mh *Match
}
type workItem struct {
cb func()
request requestWork
}
type work struct {
s *Service
wid string // Worker ID for the work queue
single [1]workItem
queue []workItem // Work queue
}
// startWorker starts a new resource worker that will listen for resources to
// process requests on.
func (s *Service) startWorker() {
s.mu.Lock()
defer s.mu.Unlock()
defer s.wg.Done()
// workqueue being nil signals we the service is closing
for s.workqueue != nil {
for len(s.workqueue) == 0 {
s.workcond.Wait()
if s.workqueue == nil {
return
}
}
w := s.workqueue[0]
if len(s.workqueue) == 1 {
s.workqueue = s.workbuf[:0]
} else {
s.workqueue = s.workqueue[1:]
}
w.processQueue()
}
}
func (w *work) processQueue() {
var wi workItem
idx := 0
for len(w.queue) > idx {
wi = w.queue[idx]
w.s.mu.Unlock()
idx++
if wi.cb != nil {
wi.cb()
} else {
w.s.processRequest(wi.request)
}
w.s.mu.Lock()
}
// Work complete. Delete if it has a work ID.
if w.wid != "" {
delete(w.s.rwork, w.wid)
}
}