From a536a3e7946fd31fcb1e0479db6740aec8938d91 Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Mon, 12 Aug 2024 10:45:26 +0300 Subject: [PATCH] make it configurable --- dbutil/connlog.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/dbutil/connlog.go b/dbutil/connlog.go index 9506d93..048a83b 100644 --- a/dbutil/connlog.go +++ b/dbutil/connlog.go @@ -11,7 +11,7 @@ import ( "database/sql" "errors" "fmt" - "runtime" + "os" "strconv" "strings" "time" @@ -19,7 +19,16 @@ import ( "golang.org/x/sync/semaphore" ) -var sem *semaphore.Weighted = semaphore.NewWeighted(int64(runtime.GOMAXPROCS(0))) +var sem *semaphore.Weighted + +func init() { + maxSqlSemaphore, _ := strconv.ParseInt(os.Getenv("MAXSQLSEMAPHORE"), 10, 64) + if maxSqlSemaphore < 1 { + maxSqlSemaphore = 1 + } + + sem = semaphore.NewWeighted(maxSqlSemaphore) +} // LoggingExecable is a wrapper for anything with database Exec methods (i.e. sql.Conn, sql.DB and sql.Tx) // that can preprocess queries (e.g. replacing $ with ? on SQLite) and log query durations.