-
Notifications
You must be signed in to change notification settings - Fork 671
/
vm_registry.go
65 lines (52 loc) · 1.73 KB
/
vm_registry.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package registry
import "github.com/ava-labs/avalanchego/ids"
var _ VMRegistry = &vmRegistry{}
// VMRegistry defines functionality to get any new virtual machines on the node,
// and install them if they're not already installed.
type VMRegistry interface {
// Reload installs all non-installed vms on the node.
Reload() ([]ids.ID, map[ids.ID]error, error)
// ReloadWithReadLock installs all non-installed vms on the node assuming
// the http read lock is currently held.
ReloadWithReadLock() ([]ids.ID, map[ids.ID]error, error)
}
// VMRegistryConfig defines configurations for VMRegistry
type VMRegistryConfig struct {
VMGetter VMGetter
VMRegisterer VMRegisterer
}
type vmRegistry struct {
config VMRegistryConfig
}
// NewVMRegistry returns a VMRegistry
func NewVMRegistry(config VMRegistryConfig) VMRegistry {
return &vmRegistry{
config: config,
}
}
func (r *vmRegistry) Reload() ([]ids.ID, map[ids.ID]error, error) {
return r.reload(r.config.VMRegisterer)
}
func (r *vmRegistry) ReloadWithReadLock() ([]ids.ID, map[ids.ID]error, error) {
return r.reload(readRegisterer{
registerer: r.config.VMRegisterer,
})
}
func (r *vmRegistry) reload(registerer registerer) ([]ids.ID, map[ids.ID]error, error) {
_, unregisteredVMs, err := r.config.VMGetter.Get()
if err != nil {
return nil, nil, err
}
registeredVms := make([]ids.ID, 0, len(unregisteredVMs))
failedVMs := make(map[ids.ID]error)
for vmID, factory := range unregisteredVMs {
if err := registerer.Register(vmID, factory); err != nil {
failedVMs[vmID] = err
continue
}
registeredVms = append(registeredVms, vmID)
}
return registeredVms, failedVMs, nil
}