diff --git a/api/conf/conf.yaml b/api/conf/conf.yaml index 212f1e575b..70efac79ce 100644 --- a/api/conf/conf.yaml +++ b/api/conf/conf.yaml @@ -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. diff --git a/api/internal/conf/conf.go b/api/internal/conf/conf.go index ca8c112772..0e30df4d92 100644 --- a/api/internal/conf/conf.go +++ b/api/internal/conf/conf.go @@ -22,6 +22,7 @@ import ( "log" "os" "path/filepath" + "runtime" "github.com/tidwall/gjson" "gopkg.in/yaml.v2" @@ -96,6 +97,7 @@ type Conf struct { Listen Listen Log Log AllowList []string `yaml:"allow_list"` + MaxCpu int `yaml:"max_cpu"` } type User struct { @@ -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 } @@ -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 } @@ -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) @@ -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) +}