/
process.go
270 lines (222 loc) · 4.58 KB
/
process.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package process
import (
"context"
"errors"
"sync"
"redwood.dev/log"
"redwood.dev/utils"
)
type Process struct {
log.Logger
name string
mu sync.RWMutex
children map[Spawnable]struct{}
state State
goroutines map[*goroutine]struct{}
closeOnce sync.Once
doneOnce sync.Once
chStop chan struct{}
chDone chan struct{}
chAutoclose chan struct{}
wg sync.WaitGroup
debug bool
}
var _ Interface = (*Process)(nil)
type Interface interface {
Spawnable
ProcessTreer
Autoclose()
AutocloseWithCleanup(closeFn func())
Ctx() context.Context
NewChild(ctx context.Context, name string) *Process
SpawnChild(ctx context.Context, child Spawnable) error
Go(ctx context.Context, name string, fn func(ctx context.Context)) <-chan struct{}
}
type Spawnable interface {
Name() string
Start() error
Close() error
Done() <-chan struct{}
State() State
}
type ProcessTreer interface {
Spawnable
ProcessTree() map[string]interface{}
}
var _ Spawnable = (*Process)(nil)
func New(name string) *Process {
name = name + " #" + utils.RandomNumberString()
return &Process{
name: name,
children: make(map[Spawnable]struct{}),
goroutines: make(map[*goroutine]struct{}),
chStop: make(chan struct{}),
chDone: make(chan struct{}),
Logger: log.NewLogger(""),
}
}
func (p *Process) Start() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.state != Unstarted {
panic(p.name + " already started")
}
p.state = Started
return nil
}
func (p *Process) Close() error {
p.closeOnce.Do(func() {
// p.Infof(0, p.name+" shutting down...")
func() {
p.mu.Lock()
defer p.mu.Unlock()
if p.state != Started {
panic(p.name + " not started")
}
p.state = Closed
}()
close(p.chStop)
p.wg.Wait()
close(p.chDone)
})
return nil
}
func (p *Process) Autoclose() {
go func() {
p.wg.Wait()
p.Close()
}()
}
func (p *Process) AutocloseWithCleanup(closeFn func()) {
go func() {
p.wg.Wait()
closeFn()
p.Close()
}()
}
func (p *Process) Name() string {
return p.name
}
func (p *Process) State() State {
p.mu.RLock()
defer p.mu.RUnlock()
return p.state
}
func (p *Process) Ctx() context.Context {
return utils.ChanContext(p.chStop)
}
func (p *Process) ProcessTree() map[string]interface{} {
p.mu.RLock()
defer p.mu.RUnlock()
var goroutines []string
for goroutine := range p.goroutines {
goroutines = append(goroutines, goroutine.name)
}
children := make(map[string]interface{}, len(p.children))
for child := range p.children {
if child, is := child.(ProcessTreer); is {
children[child.Name()] = child.ProcessTree()
} else {
children[child.Name()] = nil
}
}
return map[string]interface{}{
"status": p.state.String(),
"goroutines": goroutines,
"children": children,
}
}
func (p *Process) NewChild(ctx context.Context, name string) *Process {
child := New(name)
_ = p.SpawnChild(ctx, child)
return child
}
var (
ErrUnstarted = errors.New("unstarted")
ErrClosed = errors.New("closed")
)
func (p *Process) SpawnChild(ctx context.Context, child Spawnable) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.state == Unstarted {
panic(p.name + " can't spawn children; not started")
} else if p.state == Closed {
return nil
}
err := child.Start()
if err != nil {
return err
}
p.children[child] = struct{}{}
p.wg.Add(1)
go func() {
defer func() {
p.wg.Done()
p.mu.Lock()
delete(p.children, child)
p.mu.Unlock()
}()
var ctxDone <-chan struct{}
if ctx != nil {
ctxDone = ctx.Done()
}
select {
case <-p.chStop:
child.Close()
case <-ctxDone:
child.Close()
case <-child.Done():
}
}()
return nil
}
type goroutine struct {
name string
chDone chan struct{}
}
func (p *Process) Go(ctx context.Context, name string, fn func(ctx context.Context)) <-chan struct{} {
p.mu.Lock()
defer p.mu.Unlock()
if p.state != Started {
ch := make(chan struct{})
close(ch)
return ch
}
g := &goroutine{name: name + " #" + utils.RandomNumberString(), chDone: make(chan struct{})}
p.goroutines[g] = struct{}{}
p.wg.Add(1)
go func() {
defer func() {
p.wg.Done()
p.mu.Lock()
defer p.mu.Unlock()
delete(p.goroutines, g)
close(g.chDone)
}()
ctx, cancel := utils.CombinedContext(ctx, p.chStop)
defer cancel()
fn(ctx)
}()
return g.chDone
}
func (p *Process) Done() <-chan struct{} {
return p.chDone
}
type State int
const (
Unstarted State = iota
Started
Closed
)
func (s State) String() string {
switch s {
case Unstarted:
return "unstarted"
case Started:
return "started"
case Closed:
return "closed"
default:
return "(err: unknown)"
}
}