mirror of
https://github.com/openappsec/attachment.git
synced 2025-06-28 16:41:03 +03:00
fix config.go file
This commit is contained in:
parent
cc7e65729c
commit
5962caf2e9
@ -35,53 +35,59 @@ unsigned long get_thread_id() {
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const Name = "cp_nano_filter"
|
const Name = "cp_nano_filter"
|
||||||
|
const admin_api_server_info = "http://127.0.0.1:%s/server_info"
|
||||||
const admin_api_url = "http://127.0.0.1:%s/server_info"
|
const keep_alive_interval = 10 * time.Second
|
||||||
|
|
||||||
var filter_id atomic.Int64
|
var filter_id atomic.Int64
|
||||||
|
|
||||||
type nano_attachment C.struct_NanoAttachment
|
|
||||||
|
|
||||||
var attachments_map map[int]*nano_attachment = nil
|
var attachments_map map[int]*nano_attachment = nil
|
||||||
var thread_to_attachment_mapping map[int]int = nil
|
var thread_to_attachment_mapping map[int]int = nil
|
||||||
var attachment_to_thread_mapping map[int]int = nil
|
var attachment_to_thread_mapping map[int]int = nil
|
||||||
|
|
||||||
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
||||||
|
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
const keep_alive_interval = 10 * time.Second
|
|
||||||
|
|
||||||
var last_keep_alive time.Time
|
var last_keep_alive time.Time
|
||||||
|
|
||||||
|
type nano_attachment C.struct_NanoAttachment
|
||||||
|
|
||||||
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
||||||
type EnvoyServerInfo struct {
|
type EnvoyServerInfo struct {
|
||||||
Concurrency int `json:"concurrency"`
|
Concurrency int `json:"concurrency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEnvoyConcurrency fetches and returns the concurrency level of Envoy from the admin API
|
func getEnvoyConcurrency() int {
|
||||||
func getEnvoyConcurrency(admin_api_address string) (int, error) {
|
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
||||||
resp, err := http.Get(admin_api_address)
|
|
||||||
if err != nil {
|
if concurrency_method == "numOfCores" {
|
||||||
return 0, fmt.Errorf("failed to reach Envoy admin API: %w", err)
|
api.LogWarnf("using number of CPU cores")
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
var conc_number string
|
||||||
return 0, fmt.Errorf("unexpected status code from Envoy admin API: %d", resp.StatusCode)
|
|
||||||
|
switch concurrency_method {
|
||||||
|
case "istioCpuLimit":
|
||||||
|
conc_number = getEnv("ISTIO_CPU_LIMIT", "-1")
|
||||||
|
api.LogWarnf("using istioCpuLimit, conc_number %s", conc_number)
|
||||||
|
case "custom":
|
||||||
|
conc_number = getEnv("CONCURRENCY_NUMBER", "-1")
|
||||||
|
api.LogWarnf("using custom concurrency number, conc_number %s", conc_number)
|
||||||
|
default:
|
||||||
|
api.LogWarnf("unknown concurrency method %s, using number of CPU cores", concurrency_method)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
if conc_number == "-1" {
|
||||||
if err != nil {
|
api.LogWarnf("concurrency number is not set as an env variable, using number of CPU cores")
|
||||||
return 0, fmt.Errorf("failed to read response body: %w", err)
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
var info EnvoyServerInfo
|
conc_num, err := strconv.Atoi(conc_number)
|
||||||
if err := json.Unmarshal(body, &info); err != nil {
|
if err != nil || conc_num <= 0 {
|
||||||
return 0, fmt.Errorf("failed to parse JSON response: %w", err)
|
api.LogWarnf("error converting concurrency number %s, using number of CPU cores", conc_number)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
return info.Concurrency, nil
|
return conc_num
|
||||||
}
|
}
|
||||||
|
|
||||||
func configurationServer() {
|
func configurationServer() {
|
||||||
@ -193,37 +199,7 @@ func (p *parser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (int
|
|||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var num_of_workers int
|
num_of_workers := getEnvoyConcurrency()
|
||||||
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
|
||||||
|
|
||||||
if concurrency_method == "numOfCores" {
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
api.LogInfof("using number of cpu cores %d", num_of_workers)
|
|
||||||
} else if concurrency_method == "config" {
|
|
||||||
config_port := getEnv("CONFIG_PORT", "15000")
|
|
||||||
admin_api := fmt.Sprintf(admin_api_url, config_port)
|
|
||||||
workers, err := getEnvoyConcurrency(admin_api)
|
|
||||||
if err != nil {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from admin server, using cpu cores. err: %s", err.Error())
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else {
|
|
||||||
num_of_workers = workers
|
|
||||||
}
|
|
||||||
} else if concurrency_method == "custom" {
|
|
||||||
conc_number := getEnv("CONCURRENCY_NUMBER", "-1")
|
|
||||||
if conc_number == "-1" {
|
|
||||||
api.LogWarnf("concurrency number is not set as an env variable, using cpu cores")
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else if conc_num, err := strconv.Atoi(conc_number); err == nil && conc_num > 0 {
|
|
||||||
num_of_workers = conc_num
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("error converting conc_number %s, using num of cpu cores", conc_number)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from %s, using cpu cores", concurrency_method)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
|
|
||||||
configStruct := &xds.TypedStruct{}
|
configStruct := &xds.TypedStruct{}
|
||||||
if err := any.UnmarshalTo(configStruct); err != nil {
|
if err := any.UnmarshalTo(configStruct); err != nil {
|
||||||
|
@ -35,53 +35,59 @@ unsigned long get_thread_id() {
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const Name = "cp_nano_filter"
|
const Name = "cp_nano_filter"
|
||||||
|
const admin_api_server_info = "http://127.0.0.1:%s/server_info"
|
||||||
const Admin_api = "http://127.0.0.1:%s/server_info"
|
const keep_alive_interval = 10 * time.Second
|
||||||
|
|
||||||
var filter_id atomic.Int64
|
var filter_id atomic.Int64
|
||||||
|
|
||||||
type nano_attachment C.struct_NanoAttachment
|
|
||||||
|
|
||||||
var attachments_map map[int]*nano_attachment = nil
|
var attachments_map map[int]*nano_attachment = nil
|
||||||
var thread_to_attachment_mapping map[int]int = nil
|
var thread_to_attachment_mapping map[int]int = nil
|
||||||
var attachment_to_thread_mapping map[int]int = nil
|
var attachment_to_thread_mapping map[int]int = nil
|
||||||
|
|
||||||
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
||||||
|
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
const keep_alive_interval = 10 * time.Second
|
|
||||||
|
|
||||||
var last_keep_alive time.Time
|
var last_keep_alive time.Time
|
||||||
|
|
||||||
|
type nano_attachment C.struct_NanoAttachment
|
||||||
|
|
||||||
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
||||||
type EnvoyServerInfo struct {
|
type EnvoyServerInfo struct {
|
||||||
Concurrency int `json:"concurrency"`
|
Concurrency int `json:"concurrency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEnvoyConcurrency fetches and returns the concurrency level of Envoy from the admin API
|
func getEnvoyConcurrency() int {
|
||||||
func getEnvoyConcurrency(admin_api_address string) (int, error) {
|
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
||||||
resp, err := http.Get(admin_api_address)
|
|
||||||
if err != nil {
|
if concurrency_method == "numOfCores" {
|
||||||
return 0, fmt.Errorf("failed to reach Envoy admin API: %w", err)
|
api.LogWarnf("using number of CPU cores")
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
var conc_number string
|
||||||
return 0, fmt.Errorf("unexpected status code from Envoy admin API: %d", resp.StatusCode)
|
|
||||||
|
switch concurrency_method {
|
||||||
|
case "istioCpuLimit":
|
||||||
|
conc_number = getEnv("ISTIO_CPU_LIMIT", "-1")
|
||||||
|
api.LogWarnf("using istioCpuLimit, conc_number %s", conc_number)
|
||||||
|
case "custom":
|
||||||
|
conc_number = getEnv("CONCURRENCY_NUMBER", "-1")
|
||||||
|
api.LogWarnf("using custom concurrency number, conc_number %s", conc_number)
|
||||||
|
default:
|
||||||
|
api.LogWarnf("unknown concurrency method %s, using number of CPU cores", concurrency_method)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
if conc_number == "-1" {
|
||||||
if err != nil {
|
api.LogWarnf("concurrency number is not set as an env variable, using number of CPU cores")
|
||||||
return 0, fmt.Errorf("failed to read response body: %w", err)
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
var info EnvoyServerInfo
|
conc_num, err := strconv.Atoi(conc_number)
|
||||||
if err := json.Unmarshal(body, &info); err != nil {
|
if err != nil || conc_num <= 0 {
|
||||||
return 0, fmt.Errorf("failed to parse JSON response: %w", err)
|
api.LogWarnf("error converting concurrency number %s, using number of CPU cores", conc_number)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
return info.Concurrency, nil
|
return conc_num
|
||||||
}
|
}
|
||||||
|
|
||||||
func configurationServer() {
|
func configurationServer() {
|
||||||
@ -193,37 +199,7 @@ func (p *parser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (int
|
|||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var num_of_workers int
|
num_of_workers := getEnvoyConcurrency()
|
||||||
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
|
||||||
|
|
||||||
if concurrency_method == "numOfCores" {
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
api.LogInfof("using number of cpu cores %d", num_of_workers)
|
|
||||||
} else if concurrency_method == "config" {
|
|
||||||
config_port := getEnv("CONFIG_PORT", "15000")
|
|
||||||
admin_api := fmt.Sprintf(Admin_api, config_port)
|
|
||||||
workers, err := getEnvoyConcurrency(admin_api)
|
|
||||||
if err != nil {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from admin server, using cpu cores. err: %s", err.Error())
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else {
|
|
||||||
num_of_workers = workers
|
|
||||||
}
|
|
||||||
} else if concurrency_method == "custom" {
|
|
||||||
conc_number := getEnv("CONCURRENCY_NUMBER", "-1")
|
|
||||||
if conc_number == "-1" {
|
|
||||||
api.LogWarnf("concurrency number is not set as an env variable, using cpu cores")
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else if conc_num, err := strconv.Atoi(conc_number); err == nil && conc_num > 0 {
|
|
||||||
num_of_workers = conc_num
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("error converting conc_number %s, using num of cpu cores", conc_number)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from %s, using cpu cores", concurrency_method)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
|
|
||||||
configStruct := &xds.TypedStruct{}
|
configStruct := &xds.TypedStruct{}
|
||||||
if err := any.UnmarshalTo(configStruct); err != nil {
|
if err := any.UnmarshalTo(configStruct); err != nil {
|
||||||
@ -306,5 +282,4 @@ func ConfigFactory(c interface{}, callbacks api.FilterCallbackHandler) api.Strea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
@ -35,53 +35,59 @@ unsigned long get_thread_id() {
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const Name = "cp_nano_filter"
|
const Name = "cp_nano_filter"
|
||||||
|
const admin_api_server_info = "http://127.0.0.1:%s/server_info"
|
||||||
const Admin_api = "http://127.0.0.1:%s/server_info"
|
const keep_alive_interval = 10 * time.Second
|
||||||
|
|
||||||
var filter_id atomic.Int64
|
var filter_id atomic.Int64
|
||||||
|
|
||||||
type nano_attachment C.struct_NanoAttachment
|
|
||||||
|
|
||||||
var attachments_map map[int]*nano_attachment = nil
|
var attachments_map map[int]*nano_attachment = nil
|
||||||
var thread_to_attachment_mapping map[int]int = nil
|
var thread_to_attachment_mapping map[int]int = nil
|
||||||
var attachment_to_thread_mapping map[int]int = nil
|
var attachment_to_thread_mapping map[int]int = nil
|
||||||
|
|
||||||
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
var attachment_to_filter_request_structs map[int]*filterRequestStructs = nil
|
||||||
|
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
const keep_alive_interval = 10 * time.Second
|
|
||||||
|
|
||||||
var last_keep_alive time.Time
|
var last_keep_alive time.Time
|
||||||
|
|
||||||
|
type nano_attachment C.struct_NanoAttachment
|
||||||
|
|
||||||
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
// EnvoyServerInfo represents the structure of the JSON response from /server_info
|
||||||
type EnvoyServerInfo struct {
|
type EnvoyServerInfo struct {
|
||||||
Concurrency int `json:"concurrency"`
|
Concurrency int `json:"concurrency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEnvoyConcurrency fetches and returns the concurrency level of Envoy from the admin API
|
func getEnvoyConcurrency() int {
|
||||||
func getEnvoyConcurrency(admin_api_address string) (int, error) {
|
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
||||||
resp, err := http.Get(admin_api_address)
|
|
||||||
if err != nil {
|
if concurrency_method == "numOfCores" {
|
||||||
return 0, fmt.Errorf("failed to reach Envoy admin API: %w", err)
|
api.LogWarnf("using number of CPU cores")
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
var conc_number string
|
||||||
return 0, fmt.Errorf("unexpected status code from Envoy admin API: %d", resp.StatusCode)
|
|
||||||
|
switch concurrency_method {
|
||||||
|
case "istioCpuLimit":
|
||||||
|
conc_number = getEnv("ISTIO_CPU_LIMIT", "-1")
|
||||||
|
api.LogWarnf("using istioCpuLimit, conc_number %s", conc_number)
|
||||||
|
case "custom":
|
||||||
|
conc_number = getEnv("CONCURRENCY_NUMBER", "-1")
|
||||||
|
api.LogWarnf("using custom concurrency number, conc_number %s", conc_number)
|
||||||
|
default:
|
||||||
|
api.LogWarnf("unknown concurrency method %s, using number of CPU cores", concurrency_method)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
if conc_number == "-1" {
|
||||||
if err != nil {
|
api.LogWarnf("concurrency number is not set as an env variable, using number of CPU cores")
|
||||||
return 0, fmt.Errorf("failed to read response body: %w", err)
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
var info EnvoyServerInfo
|
conc_num, err := strconv.Atoi(conc_number)
|
||||||
if err := json.Unmarshal(body, &info); err != nil {
|
if err != nil || conc_num <= 0 {
|
||||||
return 0, fmt.Errorf("failed to parse JSON response: %w", err)
|
api.LogWarnf("error converting concurrency number %s, using number of CPU cores", conc_number)
|
||||||
|
return runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
return info.Concurrency, nil
|
return conc_num
|
||||||
}
|
}
|
||||||
|
|
||||||
func configurationServer() {
|
func configurationServer() {
|
||||||
@ -193,37 +199,7 @@ func (p *parser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (int
|
|||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var num_of_workers int
|
num_of_workers := getEnvoyConcurrency()
|
||||||
concurrency_method := getEnv("CONCURRENCY_CALC", "numOfCores")
|
|
||||||
|
|
||||||
if concurrency_method == "numOfCores" {
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
api.LogInfof("using number of cpu cores %d", num_of_workers)
|
|
||||||
} else if concurrency_method == "config" {
|
|
||||||
config_port := getEnv("CONFIG_PORT", "15000")
|
|
||||||
admin_api := fmt.Sprintf(Admin_api, config_port)
|
|
||||||
workers, err := getEnvoyConcurrency(admin_api)
|
|
||||||
if err != nil {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from admin server, using cpu cores. err: %s", err.Error())
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else {
|
|
||||||
num_of_workers = workers
|
|
||||||
}
|
|
||||||
} else if concurrency_method == "custom" {
|
|
||||||
conc_number := getEnv("CONCURRENCY_NUMBER", "-1")
|
|
||||||
if conc_number == "-1" {
|
|
||||||
api.LogWarnf("concurrency number is not set as an env variable, using cpu cores")
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
} else if conc_num, err := strconv.Atoi(conc_number); err == nil && conc_num > 0 {
|
|
||||||
num_of_workers = conc_num
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("error converting conc_number %s, using num of cpu cores", conc_number)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
api.LogWarnf("unable to fetch concurrency from %s, using cpu cores", concurrency_method)
|
|
||||||
num_of_workers = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
|
|
||||||
configStruct := &xds.TypedStruct{}
|
configStruct := &xds.TypedStruct{}
|
||||||
if err := any.UnmarshalTo(configStruct); err != nil {
|
if err := any.UnmarshalTo(configStruct); err != nil {
|
||||||
@ -306,5 +282,4 @@ func ConfigFactory(c interface{}, callbacks api.FilterCallbackHandler) api.Strea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user