|
| 1 | +//go:build linux || darwin || freebsd || openbsd |
| 2 | + |
| 3 | +// Package limits includes POSIX-specific limit tuning to mirror xinetd-like defaults. |
| 4 | +// Separating platforms keeps platform quirks localized and easier to maintain. |
| 5 | +package limits |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "syscall" |
| 11 | +) |
| 12 | + |
| 13 | +func collectLimitRequests(logger *log.Logger) []limitRequest { |
| 14 | + desiredOpenFiles := uint64(100000) |
| 15 | + desiredProcesses := uint64(100000) |
| 16 | + |
| 17 | + requests := []limitRequest{ |
| 18 | + buildInfinityRequest("virtual memory (rlimit_as)", syscall.RLIMIT_AS), |
| 19 | + buildInfinityRequest("CPU time (rlimit_cpu)", syscall.RLIMIT_CPU), |
| 20 | + buildTargetRequest("open files (rlimit_files)", syscall.RLIMIT_NOFILE, desiredOpenFiles, logger), |
| 21 | + } |
| 22 | + |
| 23 | + if procResource, ok := processLimitResource(); ok { |
| 24 | + requests = append(requests, buildTargetRequest("process count (rlimit_proc)", procResource, desiredProcesses, logger)) |
| 25 | + } else { |
| 26 | + logger.Printf("Process limit resource is unavailable on this platform; skipping rlimit_proc") |
| 27 | + } |
| 28 | + |
| 29 | + return requests |
| 30 | +} |
| 31 | + |
| 32 | +func buildInfinityRequest(label string, resource int) limitRequest { |
| 33 | + return limitRequest{ |
| 34 | + description: fmt.Sprintf("%s -> unlimited", label), |
| 35 | + apply: func() error { |
| 36 | + unlimited := ^uint64(0) |
| 37 | + |
| 38 | + current := &syscall.Rlimit{} |
| 39 | + if err := syscall.Getrlimit(resource, current); err != nil { |
| 40 | + return fmt.Errorf("failed reading %s: %w", label, err) |
| 41 | + } |
| 42 | + |
| 43 | + desired := &syscall.Rlimit{Cur: unlimited, Max: unlimited} |
| 44 | + if current.Cur == desired.Cur && current.Max == desired.Max { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + if err := syscall.Setrlimit(resource, desired); err != nil { |
| 49 | + return fmt.Errorf("failed setting %s to unlimited: %w", label, err) |
| 50 | + } |
| 51 | + return nil |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func buildTargetRequest(label string, resource int, target uint64, logger *log.Logger) limitRequest { |
| 57 | + return limitRequest{ |
| 58 | + description: fmt.Sprintf("%s -> %d", label, target), |
| 59 | + apply: func() error { |
| 60 | + current := &syscall.Rlimit{} |
| 61 | + if err := syscall.Getrlimit(resource, current); err != nil { |
| 62 | + return fmt.Errorf("failed reading %s: %w", label, err) |
| 63 | + } |
| 64 | + |
| 65 | + desired := &syscall.Rlimit{Cur: target, Max: target} |
| 66 | + if current.Max > desired.Max { |
| 67 | + desired.Max = current.Max |
| 68 | + } |
| 69 | + if desired.Cur > desired.Max { |
| 70 | + desired.Cur = desired.Max |
| 71 | + } |
| 72 | + |
| 73 | + if current.Cur >= desired.Cur && current.Max >= desired.Max { |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + if err := syscall.Setrlimit(resource, desired); err != nil { |
| 78 | + logger.Printf("Adjusting %s hit %v; trying best-effort with existing max", label, err) |
| 79 | + fallback := &syscall.Rlimit{Cur: desired.Cur, Max: current.Max} |
| 80 | + if fallback.Cur > fallback.Max { |
| 81 | + fallback.Cur = fallback.Max |
| 82 | + } |
| 83 | + if setErr := syscall.Setrlimit(resource, fallback); setErr != nil { |
| 84 | + return fmt.Errorf("failed setting %s even after fallback: %w", label, setErr) |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
0 commit comments