### Install automaxprocs Go Package Source: https://github.com/uber-go/automaxprocs/blob/master/README.md This command installs the automaxprocs Go package using the go get command. It ensures that the latest version of the package is downloaded and available for use in your Go projects. This is a prerequisite for utilizing the package's functionality. ```bash go get -u go.uber.org/automaxprocs ``` -------------------------------- ### Combining GOMAXPROCS Options in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Configure GOMAXPROCS using multiple options simultaneously for comprehensive control. This example combines custom logging, a minimum GOMAXPROCS value, and a standard rounding function. It demonstrates how to set up a robust configuration for an application starting up. ```go package main import ( "log" "math" "os" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { logger := log.New(os.Stderr, "[maxprocs] ", log.LstdFlags) undo, err := maxprocs.Set( maxprocs.Logger(logger.Printf), maxprocs.Min(2), maxprocs.RoundQuotaFunc(func(v float64) int { return int(math.Round(v)) }), ) if err != nil { logger.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() logger.Printf("Application starting with GOMAXPROCS=%d", runtime.GOMAXPROCS(0)) // Run your application startServer() } func startServer() { // Application implementation } ``` -------------------------------- ### Initialize automaxprocs in Go Application Source: https://github.com/uber-go/automaxprocs/blob/master/README.md This Go code snippet demonstrates how to integrate the automaxprocs package into an application. By importing the package as a blank identifier (`_`), its initialization logic runs automatically when the program starts. This ensures that GOMAXPROCS is set according to the container's CPU quota without explicit calls in your application code. ```go import _ "go.uber.org/automaxprocs" func main() { // Your application logic here. } ``` -------------------------------- ### Setting Minimum GOMAXPROCS in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Ensure GOMAXPROCS does not fall below a specified minimum value using the maxprocs.Min() option. This is useful when a certain level of concurrency is required regardless of the container's CPU allocation. The example sets a minimum of 2 GOMAXPROCS, overriding smaller CPU quotas. ```go package main import ( "log" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Even if CPU quota suggests 1 core, use at least 2 undo, err := maxprocs.Set( maxprocs.Logger(log.Printf), maxprocs.Min(2), ) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // GOMAXPROCS will be at least 2 log.Printf("GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) // Application logic } ``` -------------------------------- ### Custom Rounding Function for GOMAXPROCS in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Define a custom rounding function for fractional CPU quotas using maxprocs.RoundQuotaFunc(). This allows precise control over how the library converts the CPU quota (e.g., 1.5 CPUs) into an integer GOMAXPROCS value. The example uses math.Ceil to always round up. ```go package main import ( "log" "math" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Use ceiling instead of default rounding ceilRound := func(v float64) int { return int(math.Ceil(v)) } undo, err := maxprocs.Set( maxprocs.Logger(log.Printf), maxprocs.RoundQuotaFunc(ceilRound), ) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // With CPU quota 1.5: // Default rounding: GOMAXPROCS=2 (rounds to nearest) // Ceiling rounding: GOMAXPROCS=2 (rounds up) // Floor rounding would give: GOMAXPROCS=1 (rounds down) log.Printf("GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) } ``` -------------------------------- ### Combining Multiple Options Source: https://context7.com/uber-go/automaxprocs/llms.txt Use multiple configuration options together in a single call to `maxprocs.Set()` for fine-grained control over GOMAXPROCS setting. ```APIDOC ## Combining Multiple Options ### Description Use multiple configuration options together in a single call to `maxprocs.Set()` for fine-grained control over GOMAXPROCS setting. ### Method `maxprocs.Set(option1, option2, ...)` ### Endpoint N/A (Function Call) ### Parameters Accepts any valid options for `maxprocs.Set()`, such as `Logger`, `Min`, `RoundQuotaFunc`, etc. ### Request Example ```go package main import ( "log" "math" "os" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { logger := log.New(os.Stderr, "[maxprocs] ", log.LstdFlags) undo, err := maxprocs.Set( maxprocs.Logger(logger.Printf), maxprocs.Min(2), maxprocs.RoundQuotaFunc(func(v float64) int { return int(math.Round(v)) }), ) if err != nil { logger.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() logger.Printf("Application starting with GOMAXPROCS=%d", runtime.GOMAXPROCS(0)) // Run your application startServer() } func startServer() { // Application implementation } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Automatic GOMAXPROCS Configuration in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Automatically configure GOMAXPROCS by importing the package with a blank identifier. This sets GOMAXPROCS during package initialization based on container CPU quotas. No explicit function calls are needed after import. The code demonstrates setting up an HTTP server that reports the configured GOMAXPROCS. ```go package main import ( _ "go.uber.org/automaxprocs" "fmt" "net/http" "runtime" ) func main() { // GOMAXPROCS is already set based on CPU quota fmt.Printf("Running with GOMAXPROCS=%d\n", runtime.GOMAXPROCS(0)) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) }) http.ListenAndServe(":8080", nil) } ``` -------------------------------- ### Automatic GOMAXPROCS Configuration Source: https://context7.com/uber-go/automaxprocs/llms.txt Import the package with a blank identifier to automatically configure GOMAXPROCS during package initialization. This is the simplest way to enable the optimization. ```APIDOC ## Automatic GOMAXPROCS Configuration ### Description Import the package with a blank identifier to automatically configure GOMAXPROCS during package initialization. This is the simplest way to enable the optimization. ### Method Import (Blank Identifier) ### Endpoint N/A (Package Initialization) ### Parameters None ### Request Example ```go package main import ( _ "go.uber.org/automaxprocs" "fmt" "net/http" "runtime" ) func main() { // GOMAXPROCS is already set based on CPU quota fmt.Printf("Running with GOMAXPROCS=%d\n", runtime.GOMAXPROCS(0)) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) }) http.ListenAndServe(":8080", nil) } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Configuration with Logging Source: https://context7.com/uber-go/automaxprocs/llms.txt Enable debug logging by providing a logger function to `maxprocs.Set()`. This helps in understanding how the GOMAXPROCS value is determined. ```APIDOC ## Configuration with Logging ### Description Enable debug logging by providing a logger function to `maxprocs.Set()`. This helps in understanding how the GOMAXPROCS value is determined. ### Method `maxprocs.Set(maxprocs.Logger(logger_function))` ### Endpoint N/A (Function Call) ### Parameters - **Logger** (func(string, ...interface{})) - A function that accepts format strings and arguments, similar to `log.Printf`. ### Request Example ```go package main import ( "log" "os" "go.uber.org/automaxprocs/maxprocs" ) func main() { logger := log.New(os.Stdout, "[automaxprocs] ", log.LstdFlags) undo, err := maxprocs.Set(maxprocs.Logger(logger.Printf)) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // Logs output like: // [automaxprocs] maxprocs: Updating GOMAXPROCS=2: determined from CPU quota // Application continues } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Go: Set GOMAXPROCS respecting environment variable Source: https://context7.com/uber-go/automaxprocs/llms.txt This Go code snippet demonstrates how the automaxprocs library respects the GOMAXPROCS environment variable if it's already set. It logs a message indicating if the variable is set and then attempts to configure GOMAXPROCS using the library, deferring to the environment if present. The undo function is used to clean up the configuration. ```go package main import ( "log" "os" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // If GOMAXPROCS env var is set, automaxprocs won't override it if val := os.Getenv("GOMAXPROCS"); val != "" { log.Printf("GOMAXPROCS already set to %s via environment") } undo, err := maxprocs.Set(maxprocs.Logger(log.Printf)) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // If GOMAXPROCS was set, logs: // maxprocs: Honoring GOMAXPROCS="4" as set in environment log.Printf("Running with GOMAXPROCS=%d", runtime.GOMAXPROCS(0)) } ``` -------------------------------- ### GOMAXPROCS Configuration with Debug Logging in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Enable debug logging during GOMAXPROCS configuration by providing a logger function to maxprocs.Set(). This helps in understanding how the library determines the GOMAXPROCS value from CPU quotas. The logger function should accept a format string and arguments, similar to log.Printf. ```go package main import ( "log" "os" "go.uber.org/automaxprocs/maxprocs" ) func main() { logger := log.New(os.Stdout, "[automaxprocs] ", log.LstdFlags) undo, err := maxprocs.Set(maxprocs.Logger(logger.Printf)) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // Logs output like: // [automaxprocs] maxprocs: Updating GOMAXPROCS=2: determined from CPU quota // Application continues } ``` -------------------------------- ### Manual Configuration with maxprocs.Set() Source: https://context7.com/uber-go/automaxprocs/llms.txt Explicitly control when GOMAXPROCS is set and handle errors programmatically using the `maxprocs.Set()` function. This function returns an `undo` function to restore the original GOMAXPROCS value. ```APIDOC ## Manual Configuration with maxprocs.Set() ### Description Explicitly control when GOMAXPROCS is set and handle errors programmatically using the `maxprocs.Set()` function. This function returns an `undo` function to restore the original GOMAXPROCS value. ### Method `maxprocs.Set()` ### Endpoint N/A (Function Call) ### Parameters None (for basic usage) ### Request Example ```go package main import ( "log" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Set GOMAXPROCS and get an undo function undo, err := maxprocs.Set() if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() log.Printf("GOMAXPROCS set to %d", runtime.GOMAXPROCS(0)) // Application logic here // When undo() is called on defer, GOMAXPROCS is restored } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Manual GOMAXPROCS Configuration with maxprocs.Set() in Go Source: https://context7.com/uber-go/automaxprocs/llms.txt Manually control GOMAXPROCS configuration and error handling using the maxprocs.Set() function. This provides explicit control over when GOMAXPROCS is adjusted and allows for programmatic error management. The function returns an 'undo' function to restore the original GOMAXPROCS value, typically used with defer. ```go package main import ( "log" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Set GOMAXPROCS and get an undo function undo, err := maxprocs.Set() if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() log.Printf("GOMAXPROCS set to %d", runtime.GOMAXPROCS(0)) // Application logic here // When undo() is called on defer, GOMAXPROCS is restored } ``` -------------------------------- ### Setting Minimum GOMAXPROCS Source: https://context7.com/uber-go/automaxprocs/llms.txt Ensure GOMAXPROCS never falls below a specified minimum value by using the `maxprocs.Min()` option in `maxprocs.Set()`. ```APIDOC ## Setting Minimum GOMAXPROCS ### Description Ensure GOMAXPROCS never falls below a specified minimum value by using the `maxprocs.Min()` option in `maxprocs.Set()`. ### Method `maxprocs.Set(maxprocs.Min(minimum_value))` ### Endpoint N/A (Function Call) ### Parameters - **Min** (int) - The minimum value for GOMAXPROCS. ### Request Example ```go package main import ( "log" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Even if CPU quota suggests 1 core, use at least 2 undo, err := maxprocs.Set( maxprocs.Logger(log.Printf), maxprocs.Min(2), ) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // GOMAXPROCS will be at least 2 log.Printf("GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) // Application logic } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` -------------------------------- ### Custom Rounding Function Source: https://context7.com/uber-go/automaxprocs/llms.txt Control how fractional CPU quotas are converted to integer GOMAXPROCS values by providing a custom rounding function using `maxprocs.RoundQuotaFunc()`. ```APIDOC ## Custom Rounding Function ### Description Control how fractional CPU quotas are converted to integer GOMAXPROCS values by providing a custom rounding function using `maxprocs.RoundQuotaFunc()`. ### Method `maxprocs.Set(maxprocs.RoundQuotaFunc(rounding_function))` ### Endpoint N/A (Function Call) ### Parameters - **RoundQuotaFunc** (func(float64) int) - A function that takes a float64 (CPU quota) and returns an int (GOMAXPROCS value). ### Request Example ```go package main import ( "log" "math" "runtime" "go.uber.org/automaxprocs/maxprocs" ) func main() { // Use ceiling instead of default rounding ceilRound := func(v float64) int { return int(math.Ceil(v)) } undo, err := maxprocs.Set( maxprocs.Logger(log.Printf), maxprocs.RoundQuotaFunc(ceilRound), ) if err != nil { log.Fatalf("failed to set GOMAXPROCS: %v", err) } defer undo() // With CPU quota 1.5: // Default rounding: GOMAXPROCS=2 (rounds to nearest) // Ceiling rounding: GOMAXPROCS=2 (rounds up) // Floor rounding would give: GOMAXPROCS=1 (rounds down) log.Printf("GOMAXPROCS: %d", runtime.GOMAXPROCS(0)) } ``` ### Response #### Success Response (N/A) N/A #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.