-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtaskqueue.go
More file actions
151 lines (135 loc) · 3.89 KB
/
taskqueue.go
File metadata and controls
151 lines (135 loc) · 3.89 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
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
package taskqueue
import (
"context"
"sync"
"time"
"github.com/ipfs/go-peertaskqueue"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipfs/go-peertaskqueue/peertracker"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/ipfs/go-graphsync"
)
const thawSpeed = time.Millisecond * 100
// Executor runs a single task on the queue
type Executor interface {
ExecuteTask(ctx context.Context, pid peer.ID, task *peertask.Task) bool
}
type TaskQueue interface {
PushTask(p peer.ID, task peertask.Task)
TaskDone(p peer.ID, task *peertask.Task)
Remove(t peertask.Topic, p peer.ID)
Stats() graphsync.RequestStats
WithPeerTopics(p peer.ID, f func(*peertracker.PeerTrackerTopics))
}
// WorkerTaskQueue is a wrapper around peertaskqueue.PeerTaskQueue that manages running workers
// that pop tasks and execute them
type WorkerTaskQueue struct {
lockTopics sync.Mutex
*peertaskqueue.PeerTaskQueue
ctx context.Context
cancelFn func()
workSignal chan struct{}
noTaskCond *sync.Cond
ticker *time.Ticker
activeTasks int32
}
// NewTaskQueue initializes a new queue
func NewTaskQueue(ctx context.Context, ptqopts ...peertaskqueue.Option) *WorkerTaskQueue {
ctx, cancelFn := context.WithCancel(ctx)
return &WorkerTaskQueue{
ctx: ctx,
cancelFn: cancelFn,
PeerTaskQueue: peertaskqueue.New(ptqopts...),
workSignal: make(chan struct{}, 1),
noTaskCond: sync.NewCond(&sync.Mutex{}),
ticker: time.NewTicker(thawSpeed),
}
}
// PushTask pushes a new task on to the queue
func (tq *WorkerTaskQueue) PushTask(p peer.ID, task peertask.Task) {
tq.lockTopics.Lock()
tq.PeerTaskQueue.PushTasks(p, task)
tq.lockTopics.Unlock()
select {
case tq.workSignal <- struct{}{}:
default:
}
}
// TaskDone marks a task as completed so further tasks can be executed
func (tq *WorkerTaskQueue) TaskDone(p peer.ID, task *peertask.Task) {
tq.lockTopics.Lock()
tq.PeerTaskQueue.TasksDone(p, task)
tq.lockTopics.Unlock()
}
// Stats returns statistics about a task queue
func (tq *WorkerTaskQueue) Stats() graphsync.RequestStats {
tq.lockTopics.Lock()
ptqstats := tq.PeerTaskQueue.Stats()
tq.lockTopics.Unlock()
return graphsync.RequestStats{
TotalPeers: uint64(ptqstats.NumPeers),
Active: uint64(ptqstats.NumActive),
Pending: uint64(ptqstats.NumPending),
}
}
func (tq *WorkerTaskQueue) WithPeerTopics(p peer.ID, withPeerTopics func(*peertracker.PeerTrackerTopics)) {
tq.lockTopics.Lock()
peerTopics := tq.PeerTaskQueue.PeerTopics(p)
withPeerTopics(peerTopics)
tq.lockTopics.Unlock()
}
// Startup runs the given number of task workers with the given executor
func (tq *WorkerTaskQueue) Startup(workerCount uint64, executor Executor) {
for range workerCount {
go tq.worker(executor)
}
}
// Shutdown shuts down all running workers
func (tq *WorkerTaskQueue) Shutdown() {
tq.cancelFn()
}
func (tq *WorkerTaskQueue) WaitForNoActiveTasks() {
tq.noTaskCond.L.Lock()
for tq.activeTasks > 0 {
tq.noTaskCond.Wait()
}
tq.noTaskCond.L.Unlock()
}
func (tq *WorkerTaskQueue) worker(executor Executor) {
targetWork := 1
for {
tq.lockTopics.Lock()
pid, tasks, _ := tq.PeerTaskQueue.PopTasks(targetWork)
tq.lockTopics.Unlock()
for len(tasks) == 0 {
select {
case <-tq.ctx.Done():
return
case <-tq.workSignal:
tq.lockTopics.Lock()
pid, tasks, _ = tq.PeerTaskQueue.PopTasks(targetWork)
tq.lockTopics.Unlock()
case <-tq.ticker.C:
tq.lockTopics.Lock()
tq.PeerTaskQueue.ThawRound()
pid, tasks, _ = tq.PeerTaskQueue.PopTasks(targetWork)
tq.lockTopics.Unlock()
}
}
for _, task := range tasks {
tq.noTaskCond.L.Lock()
tq.activeTasks = tq.activeTasks + 1
tq.noTaskCond.L.Unlock()
terminate := executor.ExecuteTask(tq.ctx, pid, task)
tq.noTaskCond.L.Lock()
tq.activeTasks = tq.activeTasks - 1
if tq.activeTasks == 0 {
tq.noTaskCond.Broadcast()
}
tq.noTaskCond.L.Unlock()
if terminate {
return
}
}
}
}