-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
102 lines (85 loc) · 1.72 KB
/
cgroup.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
package cgroup
import (
"fmt"
"sync"
)
type ICgroup interface {
Create() error
Version() CgroupVersion
SetOptions(options ...Option)
Instance() any
Close() error
Load() error
LimitPid(pid int) error
GetLimitPids() ([]uint64, error)
Stats() (any, error)
SetDebug(debug bool)
}
type cgroupImpl struct {
version CgroupVersion
cg ICgroup
ch chan bool
lock sync.Mutex
}
func NewCgroup(version CgroupVersion, options ...Option) (ICgroup, error) {
cg := &cgroupImpl{
version: version,
ch: make(chan bool),
lock: sync.Mutex{},
}
switch version {
case V1:
cg.cg = newCgroupImplV1()
case V2:
cg.cg = newCgroupImplV2()
default:
return nil, fmt.Errorf("unsupported cgroup version")
}
cg.SetOptions(options...)
return cg, nil
}
func (c *cgroupImpl) Version() CgroupVersion {
return c.version
}
func (c *cgroupImpl) Create() error {
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.Create()
}
func (c *cgroupImpl) SetOptions(options ...Option) {
c.lock.Lock()
defer c.lock.Unlock()
c.cg.SetOptions(options...)
}
func (c *cgroupImpl) Instance() any {
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.Instance()
}
func (c *cgroupImpl) Close() error {
close(c.ch)
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.Close()
}
func (c *cgroupImpl) Load() error {
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.Load()
}
func (c *cgroupImpl) LimitPid(pid int) error {
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.LimitPid(pid)
}
func (c *cgroupImpl) GetLimitPids() ([]uint64, error) {
c.lock.Lock()
defer c.lock.Unlock()
return c.cg.GetLimitPids()
}
func (c *cgroupImpl) Stats() (any, error) {
return c.cg.Stats()
}
func (c *cgroupImpl) SetDebug(debug bool) {
c.cg.SetDebug(debug)
}