#!/bin/sh
# agh-launch — start AdGuardHome with a RAM-relative Go soft memory limit.
#
# Loading a large blocklist transiently allocates ~2x the steady footprint (Go
# parses the whole filter, then GC reclaims). Left unbounded that spike can OOM a
# small box. A GOMEMLIMIT at ~50% of box RAM makes Go's GC work harder as the heap
# approaches it, capping the load peak near steady without starving a big box. It
# is a SOFT limit: Go still exceeds it rather than dying if the live set genuinely
# needs more, so correctness is unaffected. Honour an operator-set value.
RAM_MB=$(awk '/MemTotal:/{print int($2/1024)}' /proc/meminfo 2>/dev/null)
[ -n "$RAM_MB" ] || RAM_MB=2048
[ -n "$GOMEMLIMIT" ] || export GOMEMLIMIT="$((RAM_MB * 50 / 100))MiB"
exec /usr/local/sbin/AdGuardHome "$@"
