-
Notifications
You must be signed in to change notification settings - Fork 662
/
manager.go
48 lines (40 loc) · 918 Bytes
/
manager.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package runtime
import (
"context"
"sync"
)
// Manages tracking and shutdown of VM runtimes.
type manager struct {
lock sync.Mutex
runtimes []Stopper
}
// NewManager returns manager of VM runtimes.
//
// TODO: If a runtime exits before the call to `manager.Stop`, it would be nice
// to remove it from the current set.
func NewManager() Manager {
return &manager{}
}
func (m *manager) Stop(ctx context.Context) {
var wg sync.WaitGroup
m.lock.Lock()
defer func() {
m.lock.Unlock()
wg.Wait()
}()
wg.Add(len(m.runtimes))
for _, rt := range m.runtimes {
go func(runtime Stopper) {
defer wg.Done()
runtime.Stop(ctx)
}(rt)
}
m.runtimes = nil
}
func (m *manager) TrackRuntime(runtime Stopper) {
m.lock.Lock()
defer m.lock.Unlock()
m.runtimes = append(m.runtimes, runtime)
}