From aac0f5d327efc872160cd824e02480c091e49320 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 25 Feb 2025 17:51:03 -0500 Subject: [PATCH] Update unix socket support for v0.18.1 --- .SRCINFO | 12 +- PKGBUILD | 10 +- support-unix-sockets.patch | 704 ++++++++++++++++++++++++++++++++++--- 3 files changed, 661 insertions(+), 65 deletions(-) diff --git a/.SRCINFO b/.SRCINFO index 3e3743e..122ca2c 100644 --- a/.SRCINFO +++ b/.SRCINFO @@ -1,6 +1,6 @@ pkgbase = gotosocial pkgdesc = ActivityPub social network server written in Golang - pkgver = 0.17.3 + pkgver = 0.18.1 pkgrel = 11 url = https://gotosocial.org arch = x86_64 @@ -53,20 +53,20 @@ pkgbase = gotosocial backup = etc/gotosocial/template/status_poll.tmpl backup = etc/gotosocial/template/tag.tmpl backup = etc/gotosocial/template/thread.tmpl - source = gotosocial::git+https://github.com/superseriousbusiness/gotosocial#tag=v0.17.3 + source = gotosocial::git+https://github.com/superseriousbusiness/gotosocial#tag=v0.18.1 source = sysusers.conf source = tmpfiles.conf source = use-fhs-directories.patch source = support-unix-sockets.patch - sha512sums = 029c59888f75f217446a06309db075556a4dfdeafd9b731fa7e0b7a437fca3f030e10029b0f774f04695b16bd7c0504e28e964e8ed280bea1bdce3ec007c67d7 + sha512sums = 74733f80ed011e3a3cad455ae35a931eb93b051cf493e5f87341d8f9a47f416e6886cc769b4ffeb623536466ff72932f3384c62da5348285801ddfe8107fc063 sha512sums = 68890539a1285a819d5a2cd755aeabd59a9872926d9c32e5d54faaf2771414f006e568f2f813f3c6fcd9dbeda7b6e57c924d7490521880cb65632e02fabcbd63 sha512sums = b89fad3073e140f17167515b38942e5b5e2bc2aee03c484e1bb7cf6444f86cb1e2a13a60b101e04d22633d348be073ca26cd309da4746e5062c12b4f3ce4b38a sha512sums = 913a5209487aba06bf1d8ac7c02506a05d01a8e12f172666c84bf6870d6237640d4745617b0f07ea8c9dcf665f4e0d24a0aabef31611909f7e9384ed6e7b7e77 - sha512sums = 0dabf00f568ea041284789023eccea97863ce6ddb195c94318cf0a932b7c35da9beab9c78eb1c1b9ffa196685fb44eb98e0ad94da96b351644c856d9ac86e24f - b2sums = 579a4e16f97aef42dc1952873f5a443476d34d8724c1c401ae6ae6acb1fb0e109ad5045da3597475e8c0816afdb276dc8820f40f6cb82e2426a61a2efceffa8b + sha512sums = 5cd0f4f325c452f311e5187147a0d91bae5b2537f329a087e6bc8618317021dbf2d80d7c42d135946ce2399223bebfa4988d856bdba730a20084ce9c21f77990 + b2sums = eb0e91b9e7196c93a4e1eddf5eba448951ae9cb38cf599dfe87c182235f666a5f89deb28819ddcaf70c3cb0f0a1ed90ef05dbac4b23972ec05645cd6f23eaf29 b2sums = 0a5be7ac18af882c0c89d8e930eb76c2e60bc2c1d5a375ab04e987c7de9a7a3175319c4e5fbc818261141daf5f70d583aebcc2dc197fe3a88047fbfe488ccb94 b2sums = 4f65af952441c0f54bb32049a149675e207f8993678423d369c4095c57476464614ac720eccc64d7a93a81268ad7ca41cae75ca7211bd7b78f9035f6e5341f04 b2sums = 9edd4520fb99856feb82d01935588add7f805aa180f2ed0fe169cb26576bc2e1d2c1e6ab11604d977cec6a4ad8f1d5be1413e1a366de59b89c5b869136538f8c - b2sums = 4f73bfcac274e10e0378aebbd6792bb22eb9be028f9ab2f62605d4ee29cfaa400217afe94d71b5961c54b98dab79966116462c352653d4842a55dcd214ee4625 + b2sums = a2ae0a592608bb66784c704a076ceacd3f96b81006354a02da33d1292125f5e4e96c35472206cd2c998a9bc0eb2f191ccc66be69eb1397d8aa3eee145c9e7a7a pkgname = gotosocial diff --git a/PKGBUILD b/PKGBUILD index a09fee6..e64cd9f 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ # Contributor: Stefan Husmann pkgname=gotosocial -pkgver=0.17.3 +pkgver=0.18.1 pkgrel=11 pkgdesc='ActivityPub social network server written in Golang' arch=('x86_64') @@ -62,16 +62,16 @@ source=( 'use-fhs-directories.patch' 'support-unix-sockets.patch' ) -sha512sums=('029c59888f75f217446a06309db075556a4dfdeafd9b731fa7e0b7a437fca3f030e10029b0f774f04695b16bd7c0504e28e964e8ed280bea1bdce3ec007c67d7' +sha512sums=('74733f80ed011e3a3cad455ae35a931eb93b051cf493e5f87341d8f9a47f416e6886cc769b4ffeb623536466ff72932f3384c62da5348285801ddfe8107fc063' '68890539a1285a819d5a2cd755aeabd59a9872926d9c32e5d54faaf2771414f006e568f2f813f3c6fcd9dbeda7b6e57c924d7490521880cb65632e02fabcbd63' 'b89fad3073e140f17167515b38942e5b5e2bc2aee03c484e1bb7cf6444f86cb1e2a13a60b101e04d22633d348be073ca26cd309da4746e5062c12b4f3ce4b38a' '913a5209487aba06bf1d8ac7c02506a05d01a8e12f172666c84bf6870d6237640d4745617b0f07ea8c9dcf665f4e0d24a0aabef31611909f7e9384ed6e7b7e77' - '0dabf00f568ea041284789023eccea97863ce6ddb195c94318cf0a932b7c35da9beab9c78eb1c1b9ffa196685fb44eb98e0ad94da96b351644c856d9ac86e24f') -b2sums=('579a4e16f97aef42dc1952873f5a443476d34d8724c1c401ae6ae6acb1fb0e109ad5045da3597475e8c0816afdb276dc8820f40f6cb82e2426a61a2efceffa8b' + '5cd0f4f325c452f311e5187147a0d91bae5b2537f329a087e6bc8618317021dbf2d80d7c42d135946ce2399223bebfa4988d856bdba730a20084ce9c21f77990') +b2sums=('eb0e91b9e7196c93a4e1eddf5eba448951ae9cb38cf599dfe87c182235f666a5f89deb28819ddcaf70c3cb0f0a1ed90ef05dbac4b23972ec05645cd6f23eaf29' '0a5be7ac18af882c0c89d8e930eb76c2e60bc2c1d5a375ab04e987c7de9a7a3175319c4e5fbc818261141daf5f70d583aebcc2dc197fe3a88047fbfe488ccb94' '4f65af952441c0f54bb32049a149675e207f8993678423d369c4095c57476464614ac720eccc64d7a93a81268ad7ca41cae75ca7211bd7b78f9035f6e5341f04' '9edd4520fb99856feb82d01935588add7f805aa180f2ed0fe169cb26576bc2e1d2c1e6ab11604d977cec6a4ad8f1d5be1413e1a366de59b89c5b869136538f8c' - '4f73bfcac274e10e0378aebbd6792bb22eb9be028f9ab2f62605d4ee29cfaa400217afe94d71b5961c54b98dab79966116462c352653d4842a55dcd214ee4625') + 'a2ae0a592608bb66784c704a076ceacd3f96b81006354a02da33d1292125f5e4e96c35472206cd2c998a9bc0eb2f191ccc66be69eb1397d8aa3eee145c9e7a7a') pkgver() { cd "$pkgname" diff --git a/support-unix-sockets.patch b/support-unix-sockets.patch index aeb1a49..c46558d 100644 --- a/support-unix-sockets.patch +++ b/support-unix-sockets.patch @@ -1,28 +1,13 @@ -From 6d28643f94db3af7eaa934780c478452e2c707c6 Mon Sep 17 00:00:00 2001 +From 81a5c46ffe501341d942a2be7931b7c6b5ce9571 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Thu, 9 May 2024 00:04:42 -0400 -Subject: [PATCH] Support listening on unix sockets +Subject: [PATCH 1/2] Support listening on unix sockets 1. bind-address now accepts unix socket paths 2. Add config option trusted-platform ---- - example/config.yaml | 13 +- - go.mod | 1 + - go.sum | 2 + - internal/config/config.go | 1 + - internal/config/helpers.gen.go | 25 +++ - internal/router/router.go | 20 ++ - vendor/go.balki.me/anyhttp/LICENSE | 201 +++++++++++++++++++ - vendor/go.balki.me/anyhttp/README.md | 77 ++++++++ - vendor/go.balki.me/anyhttp/anyhttp.go | 269 ++++++++++++++++++++++++++ - vendor/modules.txt | 3 + - 10 files changed, 609 insertions(+), 3 deletions(-) - create mode 100644 vendor/go.balki.me/anyhttp/LICENSE - create mode 100644 vendor/go.balki.me/anyhttp/README.md - create mode 100644 vendor/go.balki.me/anyhttp/anyhttp.go diff --git a/example/config.yaml b/example/config.yaml -index 644b51575..6ea35672a 100644 +index 2b3a873fb..52615a345 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -105,13 +105,13 @@ account-domain: "" @@ -64,36 +49,11 @@ index 644b51575..6ea35672a 100644 # Default: "" db-address: "" -diff --git a/go.mod b/go.mod -index 2b7ab98fd..5b09f7696 100644 ---- a/go.mod -+++ b/go.mod -@@ -63,6 +63,7 @@ require ( - github.com/uptrace/bun/extra/bunotel v1.2.1 - github.com/wagslane/go-password-validator v0.3.0 - github.com/yuin/goldmark v1.7.8 -+ go.balki.me/anyhttp v0.3.0 - go.opentelemetry.io/otel v1.29.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 -diff --git a/go.sum b/go.sum -index 72e252234..ff8cf88c0 100644 ---- a/go.sum -+++ b/go.sum -@@ -623,6 +623,8 @@ github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= - github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= - gitlab.com/NyaaaWhatsUpDoc/sqlite v1.33.1-concurrency-workaround h1:pFMJnlc1PuH+jcVz4vz53vcpnoZG+NqFBr3qikDmEB4= - gitlab.com/NyaaaWhatsUpDoc/sqlite v1.33.1-concurrency-workaround/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k= -+go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY= -+go.balki.me/anyhttp v0.3.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s= - go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80= - go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= - go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= diff --git a/internal/config/config.go b/internal/config/config.go -index 9001b61d0..a59bffc7d 100644 +index 8ce2105b4..fa90aad80 100644 --- a/internal/config/config.go +++ b/internal/config/config.go -@@ -58,6 +58,7 @@ type Configuration struct { +@@ -59,6 +59,7 @@ type Configuration struct { BindAddress string `name:"bind-address" usage:"Bind address to use for the GoToSocial server (eg., 0.0.0.0, 172.138.0.9, [::], localhost). For ipv6, enclose the address in square brackets, eg [2001:db8::fed1]. Default binds to all interfaces."` Port int `name:"port" usage:"Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine."` TrustedProxies []string `name:"trusted-proxies" usage:"Proxies to trust when parsing x-forwarded headers into real IPs."` @@ -102,10 +62,10 @@ index 9001b61d0..a59bffc7d 100644 DbType string `name:"db-type" usage:"Database type: eg., postgres"` diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go -index 2a7e5b6ad..c95c5f312 100644 +index 156c19fd5..a3c09fe8b 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go -@@ -350,6 +350,31 @@ func GetTrustedProxies() []string { return global.GetTrustedProxies() } +@@ -351,6 +351,31 @@ func GetTrustedProxies() []string { return global.GetTrustedProxies() } // SetTrustedProxies safely sets the value for global configuration 'TrustedProxies' field func SetTrustedProxies(v []string) { global.SetTrustedProxies(v) } @@ -138,7 +98,7 @@ index 2a7e5b6ad..c95c5f312 100644 func (st *ConfigState) GetSoftwareVersion() (v string) { st.mutex.RLock() diff --git a/internal/router/router.go b/internal/router/router.go -index cf9033059..e952d383c 100644 +index c2bf18b4f..73bfc8e96 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -23,6 +23,7 @@ @@ -157,7 +117,7 @@ index cf9033059..e952d383c 100644 "golang.org/x/crypto/acme/autocert" ) -@@ -74,6 +76,11 @@ func New(ctx context.Context) (*Router, error) { +@@ -75,6 +77,11 @@ func New(ctx context.Context) (*Router, error) { engine.MaxMultipartMemory = maxMultipartMemory engine.HandleMethodNotAllowed = true @@ -169,7 +129,7 @@ index cf9033059..e952d383c 100644 // Set up client IP forwarding via // trusted x-forwarded-* headers. trustedProxies := config.GetTrustedProxies() -@@ -134,6 +141,7 @@ func (r *Router) Start() error { +@@ -135,6 +142,7 @@ func (r *Router) Start() error { certFile = config.GetTLSCertificateChain() keyFile = config.GetTLSCertificateKey() leEnabled = config.GetLetsEncryptEnabled() @@ -177,7 +137,7 @@ index cf9033059..e952d383c 100644 ) switch { -@@ -154,6 +162,18 @@ func (r *Router) Start() error { +@@ -155,6 +163,18 @@ func (r *Router) Start() error { return err } @@ -762,10 +722,10 @@ index 000000000..5c0615442 + _ = os.Unsetenv("LISTEN_FDNAMES") +} diff --git a/vendor/modules.txt b/vendor/modules.txt -index 10c1e595c..78fb14705 100644 +index 8c52d8949..dfb915c88 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt -@@ -963,6 +963,9 @@ github.com/yuin/goldmark/renderer +@@ -961,6 +961,9 @@ github.com/yuin/goldmark/renderer github.com/yuin/goldmark/renderer/html github.com/yuin/goldmark/text github.com/yuin/goldmark/util @@ -776,5 +736,641 @@ index 10c1e595c..78fb14705 100644 ## explicit; go 1.18 go.mongodb.org/mongo-driver/bson -- -2.47.1 +2.48.1 + + +From d2de56e788b1660b404b50e99371937e9aa5128c Mon Sep 17 00:00:00 2001 +From: Balakrishnan Balasubramanian +Date: Tue, 25 Feb 2025 17:35:39 -0500 +Subject: [PATCH 2/2] Update anyhttp + +Run go mod tidy +Run go mod vendor +Update GetListener function + +diff --git a/go.mod b/go.mod +index 1dff4fe44..51f014ba4 100644 +--- a/go.mod ++++ b/go.mod +@@ -70,6 +70,7 @@ require ( + github.com/uptrace/bun/extra/bunotel v1.2.9 + github.com/wagslane/go-password-validator v0.3.0 + github.com/yuin/goldmark v1.7.8 ++ go.balki.me/anyhttp v0.5.0 + go.opentelemetry.io/otel v1.34.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.34.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 +diff --git a/go.sum b/go.sum +index d1713cceb..18e258500 100644 +--- a/go.sum ++++ b/go.sum +@@ -619,6 +619,8 @@ github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= + github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= + gitlab.com/NyaaaWhatsUpDoc/sqlite v1.35.0-concurrency-workaround h1:rSPHdoNXzXyWQUUeMEy8pdOFn8lH7XqdBRTS9G+jdTg= + gitlab.com/NyaaaWhatsUpDoc/sqlite v1.35.0-concurrency-workaround/go.mod h1:9cr2sicr7jIaWTBKQmAxQLfBv9LL0su4ZTEV+utt3ic= ++go.balki.me/anyhttp v0.5.0 h1:uys0oRciBpZfwtxXAevScKy6amIQBXyDrcV0EtGF5zo= ++go.balki.me/anyhttp v0.5.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s= + go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80= + go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= + go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +diff --git a/internal/router/router.go b/internal/router/router.go +index 73bfc8e96..97ee65847 100644 +--- a/internal/router/router.go ++++ b/internal/router/router.go +@@ -164,9 +164,9 @@ func (r *Router) Start() error { + } + + // TLS handled by reverse proxy connecting using unix socket +- case strings.HasPrefix(bindAddr, "unix/"): ++ case strings.HasPrefix(bindAddr, "unix?"): + listen, err = func() (func() error, error) { +- _, listener, err := anyhttp.GetListener(bindAddr) ++ listener, _, _, err := anyhttp.GetListener(bindAddr) + if err != nil { + return nil, err + } +diff --git a/vendor/go.balki.me/anyhttp/README.md b/vendor/go.balki.me/anyhttp/README.md +index 7e9b11922..85bce6fc3 100644 +--- a/vendor/go.balki.me/anyhttp/README.md ++++ b/vendor/go.balki.me/anyhttp/README.md +@@ -17,56 +17,57 @@ Just replace `http.ListenAndServe` with `anyhttp.ListenAndServe`. + + Syntax + +- unix/ ++ unix?path=&mode=&remove_existing= + + Examples + +- unix/relative/path.sock +- unix//var/run/app/absolutepath.sock ++ unix?path=relative/path.sock ++ unix?path=/var/run/app/absolutepath.sock ++ unix?path=/run/app.sock&mode=600&remove_existing=false ++ ++| option | description | default | ++|-----------------|------------------------------------------------|----------| ++| path | path to unix socket | Required | ++| mode | socket file mode | 666 | ++| remove_existing | Whether to remove existing socket file or fail | true | + + ### Systemd Socket activated fd: + + Syntax + +- sysd/fdidx/ +- sysd/fdname/ ++ sysd?idx=&name=&check_pid=&unset_env=&idle_timeout= ++ ++Only one of `idx` or `name` has to be set + + Examples: +- ++ + # First (or only) socket fd passed to app +- sysd/fdidx/0 ++ sysd?idx=0 + + # Socket with FileDescriptorName +- sysd/fdname/myapp ++ sysd?name=myapp + +- # Using default name +- sysd/fdname/myapp.socket ++ # Using default name and auto shutdown if no requests received in last 30 minutes ++ sysd?name=myapp.socket&idle_timeout=30m + +-### TCP port ++| option | description | default | ++|--------------|--------------------------------------------------------------------------------------------|------------------| ++| name | Name configured via FileDescriptorName or socket file name | Required | ++| idx | FD Index. Actual fd num will be 3 + idx | Required | ++| idle_timeout | time to wait before shutdown. [syntax][0] | no auto shutdown | ++| check_pid | Check process PID matches LISTEN_PID | true | ++| unset_env | Unsets the LISTEN\* environment variables, so they don't get passed to any child processes | true | + +-If the address is a number less than 65536, it is assumed as a port and passed +-as `http.ListenAndServe(":",...)` Anything else is directly passed to +-`http.ListenAndServe` as well. Below examples should work ++### TCP ++ ++If the address is not one of above, it is assumed to be tcp and passed to `http.ListenAndServe`. ++ ++Examples: + + :http + :8888 + 127.0.0.1:8080 + +-## Idle server auto shutdown +- +-When using systemd socket activation, idle servers can be shut down to save on +-resources. They will be restarted with socket activation when new request +-arrives. Quick example for the case. (Error checking skipped for brevity) +- +-```go +-addrType, httpServer, done, _ := anyhttp.Serve(addr, idle.WrapHandler(nil)) +-if addrType == anyhttp.SystemdFD { +- idle.Wait(30 * time.Minute) +- httpServer.Shutdown(context.TODO()) +-} +-<-done +-``` +- + ## Documentation + + https://pkg.go.dev/go.balki.me/anyhttp +@@ -75,3 +76,5 @@ https://pkg.go.dev/go.balki.me/anyhttp + + * https://gist.github.com/teknoraver/5ffacb8757330715bcbcc90e6d46ac74#file-unixhttpd-go + * https://github.com/coreos/go-systemd/tree/main/activation ++ ++[0]: https://pkg.go.dev/time#ParseDuration +diff --git a/vendor/go.balki.me/anyhttp/anyhttp.go b/vendor/go.balki.me/anyhttp/anyhttp.go +index 5c0615442..8d316a78f 100644 +--- a/vendor/go.balki.me/anyhttp/anyhttp.go ++++ b/vendor/go.balki.me/anyhttp/anyhttp.go +@@ -2,25 +2,30 @@ + package anyhttp + + import ( ++ "context" + "errors" + "fmt" + "io/fs" + "net" + "net/http" ++ "net/url" + "os" + "strconv" + "strings" + "sync" + "syscall" ++ "time" ++ ++ "go.balki.me/anyhttp/idle" + ) + + // AddressType of the address passed + type AddressType string + + var ( +- // UnixSocket - address is a unix socket, e.g. unix//run/foo.sock ++ // UnixSocket - address is a unix socket, e.g. unix?path=/run/foo.sock + UnixSocket AddressType = "UnixSocket" +- // SystemdFD - address is a systemd fd, e.g. sysd/fdname/myapp.socket ++ // SystemdFD - address is a systemd fd, e.g. sysd?name=myapp.socket + SystemdFD AddressType = "SystemdFD" + // TCP - address is a TCP address, e.g. :1234 + TCP AddressType = "TCP" +@@ -97,6 +102,8 @@ type SysdConfig struct { + CheckPID bool + // Unsets the LISTEN* environment variables, so they don't get passed to any child processes + UnsetEnv bool ++ // Shutdown http server if no requests received for below timeout ++ IdleTimeout *time.Duration + } + + // DefaultSysdConfig has the default values for SysdConfig +@@ -196,69 +203,86 @@ func (s *SysdConfig) GetListener() (net.Listener, error) { + return nil, errors.New("neither FDIndex nor FDName set") + } + +-// GetListener gets a unix or systemd socket listener +-func GetListener(addr string) (AddressType, net.Listener, error) { +- if strings.HasPrefix(addr, "unix/") { +- usc := NewUnixSocketConfig(strings.TrimPrefix(addr, "unix/")) +- l, err := usc.GetListener() +- return UnixSocket, l, err +- } ++// GetListener is low level function for use with non-http servers. e.g. tcp, smtp ++// Caller should handle idle timeout if needed ++func GetListener(addr string) (net.Listener, AddressType, any /* cfg */, error) { + +- if strings.HasPrefix(addr, "sysd/fdidx/") { +- idx, err := strconv.Atoi(strings.TrimPrefix(addr, "sysd/fdidx/")) ++ addrType, unixSocketConfig, sysdConfig, perr := parseAddress(addr) ++ if perr != nil { ++ return nil, Unknown, nil, perr ++ } ++ if unixSocketConfig != nil { ++ listener, err := unixSocketConfig.GetListener() + if err != nil { +- return Unknown, nil, fmt.Errorf("invalid fdidx, addr:%q err: %w", addr, err) ++ return nil, Unknown, nil, err + } +- sysdc := NewSysDConfigWithFDIdx(idx) +- l, err := sysdc.GetListener() +- return SystemdFD, l, err +- } +- +- if strings.HasPrefix(addr, "sysd/fdname/") { +- sysdc := NewSysDConfigWithFDName(strings.TrimPrefix(addr, "sysd/fdname/")) +- l, err := sysdc.GetListener() +- return SystemdFD, l, err +- } +- +- if port, err := strconv.Atoi(addr); err == nil { +- if port > 0 && port < 65536 { +- addr = fmt.Sprintf(":%v", port) +- } else { +- return Unknown, nil, fmt.Errorf("invalid port: %v", port) ++ return listener, addrType, unixSocketConfig, nil ++ } else if sysdConfig != nil { ++ listener, err := sysdConfig.GetListener() ++ if err != nil { ++ return nil, Unknown, nil, err + } ++ return listener, addrType, sysdConfig, nil + } +- + if addr == "" { + addr = ":http" + } +- +- l, err := net.Listen("tcp", addr) +- return TCP, l, err ++ listener, err := net.Listen("tcp", addr) ++ return listener, TCP, nil, err + } + +-// Serve creates and serve a http server. +-func Serve(addr string, h http.Handler) (AddressType, *http.Server, <-chan error, error) { +- addrType, listener, err := GetListener(addr) ++type ServerCtx struct { ++ AddressType AddressType ++ Listener net.Listener ++ Server *http.Server ++ Idler idle.Idler ++ Done <-chan error ++ UnixSocketConfig *UnixSocketConfig ++ SysdConfig *SysdConfig ++} ++ ++func (s *ServerCtx) Wait() error { ++ return <-s.Done ++} ++ ++func (s *ServerCtx) Addr() net.Addr { ++ return s.Listener.Addr() ++} ++ ++func (s *ServerCtx) Shutdown(ctx context.Context) error { ++ err := s.Server.Shutdown(ctx) + if err != nil { +- return addrType, nil, nil, err ++ return err + } +- srv := &http.Server{Handler: h} +- done := make(chan error) +- go func() { +- done <- srv.Serve(listener) +- close(done) +- }() +- return addrType, srv, done, nil ++ return <-s.Done ++} ++ ++// ServeTLS creates and serves a HTTPS server. ++func ServeTLS(addr string, h http.Handler, certFile string, keyFile string) (*ServerCtx, error) { ++ return serve(addr, h, certFile, keyFile) ++} ++ ++// Serve creates and serves a HTTP server. ++func Serve(addr string, h http.Handler) (*ServerCtx, error) { ++ return serve(addr, h, "", "") + } + + // ListenAndServe is the drop-in replacement for `http.ListenAndServe`. + // Supports unix and systemd sockets in addition + func ListenAndServe(addr string, h http.Handler) error { +- _, _, done, err := Serve(addr, h) ++ ctx, err := Serve(addr, h) + if err != nil { + return err + } +- return <-done ++ return ctx.Wait() ++} ++ ++func ListenAndServeTLS(addr string, certFile string, keyFile string, h http.Handler) error { ++ ctx, err := ServeTLS(addr, h, certFile, keyFile) ++ if err != nil { ++ return err ++ } ++ return ctx.Wait() + } + + // UnsetSystemdListenVars unsets the LISTEN* environment variables so they are not passed to any child processes +@@ -267,3 +291,150 @@ func UnsetSystemdListenVars() { + _ = os.Unsetenv("LISTEN_FDS") + _ = os.Unsetenv("LISTEN_FDNAMES") + } ++ ++func parseAddress(addr string) (addrType AddressType, usc *UnixSocketConfig, sysc *SysdConfig, err error) { ++ usc = nil ++ sysc = nil ++ err = nil ++ u, err := url.Parse(addr) ++ if err != nil { ++ return TCP, nil, nil, nil ++ } ++ if u.Path == "unix" { ++ duc := DefaultUnixSocketConfig ++ usc = &duc ++ addrType = UnixSocket ++ for key, val := range u.Query() { ++ if len(val) != 1 { ++ err = fmt.Errorf("unix socket address error. Multiple %v found: %v", key, val) ++ return ++ } ++ if key == "path" { ++ usc.SocketPath = val[0] ++ } else if key == "mode" { ++ if _, serr := fmt.Sscanf(val[0], "%o", &usc.SocketMode); serr != nil { ++ err = fmt.Errorf("unix socket address error. Bad mode: %v, err: %w", val, serr) ++ return ++ } ++ } else if key == "remove_existing" { ++ if removeExisting, berr := strconv.ParseBool(val[0]); berr == nil { ++ usc.RemoveExisting = removeExisting ++ } else { ++ err = fmt.Errorf("unix socket address error. Bad remove_existing: %v, err: %w", val, berr) ++ return ++ } ++ } else { ++ err = fmt.Errorf("unix socket address error. Bad option; key: %v, val: %v", key, val) ++ return ++ } ++ } ++ if usc.SocketPath == "" { ++ err = fmt.Errorf("unix socket address error. Missing path; addr: %v", addr) ++ return ++ } ++ } else if u.Path == "sysd" { ++ dsc := DefaultSysdConfig ++ sysc = &dsc ++ addrType = SystemdFD ++ for key, val := range u.Query() { ++ if len(val) != 1 { ++ err = fmt.Errorf("systemd socket fd address error. Multiple %v found: %v", key, val) ++ return ++ } ++ if key == "name" { ++ sysc.FDName = &val[0] ++ } else if key == "idx" { ++ if idx, ierr := strconv.Atoi(val[0]); ierr == nil { ++ sysc.FDIndex = &idx ++ } else { ++ err = fmt.Errorf("systemd socket fd address error. Bad idx: %v, err: %w", val, ierr) ++ return ++ } ++ } else if key == "check_pid" { ++ if checkPID, berr := strconv.ParseBool(val[0]); berr == nil { ++ sysc.CheckPID = checkPID ++ } else { ++ err = fmt.Errorf("systemd socket fd address error. Bad check_pid: %v, err: %w", val, berr) ++ return ++ } ++ } else if key == "unset_env" { ++ if unsetEnv, berr := strconv.ParseBool(val[0]); berr == nil { ++ sysc.UnsetEnv = unsetEnv ++ } else { ++ err = fmt.Errorf("systemd socket fd address error. Bad unset_env: %v, err: %w", val, berr) ++ return ++ } ++ } else if key == "idle_timeout" { ++ if timeout, terr := time.ParseDuration(val[0]); terr == nil { ++ sysc.IdleTimeout = &timeout ++ } else { ++ err = fmt.Errorf("systemd socket fd address error. Bad idle_timeout: %v, err: %w", val, terr) ++ return ++ } ++ } else { ++ err = fmt.Errorf("systemd socket fd address error. Bad option; key: %v, val: %v", key, val) ++ return ++ } ++ } ++ if (sysc.FDIndex == nil) == (sysc.FDName == nil) { ++ err = fmt.Errorf("systemd socket fd address error. Exactly only one of name and idx has to be set. name: %v, idx: %v", sysc.FDName, sysc.FDIndex) ++ return ++ } ++ } else { ++ // Just assume as TCP address ++ return TCP, nil, nil, nil ++ } ++ return ++} ++ ++func serve(addr string, h http.Handler, certFile string, keyFile string) (*ServerCtx, error) { ++ ++ serveFn := func() func(ctx *ServerCtx) error { ++ if certFile != "" { ++ return func(ctx *ServerCtx) error { ++ return ctx.Server.ServeTLS(ctx.Listener, certFile, keyFile) ++ } ++ } ++ return func(ctx *ServerCtx) error { ++ return ctx.Server.Serve(ctx.Listener) ++ } ++ }() ++ var ctx ServerCtx ++ var err error ++ var cfg any ++ ++ ctx.Listener, ctx.AddressType, cfg, err = GetListener(addr) ++ if err != nil { ++ return nil, err ++ } ++ switch ctx.AddressType { ++ case UnixSocket: ++ ctx.UnixSocketConfig = cfg.(*UnixSocketConfig) ++ case SystemdFD: ++ ctx.SysdConfig = cfg.(*SysdConfig) ++ } ++ errChan := make(chan error) ++ ctx.Done = errChan ++ if ctx.AddressType == SystemdFD && ctx.SysdConfig.IdleTimeout != nil { ++ ctx.Idler = idle.CreateIdler(*ctx.SysdConfig.IdleTimeout) ++ ctx.Server = &http.Server{Handler: idle.WrapIdlerHandler(ctx.Idler, h)} ++ waitErrChan := make(chan error) ++ go func() { ++ waitErrChan <- serveFn(&ctx) ++ }() ++ go func() { ++ select { ++ case err := <-waitErrChan: ++ errChan <- err ++ case <-ctx.Idler.Chan(): ++ errChan <- ctx.Server.Shutdown(context.TODO()) ++ } ++ }() ++ } else { ++ ctx.Server = &http.Server{Handler: h} ++ go func() { ++ errChan <- serveFn(&ctx) ++ }() ++ } ++ return &ctx, nil ++} +diff --git a/vendor/go.balki.me/anyhttp/idle/idle.go b/vendor/go.balki.me/anyhttp/idle/idle.go +new file mode 100644 +index 000000000..ee3d81ff1 +--- /dev/null ++++ b/vendor/go.balki.me/anyhttp/idle/idle.go +@@ -0,0 +1,127 @@ ++// Package idle helps to gracefully shutdown idle (typically http) servers ++package idle ++ ++import ( ++ "fmt" ++ "net/http" ++ "sync/atomic" ++ "time" ++) ++ ++var ( ++ // For simple servers without backgroud jobs, global singleton for simpler API ++ // Enter/Exit worn't work for global idler as Enter may be called before Wait, use CreateIdler in those cases ++ gIdler atomic.Pointer[idler] ++) ++ ++// Wait waits till the server is idle and returns. i.e. no Ticks in last duration ++func Wait(timeout time.Duration) error { ++ i := CreateIdler(timeout).(*idler) ++ ok := gIdler.CompareAndSwap(nil, i) ++ if !ok { ++ return fmt.Errorf("idler already waiting") ++ } ++ i.Wait() ++ return nil ++} ++ ++// Tick records the current time. This will make the server not idle until next Tick or timeout ++func Tick() { ++ i := gIdler.Load() ++ if i != nil { ++ i.Tick() ++ } ++} ++ ++// WrapHandler calls Tick() before processing passing request to http.Handler ++func WrapHandler(h http.Handler) http.Handler { ++ if h == nil { ++ h = http.DefaultServeMux ++ } ++ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ++ Tick() ++ h.ServeHTTP(w, r) ++ }) ++} ++ ++// WrapIdlerHandler calls idler.Tick() before processing passing request to http.Handler ++func WrapIdlerHandler(i Idler, h http.Handler) http.Handler { ++ if h == nil { ++ h = http.DefaultServeMux ++ } ++ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ++ i.Tick() ++ h.ServeHTTP(w, r) ++ }) ++} ++ ++// Idler helps manage idle servers ++type Idler interface { ++ // Tick records the current time. This will make the server not idle until next Tick or timeout ++ Tick() ++ ++ // Wait waits till the server is idle and returns. i.e. no Ticks in last duration ++ Wait() ++ ++ // For long running background jobs, use Enter to record start time. Wait will not return while there are active jobs running ++ Enter() ++ ++ // Exit records end of a background job ++ Exit() ++ ++ // Get the channel to wait yourself ++ Chan() <-chan struct{} ++} ++ ++type idler struct { ++ lastTick atomic.Pointer[time.Time] ++ c chan struct{} ++ active atomic.Int64 ++} ++ ++func (i *idler) Enter() { ++ i.active.Add(1) ++} ++ ++func (i *idler) Exit() { ++ i.Tick() ++ i.active.Add(-1) ++} ++ ++// CreateIdler creates an Idler with given timeout ++func CreateIdler(timeout time.Duration) Idler { ++ i := &idler{} ++ i.c = make(chan struct{}) ++ i.Tick() ++ go func() { ++ for { ++ if i.active.Load() != 0 { ++ time.Sleep(timeout) ++ continue ++ } ++ t := *i.lastTick.Load() ++ now := time.Now() ++ dur := t.Add(timeout).Sub(now) ++ if dur == dur.Abs() { ++ time.Sleep(dur) ++ continue ++ } ++ break ++ } ++ close(i.c) ++ }() ++ return i ++} ++ ++func (i *idler) Tick() { ++ now := time.Now() ++ i.lastTick.Store(&now) ++} ++ ++func (i *idler) Wait() { ++ <-i.c ++} ++ ++func (i *idler) Chan() <-chan struct{} { ++ return i.c ++} +diff --git a/vendor/modules.txt b/vendor/modules.txt +index dfb915c88..bd7da894d 100644 +--- a/vendor/modules.txt ++++ b/vendor/modules.txt +@@ -466,8 +466,6 @@ github.com/microcosm-cc/bluemonday/css + # github.com/miekg/dns v1.1.63 + ## explicit; go 1.19 + github.com/miekg/dns +-# github.com/minio/crc64nvme v1.0.0 +-## explicit; go 1.22 + # github.com/minio/md5-simd v1.1.2 + ## explicit; go 1.14 + github.com/minio/md5-simd +@@ -961,9 +959,10 @@ github.com/yuin/goldmark/renderer + github.com/yuin/goldmark/renderer/html + github.com/yuin/goldmark/text + github.com/yuin/goldmark/util +-# go.balki.me/anyhttp v0.3.0 ++# go.balki.me/anyhttp v0.5.0 + ## explicit; go 1.20 + go.balki.me/anyhttp ++go.balki.me/anyhttp/idle + # go.mongodb.org/mongo-driver v1.14.0 + ## explicit; go 1.18 + go.mongodb.org/mongo-driver/bson +-- +2.48.1