$ show KimMachineGun/automemlimit
This is a small, focused Go library that automatically sets the GOMEMLIMIT environment variable for Go applications based on the Linux cgroup memory limit. It provides a simple side-effect import (import _ "github.com/KimMachineGun/automemlimit") that configures the Go runtime memory limit to a configurable fraction (default 90%) of the cgroup limit, or alternatively offers a programmatic API via memlimit.Set() with options for custom providers, ratios, minimum limits, and periodic refresh. The library handles both cgroup v1 and v2, correctly resolving the effective memory limit from the cgroup hierarchy, including handling of conflicting mounts, unlimited memory sentinels, and fallback to system memory. It is not a hybrid; it is purely an application library.
The LLM identified 2 files worth highlighting for reuse:
Core module that reads the cgroup memory limit from /proc/self/mountinfo and /proc/self/cgroup. It handles both cgroup v1 and v2, selects the appropriate memory controller, walks the cgroup hierarchy for v2 to find the effective (minimum) limit, parses memory.stat for v1 hierarchical limits, and correctly detects unlimited memory sentinels. It also resolves conflicting mount points by selecting the one closest to the root. This component is self-contained (only standard library) and demonstrates deep understanding of Linux cgroup internals, making it highly reusable as a standalone library for querying cgroup memory limits.
Defines the Provider type (func() (uint64, error)) and functional combinators: Limit (constant provider), ApplyRatio (applies a fraction), and ApplyFallback (chains providers). This is a clean, reusable pattern for composing memory limit sources. It demonstrates good software engineering practices by separating concerns and enabling flexible composition without coupling to specific implementations.