Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports changing number of executing cpu cores for manager api #1569

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
bisakhmondal marked this conversation as resolved.
Show resolved Hide resolved
runtime.GOMAXPROCS(choiceCores)
}