From b08e27a435e9f79ec5982dd1fd44c7ebc8cf9aa4 Mon Sep 17 00:00:00 2001 From: Adi Muraru Date: Mon, 27 Jun 2022 23:58:05 +0300 Subject: [PATCH] Set GOMAXPROCS automatically This is required in containerised environments to properly set GOMAXPROCS to actual container limits set and not the whole underlying node's CPUs See: https://github.com/uber-go/automaxprocs --- go.mod | 3 ++- go.sum | 9 ++++++++- main.go | 25 ++++++++++++++----------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 4f858c7..5e30c76 100644 --- a/go.mod +++ b/go.mod @@ -11,11 +11,12 @@ require ( github.com/orcaman/concurrent-map v1.0.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.12.2 - github.com/stretchr/testify v1.7.0 + github.com/stretchr/testify v1.7.5 github.com/twmb/franz-go v1.6.0 github.com/twmb/franz-go/pkg/kmsg v1.1.0 github.com/twmb/franz-go/pkg/sasl/kerberos v1.0.0 go.uber.org/atomic v1.9.0 + go.uber.org/automaxprocs v1.5.1 go.uber.org/zap v1.21.0 golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f ) diff --git a/go.sum b/go.sum index 90ae237..37f9317 100644 --- a/go.sum +++ b/go.sum @@ -269,6 +269,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= @@ -304,12 +305,16 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q= +github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/twmb/franz-go v1.5.0/go.mod h1:ZKQ5AtqBbdc783bLCay7nDc21lJnIIA8mFJYhLMF19E= github.com/twmb/franz-go v1.6.0 h1:yri7YsVBe/k1LKcoZSLILgUI3U14e82qtD9i4VOcs9c= github.com/twmb/franz-go v1.6.0/go.mod h1:xdMwpUIQL/JDKKwerc5qJQG8TU1SNIddfjKJJyqRJIg= @@ -332,6 +337,8 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk= +go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= diff --git a/main.go b/main.go index 87f5685..47adcda 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,6 @@ package main import ( "context" "fmt" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "github.com/cloudhut/kminion/v2/e2e" "github.com/cloudhut/kminion/v2/kafka" "github.com/cloudhut/kminion/v2/logging" @@ -16,7 +10,13 @@ import ( "github.com/cloudhut/kminion/v2/prometheus" promclient "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "go.uber.org/automaxprocs/maxprocs" "go.uber.org/zap" + "net" + "net/http" + "os" + "os/signal" + "strconv" ) var ( @@ -42,14 +42,17 @@ func main() { if err != nil { startupLogger.Fatal("failed to parse config", zap.Error(err)) } - logger := logging.NewLogger(cfg.Logger, cfg.Exporter.Namespace).Named("main") - if err != nil { - startupLogger.Fatal("failed to create new logger", zap.Error(err)) - } - logger.Info("started kminion", zap.String("version", version), zap.String("built_at", builtAt)) + // set GOMAXPROCS automatically + l := func(format string, a ...interface{}) { + logger.Info(fmt.Sprintf(format, a...)) + } + if _, err = maxprocs.Set(maxprocs.Logger(l)); err != nil { + logger.Fatal("failed to set GOMAXPROCS automatically", zap.Error(err)) + } + // Setup context that cancels when the application receives an interrupt signal ctx, cancel := context.WithCancel(context.Background()) c := make(chan os.Signal, 1)