Skip to content

Commit

Permalink
feat: supports changing number of executing cpu cores for manager api (
Browse files Browse the repository at this point in the history
  • Loading branch information
bisakhmondal authored Mar 10, 2021
1 parent 569c4bd commit e8256c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/conf/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ conf:
logs/access.log # supports relative path, absolute path, standard output
# such as: logs/access.log, /tmp/logs/access.log, /dev/stdout, /dev/stderr
# log example: 2020-12-09T16:38:09.039+0800 INFO filter/logging.go:46 /apisix/admin/routes/r1 {"status": 401, "host": "127.0.0.1:9000", "query": "asdfsafd=adf&a=a", "requestId": "3d50ecb8-758c-46d1-af5b-cd9d1c820156", "latency": 0, "remoteIP": "127.0.0.1", "method": "PUT", "errs": []}
max_cpu: 0 # supports tweaking with the number of OS threads are going to be used for parallelism. Default value: 0 [will use max number of available cpu cores considering hyperthreading (if any)]. If the value is negative, is will not touch the existing parallelism profile.

authentication:
secret:
secret # secret for jwt token generation.
Expand Down
26 changes: 22 additions & 4 deletions api/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"os"
"path/filepath"
"runtime"

"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -96,6 +97,7 @@ type Conf struct {
Listen Listen
Log Log
AllowList []string `yaml:"allow_list"`
MaxCpu int `yaml:"max_cpu"`
}

type User struct {
Expand Down Expand Up @@ -139,14 +141,14 @@ func setConf() {
if configurationContent, err := ioutil.ReadFile(filePath); err != nil {
panic(fmt.Sprintf("fail to read configuration: %s", filePath))
} else {
//configuration := gjson.ParseBytes(configurationContent)
// configuration := gjson.ParseBytes(configurationContent)
config := Config{}
err := yaml.Unmarshal(configurationContent, &config)
if err != nil {
log.Printf("conf: %s, error: %v", configurationContent, err)
}

//listen
// listen
if config.Conf.Listen.Port != 0 {
ServerPort = config.Conf.Listen.Port
}
Expand All @@ -160,7 +162,7 @@ func setConf() {
initEtcdConfig(config.Conf.Etcd)
}

//error log
// error log
if config.Conf.Log.ErrorLog.Level != "" {
ErrorLogLevel = config.Conf.Log.ErrorLog.Level
}
Expand All @@ -187,7 +189,10 @@ func setConf() {

AllowList = config.Conf.AllowList

//auth
// set degree of parallelism
initParallelism(config.Conf.MaxCpu)

// auth
initAuthentication(config.Authentication)

initPlugins(config.Plugins)
Expand Down Expand Up @@ -249,3 +254,16 @@ func initEtcdConfig(conf Etcd) {
Prefix: prefix,
}
}

// initialize parallelism settings
func initParallelism(choiceCores int) {
if choiceCores < 1 {
return
}
maxSupportedCores := runtime.NumCPU()

if choiceCores > maxSupportedCores {
choiceCores = maxSupportedCores
}
runtime.GOMAXPROCS(choiceCores)
}

0 comments on commit e8256c2

Please sign in to comment.