forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandboxlist.go
49 lines (41 loc) · 976 Bytes
/
sandboxlist.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
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"fmt"
"sync"
)
type sandboxList struct {
lock sync.RWMutex
sandboxes map[string]*Sandbox
}
// globalSandboxList tracks sandboxes globally
var globalSandboxList = &sandboxList{sandboxes: make(map[string]*Sandbox)}
func (p *sandboxList) addSandbox(sandbox *Sandbox) (err error) {
if sandbox == nil {
return nil
}
p.lock.Lock()
defer p.lock.Unlock()
if p.sandboxes[sandbox.id] == nil {
p.sandboxes[sandbox.id] = sandbox
} else {
err = fmt.Errorf("sandbox %s exists", sandbox.id)
}
return err
}
func (p *sandboxList) removeSandbox(id string) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.sandboxes, id)
}
func (p *sandboxList) lookupSandbox(id string) (*Sandbox, error) {
p.lock.RLock()
defer p.lock.RUnlock()
if p.sandboxes[id] != nil {
return p.sandboxes[id], nil
}
return nil, fmt.Errorf("sandbox %s does not exist", id)
}