Skip to content

Commit b08e27a

Browse files
committed
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
1 parent 6ce4b90 commit b08e27a

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ require (
1111
github.com/orcaman/concurrent-map v1.0.0
1212
github.com/pkg/errors v0.9.1
1313
github.com/prometheus/client_golang v1.12.2
14-
github.com/stretchr/testify v1.7.0
14+
github.com/stretchr/testify v1.7.5
1515
github.com/twmb/franz-go v1.6.0
1616
github.com/twmb/franz-go/pkg/kmsg v1.1.0
1717
github.com/twmb/franz-go/pkg/sasl/kerberos v1.0.0
1818
go.uber.org/atomic v1.9.0
19+
go.uber.org/automaxprocs v1.5.1
1920
go.uber.org/zap v1.21.0
2021
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
2122
)

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
269269
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
270270
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
271271
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
272+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
272273
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
273274
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
274275
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=
304305
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
305306
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
306307
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
308+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
307309
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
308310
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
309311
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
310312
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
311-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
312313
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
314+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
315+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
316+
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
317+
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
313318
github.com/twmb/franz-go v1.5.0/go.mod h1:ZKQ5AtqBbdc783bLCay7nDc21lJnIIA8mFJYhLMF19E=
314319
github.com/twmb/franz-go v1.6.0 h1:yri7YsVBe/k1LKcoZSLILgUI3U14e82qtD9i4VOcs9c=
315320
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=
332337
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
333338
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
334339
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
340+
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
341+
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
335342
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
336343
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
337344
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=

main.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"net"
7-
"net/http"
8-
"os"
9-
"os/signal"
10-
"strconv"
11-
126
"github.com/cloudhut/kminion/v2/e2e"
137
"github.com/cloudhut/kminion/v2/kafka"
148
"github.com/cloudhut/kminion/v2/logging"
159
"github.com/cloudhut/kminion/v2/minion"
1610
"github.com/cloudhut/kminion/v2/prometheus"
1711
promclient "github.com/prometheus/client_golang/prometheus"
1812
"github.com/prometheus/client_golang/prometheus/promhttp"
13+
"go.uber.org/automaxprocs/maxprocs"
1914
"go.uber.org/zap"
15+
"net"
16+
"net/http"
17+
"os"
18+
"os/signal"
19+
"strconv"
2020
)
2121

2222
var (
@@ -42,14 +42,17 @@ func main() {
4242
if err != nil {
4343
startupLogger.Fatal("failed to parse config", zap.Error(err))
4444
}
45-
4645
logger := logging.NewLogger(cfg.Logger, cfg.Exporter.Namespace).Named("main")
47-
if err != nil {
48-
startupLogger.Fatal("failed to create new logger", zap.Error(err))
49-
}
50-
5146
logger.Info("started kminion", zap.String("version", version), zap.String("built_at", builtAt))
5247

48+
// set GOMAXPROCS automatically
49+
l := func(format string, a ...interface{}) {
50+
logger.Info(fmt.Sprintf(format, a...))
51+
}
52+
if _, err = maxprocs.Set(maxprocs.Logger(l)); err != nil {
53+
logger.Fatal("failed to set GOMAXPROCS automatically", zap.Error(err))
54+
}
55+
5356
// Setup context that cancels when the application receives an interrupt signal
5457
ctx, cancel := context.WithCancel(context.Background())
5558
c := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)