-
Notifications
You must be signed in to change notification settings - Fork 16
/
routine.go
107 lines (95 loc) · 2.52 KB
/
routine.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
package machine
import (
"context"
"sync"
"time"
)
// Routine is an interface representing a goroutine
type Routine interface {
// Context returns the goroutines unique context that may be used for cancellation
Context() context.Context
// PID() is the goroutines unique process id
PID() int
// Tags() are the tags associated with the goroutine
Tags() []string
// Start is when the goroutine started
Start() time.Time
// Duration is the duration since the goroutine started
Duration() time.Duration
// Publish publishes the object to the given channel
Publish(channel string, obj interface{})
// Subscribe subscribes to a channel and executes the function on every message passed to it. It exits if the goroutines context is cancelled.
Subscribe(channel string, handler func(obj interface{}))
// Machine returns the underlying routine's machine instance
Machine() *Machine
}
type goRoutine struct {
machine *Machine
ctx context.Context
id int
tags []string
start time.Time
doneOnce sync.Once
cancel func()
}
func (r *goRoutine) Context() context.Context {
return r.ctx
}
func (r *goRoutine) PID() int {
return r.id
}
func (r *goRoutine) Tags() []string {
return r.tags
}
func (r *goRoutine) Start() time.Time {
return r.start
}
func (r *goRoutine) Duration() time.Duration {
return time.Since(r.start)
}
func (g *goRoutine) Publish(channel string, obj interface{}) {
if g.machine.subscriptions[channel] == nil {
g.machine.subMu.Lock()
g.machine.subscriptions[channel] = map[int]chan interface{}{}
g.machine.subMu.Unlock()
}
channelSubscribers := g.machine.subscriptions[channel]
for _, input := range channelSubscribers {
input <- obj
}
}
func (g *goRoutine) Subscribe(channel string, handler func(obj interface{})) {
ctx, cancel := context.WithCancel(g.ctx)
defer cancel()
g.machine.subMu.Lock()
ch := make(chan interface{}, g.machine.subChanLength)
if g.machine.subscriptions[channel] == nil {
g.machine.subscriptions[channel] = map[int]chan interface{}{}
}
g.machine.subscriptions[channel][g.id] = ch
g.machine.subMu.Unlock()
defer func() {
g.machine.subMu.Lock()
delete(g.machine.subscriptions[channel], g.id)
g.machine.subMu.Unlock()
}()
for {
select {
case <-ctx.Done():
return
case msg := <-g.machine.subscriptions[channel][g.id]:
handler(msg)
}
}
}
func (g *goRoutine) Machine() *Machine {
return g.machine
}
func (g *goRoutine) done() {
g.doneOnce.Do(func() {
g.cancel()
g.machine.mu.Lock()
delete(g.machine.routines, g.id)
g.machine.mu.Unlock()
})
}