Skip to content

Commit 3b8301b

Browse files
committed
Upgrade go-units vendor to latest version with ulimits.
Signed-off-by: Daniel Nephin <[email protected]>
1 parent 577cf61 commit 3b8301b

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

hack/vendor.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ clone git github.com/mistifyio/go-zfs v2.1.1
2020
clone git github.com/tchap/go-patricia v2.1.0
2121
clone git github.com/vdemeester/shakers 3c10293ce22b900c27acad7b28656196fcc2f73b
2222
clone git golang.org/x/net 47990a1ba55743e6ef1affd3a14e5bac8553615d https://github.com/golang/net.git
23-
clone git github.com/docker/go-units v0.2.0
23+
clone git github.com/docker/go-units 651fc226e7441360384da338d0fd37f2440ffbe3
2424
clone git github.com/docker/go-connections 4e42727957c146776e5de9cec8c39e4059ed9f20
2525

2626
#get libnetwork packages
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
dependencies:
2+
post:
3+
# install golint
4+
- go get github.com/golang/lint/golint
5+
6+
test:
7+
pre:
8+
# run analysis before tests
9+
- go vet ./...
10+
- test -z "$(golint ./... | tee /dev/stderr)"
11+
- test -z "$(gofmt -s -l . | tee /dev/stderr)"
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package units
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// Ulimit is a human friendly version of Rlimit.
10+
type Ulimit struct {
11+
Name string
12+
Hard int64
13+
Soft int64
14+
}
15+
16+
// Rlimit specifies the resource limits, such as max open files.
17+
type Rlimit struct {
18+
Type int `json:"type,omitempty"`
19+
Hard uint64 `json:"hard,omitempty"`
20+
Soft uint64 `json:"soft,omitempty"`
21+
}
22+
23+
const (
24+
// magic numbers for making the syscall
25+
// some of these are defined in the syscall package, but not all.
26+
// Also since Windows client doesn't get access to the syscall package, need to
27+
// define these here
28+
rlimitAs = 9
29+
rlimitCore = 4
30+
rlimitCPU = 0
31+
rlimitData = 2
32+
rlimitFsize = 1
33+
rlimitLocks = 10
34+
rlimitMemlock = 8
35+
rlimitMsgqueue = 12
36+
rlimitNice = 13
37+
rlimitNofile = 7
38+
rlimitNproc = 6
39+
rlimitRss = 5
40+
rlimitRtprio = 14
41+
rlimitRttime = 15
42+
rlimitSigpending = 11
43+
rlimitStack = 3
44+
)
45+
46+
var ulimitNameMapping = map[string]int{
47+
//"as": rlimitAs, // Disabled since this doesn't seem usable with the way Docker inits a container.
48+
"core": rlimitCore,
49+
"cpu": rlimitCPU,
50+
"data": rlimitData,
51+
"fsize": rlimitFsize,
52+
"locks": rlimitLocks,
53+
"memlock": rlimitMemlock,
54+
"msgqueue": rlimitMsgqueue,
55+
"nice": rlimitNice,
56+
"nofile": rlimitNofile,
57+
"nproc": rlimitNproc,
58+
"rss": rlimitRss,
59+
"rtprio": rlimitRtprio,
60+
"rttime": rlimitRttime,
61+
"sigpending": rlimitSigpending,
62+
"stack": rlimitStack,
63+
}
64+
65+
// ParseUlimit parses and returns a Ulimit from the specified string.
66+
func ParseUlimit(val string) (*Ulimit, error) {
67+
parts := strings.SplitN(val, "=", 2)
68+
if len(parts) != 2 {
69+
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
70+
}
71+
72+
if _, exists := ulimitNameMapping[parts[0]]; !exists {
73+
return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
74+
}
75+
76+
limitVals := strings.SplitN(parts[1], ":", 2)
77+
if len(limitVals) > 2 {
78+
return nil, fmt.Errorf("too many limit value arguments - %s, can only have up to two, `soft[:hard]`", parts[1])
79+
}
80+
81+
soft, err := strconv.ParseInt(limitVals[0], 10, 64)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
hard := soft // in case no hard was set
87+
if len(limitVals) == 2 {
88+
hard, err = strconv.ParseInt(limitVals[1], 10, 64)
89+
}
90+
if soft > hard {
91+
return nil, fmt.Errorf("ulimit soft limit must be less than or equal to hard limit: %d > %d", soft, hard)
92+
}
93+
94+
return &Ulimit{Name: parts[0], Soft: soft, Hard: hard}, nil
95+
}
96+
97+
// GetRlimit returns the RLimit corresponding to Ulimit.
98+
func (u *Ulimit) GetRlimit() (*Rlimit, error) {
99+
t, exists := ulimitNameMapping[u.Name]
100+
if !exists {
101+
return nil, fmt.Errorf("invalid ulimit name %s", u.Name)
102+
}
103+
104+
return &Rlimit{Type: t, Soft: uint64(u.Soft), Hard: uint64(u.Hard)}, nil
105+
}
106+
107+
func (u *Ulimit) String() string {
108+
return fmt.Sprintf("%s=%d:%d", u.Name, u.Soft, u.Hard)
109+
}

0 commit comments

Comments
 (0)