Skip to content

Commit 1c36d9d

Browse files
authored
Automatically set GOMAXPROCS to match Linux container CPU quota (#16)
* Automatically set GOMAXPROCS to match Linux container CPU quota via uber-go/automaxprocs
1 parent 7c6f8d2 commit 1c36d9d

7 files changed

Lines changed: 31 additions & 5 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ linters:
1313
- goconst
1414
# - prealloc
1515
- revive
16+
- stylecheck
1617
- unconvert
1718
# - unparam
1819

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 0.11.0
4+
5+
- Key changes:
6+
- Automatically set `GOMAXPROCS` to match Linux container CPU quota via [uber-go/automaxprocs](https://github.com/uber-go/automaxprocs). Enabled by default, can be turned off via `SET_GOMAXPROCS: false`.
7+
38
## 0.10.0
49

510
- Key changes:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Docker images are published on [ghcr.io/weisdd/lfgw](https://github.com/weisdd/l
4040
| | `ASSUMED_ROLES` | `false` | In environments, where OIDC-role names match names of namespaces, ACLs can be constructed on the fly (e.g. `["role1", "role2"]` will give access to metrics from namespaces `role1` and `role2`). The roles specified in `acl.yaml` are still considered and get merged with assumed roles. Role names may contain regular expressions, including the admin definition `.*`. |
4141
| | `ENABLE_DEDUPLICATION` | `true` | Whether to enable deduplication, which leaves some of the requests unmodified if they match the target policy. Examples can be found in the "acl.yaml syntax" section. |
4242
| | `OPTIMIZE_EXPRESSIONS` | `true` | Whether to automatically optimize expressions for non-full access requests. [More details](https://pkg.go.dev/github.com/VictoriaMetrics/metricsql#Optimize) |
43+
| | `SET_GOMAXPROCS` | `true` | Automatically set `GOMAXPROCS` to match Linux container CPU quota. |
4344
| | | | |
4445
| **Logging** | | | |
4546
| | `DEBUG` | `false` | Whether to print out debug log messages. |

cmd/lfgw/acl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (app *application) rolesToRawACL(roles []string) (string, error) {
163163
} else {
164164
// NOTE: Strictly speaking, it's getACL who is expected to pass a filtered list of roles in case Assumed roles are disabled. The error check below is not necessary, is left as an additional safeguard for now and might get removed in the future.
165165
if !app.AssumedRoles {
166-
return "", fmt.Errorf("Some of the roles are unknown and assumed roles are not enabled")
166+
return "", fmt.Errorf("some of the roles are unknown and assumed roles are not enabled")
167167
}
168168
// NOTE: Role names are not linted, so they may contain regular expressions, including the admin definition: .*
169169
rawACLs = append(rawACLs, role)
@@ -172,7 +172,7 @@ func (app *application) rolesToRawACL(roles []string) (string, error) {
172172

173173
rawACL := strings.Join(rawACLs, ", ")
174174
if rawACL == "" {
175-
return "", fmt.Errorf("Constructed empty rawACL")
175+
return "", fmt.Errorf("constructed empty rawACL")
176176
}
177177

178178
return rawACL, nil

cmd/lfgw/main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"net/http/httputil"
77
"net/url"
88
"os"
9+
"runtime"
910
"time"
1011

1112
"github.com/caarlos0/env/v6"
1213
oidc "github.com/coreos/go-oidc/v3/oidc"
1314
"github.com/rs/zerolog"
1415
zlog "github.com/rs/zerolog/log"
16+
"go.uber.org/automaxprocs/maxprocs"
1517
)
1618

1719
// Define an application struct to hold the application-wide dependencies for the
@@ -30,7 +32,8 @@ type application struct {
3032
OptimizeExpressions bool `env:"OPTIMIZE_EXPRESSIONS" envDefault:"true"`
3133
EnableDeduplication bool `env:"ENABLE_DEDUPLICATION" envDefault:"true"`
3234
SafeMode bool `env:"SAFE_MODE" envDefault:"true"`
33-
SetProxyHeaders bool `env:"SET_PROXY_HEADERS" envDefefault:"false"`
35+
SetProxyHeaders bool `env:"SET_PROXY_HEADERS" envDefault:"false"`
36+
SetGomaxProcs bool `env:"SET_GOMAXPROCS" envDefault:"true"`
3437
ACLPath string `env:"ACL_PATH" envDefault:"./acl.yaml"`
3538
AssumedRoles bool `env:"ASSUMED_ROLES" envDefault:"false"`
3639
OIDCRealmURL string `env:"OIDC_REALM_URL,required"`
@@ -75,6 +78,17 @@ func main() {
7578
zerolog.SetGlobalLevel(zerolog.DebugLevel)
7679
}
7780

81+
if app.SetGomaxProcs {
82+
undo, err := maxprocs.Set()
83+
defer undo()
84+
if err != nil {
85+
app.logger.Error().Caller().
86+
Msgf("failed to set GOMAXPROCS: %v", err)
87+
}
88+
}
89+
app.logger.Info().Caller().
90+
Msgf("Runtime settings: GOMAXPROCS = %d", runtime.GOMAXPROCS(0))
91+
7892
if app.AssumedRoles {
7993
app.logger.Info().Caller().
8094
Msg("Assumed roles mode is on")

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ require (
1010
github.com/gorilla/mux v1.8.0
1111
github.com/rs/zerolog v1.26.1
1212
github.com/stretchr/testify v1.7.1
13+
go.uber.org/automaxprocs v1.5.1
1314
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1415
)
1516

1617
require (
17-
github.com/davecgh/go-spew v1.1.0 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
1819
github.com/golang/protobuf v1.4.3 // indirect
1920
github.com/pmezard/go-difflib v1.0.0 // indirect
2021
github.com/rs/xid v1.3.0 // indirect

go.sum

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
4848
github.com/coreos/go-oidc/v3 v3.1.0 h1:6avEvcdvTa1qYsOZ6I5PRkSYHzpTNWgKYmaJfaYbrRw=
4949
github.com/coreos/go-oidc/v3 v3.1.0/go.mod h1:rEJ/idjfUyfkBit1eI1fvyr+64/g9dcKpAm8MJMesvo=
5050
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
51-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
5251
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
52+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
53+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5354
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5455
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5556
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -122,6 +123,7 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
122123
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
123124
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
124125
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
126+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
125127
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
126128
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
127129
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
@@ -146,6 +148,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
146148
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
147149
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
148150
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
151+
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
152+
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
149153
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
150154
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
151155
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

0 commit comments

Comments
 (0)