Skip to content

Instantly share code, notes, and snippets.

@phosae
Last active December 30, 2022 09:36
Show Gist options
  • Save phosae/fb7000b995f62aa3aa1fc15520a0d3c0 to your computer and use it in GitHub Desktop.
Save phosae/fb7000b995f62aa3aa1fc15520a0d3c0 to your computer and use it in GitHub Desktop.
using uber automaxprocs to set GOMAXPROCS correctly in container context
package main
import (
"fmt"
"runtime"
"go.uber.org/automaxprocs/maxprocs"
)
func main() {
fmt.Printf("direct runtime.NumCPU: %d\n", runtime.NumCPU())
fmt.Printf("direct runtime.GOMAXPROCS(0): %d\n", runtime.GOMAXPROCS(0))
maxprocs.Set(maxprocs.Logger(func(s string, i ...interface{}) {
fmt.Printf(s, i...)
fmt.Println()
}))
fmt.Printf("after uber maxprocs.Set, runtime.NumCPU: %d\n", runtime.NumCPU())
fmt.Printf("after uber maxprocs.Set, runtime.GOMAXPROCS(0): %d\n", runtime.GOMAXPROCS(0))
}
// # docker run --rm --cpus 2 zengxu/goctrlimit
// direct runtime.NumCPU: 8
// direct runtime.GOMAXPROCS(0): 8
// maxprocs: Updating GOMAXPROCS=2: determined from CPU quota
// after uber maxprocs.Set, runtime.NumCPU: 8
// after uber maxprocs.Set, runtime.GOMAXPROCS(0): 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment