From c93d3a0bb52d29766e4499fec9f37a0d7abf831b Mon Sep 17 00:00:00 2001 From: Catherine Date: Thu, 20 Nov 2025 03:43:20 +0000 Subject: [PATCH] Reload configuration on SIGHUP (if supported by OS). On Windows, there is no way to reload configuration at runtime. --- src/main.go | 20 ++++++++++++++++++++ src/signal_other.go | 7 +++++++ src/signal_posix.go | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/signal_other.go create mode 100644 src/signal_posix.go diff --git a/src/main.go b/src/main.go index ce4e7b6..318a5c7 100644 --- a/src/main.go +++ b/src/main.go @@ -303,6 +303,26 @@ func Main() { } default: + // Hook a signal (SIGHUP on *nix, nothing on Windows) for reloading the configuration + // at runtime. This is useful because it preserves S3 backend cache contents. Failed + // configuration reloads will not crash the process; you may want to check the syntax + // first with `git-pages -config ... -print-config` since there is no other feedback. + OnReload(func() { + if newConfig, err := Configure(*configTomlPath); err != nil { + log.Println("config:", err) + log.Println("config: reload failed") + } else { + log.Println("config: reloaded") + // From https://go.dev/ref/mem: + // > A read r of a memory location x holding a value that is not larger than + // > a machine word must observe some write w such that r does not happen before + // > w and there is no write w' such that w happens before w' and w' happens + // > before r. That is, each read must observe a value written by a preceding or + // > concurrent write. + config = newConfig + } + }) + // Start listening on all ports before initializing the backend, otherwise if the backend // spends some time initializing (which the S3 backend does) a proxy like Caddy can race // with git-pages on startup and return errors for requests that would have been served diff --git a/src/signal_other.go b/src/signal_other.go new file mode 100644 index 0000000..558a8cc --- /dev/null +++ b/src/signal_other.go @@ -0,0 +1,7 @@ +//go:build !unix + +package git_pages + +func OnReload(handler func()) { + // not implemented +} diff --git a/src/signal_posix.go b/src/signal_posix.go new file mode 100644 index 0000000..55c7732 --- /dev/null +++ b/src/signal_posix.go @@ -0,0 +1,20 @@ +//go:build unix + +package git_pages + +import ( + "os" + "os/signal" + "syscall" +) + +func OnReload(handler func()) { + sighup := make(chan os.Signal, 1) + signal.Notify(sighup, syscall.SIGHUP) + go func() { + for { + <-sighup + handler() + } + }() +}