Use rlimit to inspect resource limits or constrain a worker before it starts
its workload. For example, a supervisor can start an os/exec.Cmd whose child
waits on a pipe, set its descriptor limit, and then release the child.
worker := rlimit.ProcessID(cmd.Process.Pid)
current, err := worker.Get(rlimit.OpenFiles)
if err != nil {
return err
}
previous, err := worker.Swap(rlimit.OpenFiles, rlimit.Limits{
Soft: min(current.Soft, 256),
Hard: current.Hard,
})
if err != nil {
return err
}
fmt.Println(previous.Soft)
Use package-level Get and Swap to address the calling process. See the
executable examples for self-inspection and a
worker’s explicit hard-limit reduction.
Swap replaces the pair and returns its previous value in one kernel
operation. If the new pair depends on an earlier Get, coordinate other
writers across both calls. Limits{} requests two zero values. Use
rlimit.Unlimited for the native maximum unsigned 64-bit sentinel. Linux
rejects an unknown resource or a soft limit above its hard limit with
EINVAL. Errors preserve errno through errors.Is.
Limits are shared by all threads and inherited across fork and retained at
exec. Some resources account for the real user ID across processes. Match both
the target’s real, effective, and saved UIDs and GIDs using the caller’s real
IDs, or hold CAP_SYS_RESOURCE in the target’s user namespace. Linux can
also impose LSM restrictions. A hard-limit reduction can be permanent for
an unprivileged caller. Raising a hard limit requires CAP_SYS_RESOURCE in
the initial user namespace. Resource ceilings still apply, including the
kernel’s fs.nr_open maximum for the hard descriptor limit.
Linux resource limits.
Keep the target alive and coordinate its reaping while applying limits. A
ProcessID is resolved in the caller’s PID namespace and can be reused.
Linux can report ESRCH for a missing target or EPERM for a denied
operation. Invalid explicit IDs match os.ErrInvalid. For pre-workload
enforcement, have the child set its own limit or wait for the supervisor.
Go can raise the soft descriptor limit during its own startup. Apply a soft
limit to a Go worker after startup, or use a hard limit to cap the startup
adjustment. A successful self-update of OpenFiles uses Go’s explicit limit
behavior, so future standard-library exec children inherit the effective
limit. A supervisor’s update targets the worker’s kernel values, while the
worker’s Go runtime still owns its own exec behavior.
| Units | Resources |
|---|---|
| Bytes | FileSize, DataSize, StackSize, CoreSize, ResidentSetSize, LockedMemory, AddressSpace, MessageQueueBytes |
| Counts | Processes (tasks, including threads), OpenFiles (one more than the maximum descriptor number), FileLocks, PendingSignals |
| Seconds | CPUTime |
| Microseconds | RealtimeCPUTime |
| Linux priority values | NicePriority (the floor is 20 minus the limit), RealtimePriority |
Linux 7.0 retains ResidentSetSize and FileLocks as queryable settings
without enforcing them. The package covers the 16 Linux 7.0 resource IDs.
Use cgroup for hierarchy-wide workload controls.