syncthing: add GC and thread control variables
authorGeorge Sapkin <redacted>
Fri, 13 Mar 2026 21:24:56 +0000 (23:24 +0200)
committerHannu Nyman <redacted>
Sat, 21 Mar 2026 13:49:07 +0000 (15:49 +0200)
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 <redacted>
utils/syncthing/files/stdiscosrv.conf
utils/syncthing/files/stdiscosrv.init
utils/syncthing/files/strelaysrv.conf
utils/syncthing/files/strelaysrv.init
utils/syncthing/files/syncthing.conf
utils/syncthing/files/syncthing.init

index 08a95f039684e0fae1b8ff4d41cdefb0143dd984..476d618cdb9f3ba90b9e2d947bcb1aaf9532f4a2 100644 (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'
index d1d34765523c90f8aa3d4c05d1f568d24ea4f471..a4f4f986ee51ce551f71709707568dcda287e4b3 100644 (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
index 3c2b40a7f2d338c8194f36617bcb25a3ef359382..4f4f26f912b4eadbe9435b0f06519d5858ea78a9 100644 (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'
index 5bb53a944922828e56afda18ebbb2f1278af99ec..5e4c55e14acf2782222e12a29a54a65f536729e4 100644 (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" ] || \
index 2c1b6d0e2ec9ef9e105dd12545722d05b722fc30..9c68d901cd39006ce92ee42af7d1c40ec2d3b6b1 100644 (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'
index b50abf74f41faa9c59618c2e76534f9aaa8a832c..b8c93e9e5f7eb82f974176871fb1762574a6efff 100644 (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
 }
 
git clone https://git.99rst.org/PROJECT