syncthing: add GC and thread control variables

Add Go GC and threading control variables - GOGC, GOMEMLIMIT, and
GOMAXPROCS - to all services to allow more granular control of the
memory management on lower memory devices.

Link: https://go.dev/doc/gc-guide#GOGC
Link: https://pkg.go.dev/runtime#pkg-overview
Link: https://go.dev/blog/container-aware-gomaxprocs
Signed-off-by: George Sapkin <george@sapk.in>
This commit is contained in:
George Sapkin
2026-03-13 23:24:56 +02:00
committed by Hannu Nyman
parent cde9e3b668
commit 6accad3bb3
6 changed files with 73 additions and 16 deletions

View File

@@ -17,3 +17,16 @@ config stdiscosrv 'stdiscosrv'
# option compression '0'
# option debug '0'
# option http '1'
# Advanced options. Modify at your own risk.
# More info: https://go.dev/doc/gc-guide#GOGC
# option gc '0'
# Max number of OS threads to use
# 0 to match the number of CPUs (default)
# >0 to explicitly specify concurrency
# option maxprocs '0'
# Soft memory limit in MB, 0 to disable
# option memlimit '0'

View File

@@ -1,4 +1,6 @@
#!/bin/sh /etc/rc.common
#
# shellcheck disable=SC3043 # ash supports local
START=90
STOP=10
@@ -35,9 +37,12 @@ start_service() {
local db_dir="$conf_dir/discovery.db"
local db_flush_interval=''
local debug=0
local gc=0
local http=0
local key="$conf_dir/key.pem"
local listen=':8443'
local maxprocs=0
local memlimit=0
local metrics_listen=''
local nice=0
local user='syncthing'
@@ -54,6 +59,11 @@ start_service() {
procd_open_instance
procd_set_param command "$PROG"
[ "$gc" -le 0 ] || procd_append_param env GOGC="$gc"
[ "$maxprocs" -le 0 ] || procd_append_param env GOMAXPROCS="$maxprocs"
[ "$memlimit" -le 0 ] || procd_append_param env GOMEMLIMIT="$memlimit"
procd_append_param command --cert="$cert"
[ "$compression" -eq 0 ] || \
procd_append_param command --compression

View File

@@ -27,3 +27,16 @@ config strelaysrv 'strelaysrv'
# option debug '0'
# option nat '0'
# option pprof '0'
# Advanced options. Modify at your own risk.
# More info: https://go.dev/doc/gc-guide#GOGC
# option gc '0'
# Max number of OS threads to use
# 0 to match the number of CPUs (default)
# >0 to explicitly specify concurrency
# option maxprocs '0'
# Soft memory limit in MB, 0 to disable
# option memlimit '0'

View File

@@ -1,4 +1,6 @@
#!/bin/sh /etc/rc.common
#
# shellcheck disable=SC3043 # ash supports local
START=90
STOP=10
@@ -30,9 +32,12 @@ start_service() {
local enabled=0
local debug=0
local ext_address=''
local gc=0
local global_rate=''
local keys='/etc/strelaysrv'
local listen=':22067'
local maxprocs=0
local memlimit=0
local message_timeout=''
local nat=0
local nat_lease=''
@@ -64,8 +69,12 @@ start_service() {
procd_open_instance
procd_set_param command "$PROG"
[ "$debug" -eq 0 ] || \
procd_append_param command -debug
[ "$gc" -le 0 ] || procd_append_param env GOGC="$gc"
[ "$maxprocs" -le 0 ] || procd_append_param env GOMAXPROCS="$maxprocs"
[ "$memlimit" -le 0 ] || procd_append_param env GOMEMLIMIT="$memlimit"
[ "$debug" -eq 0 ] || procd_append_param command -debug
[ -z "$ext_address" ] || \
procd_append_param command -ext-address="$ext_address"
[ -z "$global_rate" ] || \

View File

@@ -14,10 +14,6 @@ config syncthing 'syncthing'
# "restart": the service has to be stopped/started for those to take effect.
option nice '19'
# 0 to match the number of CPUs (default)
# >0 to explicitly specify concurrency
option maxprocs '0'
# More info: https://docs.syncthing.net/users/syncthing.html
# option db_delete_retention_interval ''
# option db_maintenance_interval ''
@@ -31,3 +27,16 @@ config syncthing 'syncthing'
option log_max_old_files '7'
# Size in bytes
option log_max_size '1048576'
# Advanced options. Modify at your own risk.
# More info: https://go.dev/doc/gc-guide#GOGC
# option gc '0'
# Max number of OS threads to use
# 0 to match the number of CPUs (default)
# >0 to explicitly specify concurrency
# option maxprocs '0'
# Soft memory limit in MB, 0 to disable
# option memlimit '0'

View File

@@ -1,4 +1,6 @@
#!/bin/sh /etc/rc.common
#
# shellcheck disable=SC3043 # ash supports local
START=90
STOP=10
@@ -41,6 +43,7 @@ start_service() {
local enabled=0
local db_delete_retention_interval=''
local db_maintenance_interval=''
local gc=0
local gui_address='http://0.0.0.0:8384'
local gui_apikey=''
local home='/etc/syncthing'
@@ -49,29 +52,28 @@ start_service() {
local log_max_old_files=7
local log_max_size=1048576
local maxprocs=0
local memlimit=0
local nice=0
local user='syncthing'
config_load 'syncthing'
local group=$(id -gn $user)
[ "$enabled" -gt 0 ] || return 0
local group=$(id -gn $user)
mkdir -p "$home"
# A separate step to handle an upgrade use case
[ -d "$home" ] && chown -R $user:$group "$home"
# Changes to "niceness"/maxprocs are not picked up either by reload_config
# or by restart: the service has to be stopped/started for it to take effect
if [ $maxprocs -le 0 ]; then
# Default to the number of cores in this case
maxprocs=$(grep -c ^processor /proc/cpuinfo)
fi
procd_open_instance
procd_set_param command "$PROG"
procd_set_param env GOMAXPROCS="$maxprocs"
[ "$gc" -le 0 ] || procd_append_param env GOGC="$gc"
[ "$maxprocs" -le 0 ] || procd_append_param env GOMAXPROCS="$maxprocs"
[ "$memlimit" -le 0 ] || procd_append_param env GOMEMLIMIT="$memlimit"
procd_append_param command serve
[ -z "$db_delete_retention_interval" ] || \
procd_append_param command --db-delete-retention-interval="$db_delete_retention_interval"
@@ -100,6 +102,7 @@ start_service() {
procd_set_param respawn
procd_set_param stdout 0
procd_set_param stderr 1
procd_close_instance
}