mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-20 22:52:58 -05:00
New 'cpu' directive; now uses all cores by default (if needed)
This commit is contained in:
parent
509db0b08f
commit
fe1978c6f5
3 changed files with 57 additions and 1 deletions
|
@ -65,6 +65,7 @@ type Config struct {
|
|||
TLS TLSConfig
|
||||
Middleware []middleware.Middleware
|
||||
Startup []func() error
|
||||
MaxCPU int
|
||||
}
|
||||
|
||||
// Address returns the host:port of c as a string.
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package config
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// dirFunc is a type of parsing function which processes
|
||||
// a particular directive and populates the config.
|
||||
|
@ -64,5 +69,45 @@ func init() {
|
|||
p.cfg.TLS = tls
|
||||
return nil
|
||||
},
|
||||
"cpu": func(p *parser) error {
|
||||
sysCores := runtime.NumCPU()
|
||||
|
||||
if !p.nextArg() {
|
||||
return p.argErr()
|
||||
}
|
||||
strNum := p.tkn()
|
||||
|
||||
setCPU := func(val int) {
|
||||
if val < 1 {
|
||||
val = 1
|
||||
}
|
||||
if val > sysCores {
|
||||
val = sysCores
|
||||
}
|
||||
if val > p.cfg.MaxCPU {
|
||||
p.cfg.MaxCPU = val
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasSuffix(strNum, "%") {
|
||||
// Percent
|
||||
var percent float32
|
||||
pctStr := strNum[:len(strNum)-1]
|
||||
pctInt, err := strconv.Atoi(pctStr)
|
||||
if err != nil || pctInt < 1 || pctInt > 100 {
|
||||
return p.err("Parse", "Invalid number '"+strNum+"' (must be a positive percentage between 1 and 100)")
|
||||
}
|
||||
percent = float32(pctInt) / 100
|
||||
setCPU(int(float32(sysCores) * percent))
|
||||
} else {
|
||||
// Number
|
||||
num, err := strconv.Atoi(strNum)
|
||||
if err != nil || num < 0 {
|
||||
return p.err("Parse", "Invalid number '"+strNum+"' (requires positive integer or percent)")
|
||||
}
|
||||
setCPU(num)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"github.com/mholt/caddy/config"
|
||||
"github.com/mholt/caddy/middleware"
|
||||
|
@ -36,6 +37,11 @@ func New(conf config.Config) (*Server, error) {
|
|||
return nil, errors.New("Address " + addr + " is already in use")
|
||||
}
|
||||
|
||||
// Use all CPUs (if needed) by default
|
||||
if conf.MaxCPU == 0 {
|
||||
conf.MaxCPU = runtime.NumCPU()
|
||||
}
|
||||
|
||||
// Initialize
|
||||
s := new(Server)
|
||||
s.config = conf
|
||||
|
@ -53,6 +59,10 @@ func (s *Server) Serve() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if s.config.MaxCPU > 0 {
|
||||
runtime.GOMAXPROCS(s.config.MaxCPU)
|
||||
}
|
||||
|
||||
if s.config.TLS.Enabled {
|
||||
return http.ListenAndServeTLS(s.config.Address(), s.config.TLS.Certificate, s.config.TLS.Key, s)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Reference in a new issue