Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: set GOMAXPROCS env var during Set #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions maxprocs/maxprocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package maxprocs // import "go.uber.org/automaxprocs/maxprocs"
import (
"os"
"runtime"
"strconv"

iruntime "go.uber.org/automaxprocs/internal/runtime"
)
Expand Down Expand Up @@ -126,5 +127,7 @@ func Set(opts ...Option) (func(), error) {
}

runtime.GOMAXPROCS(maxProcs)

_ = os.Setenv("GOMAXPROCS", strconv.Itoa(maxProcs))
return undo, nil
}
7 changes: 6 additions & 1 deletion maxprocs/maxprocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ func TestSet(t *testing.T) {

t.Run("EnvVarPresent", func(t *testing.T) {
withMax(t, 42, func() {
assert.Equal(t, "", os.Getenv(_maxProcsKey), "shouldn't have GOMAXPROCS env var")
prev := currentMaxProcs()
undo, err := Set()
defer undo()
require.NoError(t, err, "Set failed")
assert.Equal(t, prev, currentMaxProcs(), "shouldn't alter GOMAXPROCS")

after := currentMaxProcs()
assert.Equal(t, prev, after, "shouldn't alter GOMAXPROCS")
assert.Equal(t, strconv.Itoa(after), os.Getenv(_maxProcsKey), "should have GOMAXPROCS env var")
_ = os.Unsetenv(_maxProcsKey)
})
})

Expand Down