forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
48 lines (41 loc) · 1.09 KB
/
utils.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
package utils
import (
"errors"
"path/filepath"
"runtime"
)
const (
defaultTLSDir = "tls"
)
// Util vars
var (
ErrGoMaxProcsFailure = errors.New("failed to set GOMAXPROCS")
)
// AdjustGoMaxProcs sets the runtime GOMAXPROCS val
// Since Go 1.5, Go will use the total number of logical processors that the
// system has available. Caveats to this are if someone has set the GOMAXPROCS
// env var set or wish to limit usage of the number of logical processors
// between a range from 1 to NumCPUs
func AdjustGoMaxProcs(procs int) error {
// Check for default settings, plus respecting GOMAXPROCS env but
// don't allow for values which will cause thread contention
n := runtime.NumCPU()
if procs == runtime.GOMAXPROCS(-1) {
if procs <= n {
return nil
}
}
// Sanitise the procs value (defaults to NumCPUs)
if procs < 1 || procs > n {
procs = n
}
runtime.GOMAXPROCS(procs)
if i := runtime.GOMAXPROCS(procs); i != procs {
return ErrGoMaxProcsFailure
}
return nil
}
// GetTLSDir returns the default TLS dir
func GetTLSDir(dir string) string {
return filepath.Join(dir, defaultTLSDir)
}