-
Notifications
You must be signed in to change notification settings - Fork 14
/
dispatchers.go
110 lines (93 loc) · 3.43 KB
/
dispatchers.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
package activity
import (
"fmt"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/service/swf"
"github.com/sclasen/swfsm/poller"
)
//ActivityTaskDispatcher is used by the ActivityWorker machinery to dispatch the handling of ActivityTasks.
//Different implementations can provide different concurrency models.
type ActivityTaskDispatcher interface {
DispatchTask(*swf.PollForActivityTaskOutput, func(*swf.PollForActivityTaskOutput))
}
//CallingGoroutineDispatcher is a DecisionTaskDispatcher that runs the decision handler in the polling goroutine
type CallingGoroutineDispatcher struct{}
//DispatchTask calls the handler in the same goroutine.
func (*CallingGoroutineDispatcher) DispatchTask(task *swf.PollForActivityTaskOutput, handler func(*swf.PollForActivityTaskOutput)) {
handler(task)
}
//NewGoroutineDispatcher is a DecisionTaskDispatcher that runs the decision handler in a new goroutine.
type NewGoroutineDispatcher struct {
}
//DispatchTask calls the handler in a new goroutine.
func (*NewGoroutineDispatcher) DispatchTask(task *swf.PollForActivityTaskOutput, handler func(*swf.PollForActivityTaskOutput)) {
go handler(task)
}
//BoundedGoroutineDispatcher is a DecisionTaskDispatcher that uses a bounded number of goroutines to run decision handlers.
type BoundedGoroutineDispatcher struct {
NumGoroutines int
started bool
tasks chan *swf.PollForActivityTaskOutput
}
//DispatchTask calls sends the task on a channel that NumGoroutines goroutines are selecting on.
//Goroutines recieving a task run it in the same goroutine.
//note that this is unsynchronized as DispatchTask will only be called by the single poller goroutine.
func (b *BoundedGoroutineDispatcher) DispatchTask(task *swf.PollForActivityTaskOutput, handler func(*swf.PollForActivityTaskOutput)) {
if !b.started {
if b.NumGoroutines == 0 {
//use at least 1
b.NumGoroutines = 1
}
b.tasks = make(chan *swf.PollForActivityTaskOutput)
for i := 0; i < b.NumGoroutines; i++ {
go func() {
for {
select {
case t := <-b.tasks:
handler(t)
}
}
}()
}
b.started = true
}
b.tasks <- task
}
// CountdownGoroutineDispatcher is a dispatcher that you can register with a ShutdownManager. Used in your
// ActivityWorkers, it will count in-flight activities. It doesnt ack shutdowns until the number of in-flight activities are zero.
type CountdownGoroutineDispatcher struct {
Stop chan bool
StopAck chan bool
inFlight int64
}
var countdownDispatcherNameSeq int64
func countdownDispatcherName() string {
seq := atomic.AddInt64(&countdownDispatcherNameSeq, 1)
return fmt.Sprintf("countdown-%d", seq)
}
//RegisterNewCountdownGoroutineDispatcher constructs a new CountdownGoroutineDispatcher, start it and register it with the given ShutdownManager
func RegisterNewCountdownGoroutineDispatcher(mgr poller.ShutdownManager) *CountdownGoroutineDispatcher {
g := &CountdownGoroutineDispatcher{
Stop: make(chan bool, 1),
StopAck: make(chan bool, 1),
}
go g.Start()
mgr.Register(countdownDispatcherName(), g.Stop, g.StopAck)
return g
}
func (m *CountdownGoroutineDispatcher) DispatchTask(t *swf.PollForActivityTaskOutput, f func(*swf.PollForActivityTaskOutput)) {
//run tasks in a new goroutine
go func() {
atomic.AddInt64(&m.inFlight, 1)
f(t)
atomic.AddInt64(&m.inFlight, -1)
}()
}
func (m *CountdownGoroutineDispatcher) Start() {
<-m.Stop
for atomic.LoadInt64(&m.inFlight) > 0 {
time.Sleep(1 * time.Second)
}
m.StopAck <- true
}