-
Notifications
You must be signed in to change notification settings - Fork 158
/
constraints.go
81 lines (64 loc) · 1.67 KB
/
constraints.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
package empire
import (
"encoding/json"
"fmt"
. "github.com/remind101/empire/pkg/bytesize"
"github.com/remind101/empire/pkg/constraints"
)
var (
Constraints1X = Constraints{constraints.CPUShare(256), constraints.Memory(512 * MB), constraints.Nproc(256)}
Constraints2X = Constraints{constraints.CPUShare(512), constraints.Memory(1 * GB), constraints.Nproc(512)}
ConstraintsPX = Constraints{constraints.CPUShare(1024), constraints.Memory(6 * GB), 0}
// NamedConstraints maps a heroku dynos size to a Constraints.
NamedConstraints = map[string]Constraints{
"1X": Constraints1X,
"2X": Constraints2X,
"PX": ConstraintsPX,
}
// DefaultConstraints defaults to 1X process size.
DefaultConstraints = Constraints1X
)
// Constraints aliases the constraints.Constraints type to implement the
// json.Unmarshaller interface.
type Constraints constraints.Constraints
func parseConstraints(con string) (*Constraints, error) {
if con == "" {
return nil, nil
}
if n, ok := NamedConstraints[con]; ok {
c := Constraints(n)
return &c, nil
}
c, err := constraints.Parse(con)
if err != nil {
return nil, err
}
r := Constraints(c)
return &r, nil
}
func (c *Constraints) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
cc, err := parseConstraints(s)
if err != nil {
return err
}
if cc != nil {
*c = *cc
}
return nil
}
func (c Constraints) String() string {
for n, constraint := range NamedConstraints {
if c == Constraints(constraint) {
return n
}
}
if c.Nproc == 0 {
return fmt.Sprintf("%d:%s", c.CPUShare, c.Memory)
} else {
return fmt.Sprintf("%d:%s:nproc=%d", c.CPUShare, c.Memory, c.Nproc)
}
}