From 1845018d21aba9253c01bccf587c2edc983e392f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 26 Nov 2024 13:35:30 +0300 Subject: [PATCH 1/3] add global `OS_MAX_THREADS` env variable --- env/env.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/env/env.go b/env/env.go index 69ed094b..f7dfa84f 100644 --- a/env/env.go +++ b/env/env.go @@ -8,8 +8,14 @@ import ( ) var ( - TLS_VERIFY = os.Getenv("TLS_VERIFY") == "true" - DEBUG = os.Getenv("DEBUG") == "true" + TLS_VERIFY = os.Getenv("TLS_VERIFY") == "true" + DEBUG = os.Getenv("DEBUG") == "true" + OS_MAX_THREADS = func() int { + if value, err := strconv.Atoi(os.Getenv("OS_MAX_THREADS")); err == nil && value != 0 { + return value + } + return 10000 + }() ) // ExpandWithEnv updates string variables to their corresponding environment values. From a864c499f78b8a2245f1ee91de02b41897688aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 26 Nov 2024 13:44:56 +0300 Subject: [PATCH 2/3] add new pkg `global` --- env/env.go | 10 ++-------- global/global.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 global/global.go diff --git a/env/env.go b/env/env.go index f7dfa84f..69ed094b 100644 --- a/env/env.go +++ b/env/env.go @@ -8,14 +8,8 @@ import ( ) var ( - TLS_VERIFY = os.Getenv("TLS_VERIFY") == "true" - DEBUG = os.Getenv("DEBUG") == "true" - OS_MAX_THREADS = func() int { - if value, err := strconv.Atoi(os.Getenv("OS_MAX_THREADS")); err == nil && value != 0 { - return value - } - return 10000 - }() + TLS_VERIFY = os.Getenv("TLS_VERIFY") == "true" + DEBUG = os.Getenv("DEBUG") == "true" ) // ExpandWithEnv updates string variables to their corresponding environment values. diff --git a/global/global.go b/global/global.go new file mode 100644 index 00000000..15ed4f29 --- /dev/null +++ b/global/global.go @@ -0,0 +1,21 @@ +package global + +import ( + "os" + "strconv" + + "github.com/projectdiscovery/utils/sysutil" +) + +var OS_MAX_THREADS int + +func init() { + OS_MAX_THREADS = func() int { + if value, err := strconv.Atoi(os.Getenv("OS_MAX_THREADS")); err == nil && value != 0 { + return value + } + return 10000 + }() + + _ = sysutil.SetMaxThreads(OS_MAX_THREADS) +} From 6bcddac76012135cd6f23409aef450e896751d1a Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Tue, 26 Nov 2024 16:38:35 +0100 Subject: [PATCH 3/3] refactor --- global/global.go | 21 --------------------- global/max_threads.go | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 global/global.go create mode 100644 global/max_threads.go diff --git a/global/global.go b/global/global.go deleted file mode 100644 index 15ed4f29..00000000 --- a/global/global.go +++ /dev/null @@ -1,21 +0,0 @@ -package global - -import ( - "os" - "strconv" - - "github.com/projectdiscovery/utils/sysutil" -) - -var OS_MAX_THREADS int - -func init() { - OS_MAX_THREADS = func() int { - if value, err := strconv.Atoi(os.Getenv("OS_MAX_THREADS")); err == nil && value != 0 { - return value - } - return 10000 - }() - - _ = sysutil.SetMaxThreads(OS_MAX_THREADS) -} diff --git a/global/max_threads.go b/global/max_threads.go new file mode 100644 index 00000000..ef624c28 --- /dev/null +++ b/global/max_threads.go @@ -0,0 +1,24 @@ +package global + +import ( + "os" + "strconv" + + "github.com/projectdiscovery/utils/sysutil" +) + +const OS_MAX_THREADS_ENV = "OS_MAX_THREADS" + +func init() { + handleOSMaxThreads() +} + +func handleOSMaxThreads() { + osMaxThreads := os.Getenv(OS_MAX_THREADS_ENV) + if osMaxThreads == "" { + return + } + if value, err := strconv.Atoi(osMaxThreads); err == nil && value > 0 { + _ = sysutil.SetMaxThreads(value) + } +}