-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlimits.go
More file actions
35 lines (31 loc) · 764 Bytes
/
limits.go
File metadata and controls
35 lines (31 loc) · 764 Bytes
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
package main
import (
"syscall"
)
type Limit struct {
Resource int
Rlimit *syscall.Rlimit
}
func SetLimits(cfg *Config) ([]*Limit, error) {
var limits []*Limit
if cfg.MaxOpenFiles > 0 {
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return nil, err
}
limits = append(limits, &Limit{Resource: syscall.RLIMIT_NOFILE, Rlimit: &limit})
max := uint64(cfg.MaxOpenFiles)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{Cur: max, Max: max}); err != nil {
return nil, err
}
}
return limits, nil
}
func RestoreLimits(limits []*Limit) error {
for _, v := range limits {
if err := syscall.Setrlimit(v.Resource, v.Rlimit); err != nil {
return err
}
}
return nil
}