From fa4761a9e437e896719246a8840b8856a48a199c Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 2 Jul 2024 11:47:57 +0200 Subject: extract listenfd library --- go.mod | 2 +- go.sum | 4 +-- internal/listenfd/listenfd.go | 67 ------------------------------------------- internal/server/tcp.go | 2 +- internal/server/tls.go | 2 +- 5 files changed, 5 insertions(+), 72 deletions(-) delete mode 100644 internal/listenfd/listenfd.go diff --git a/go.mod b/go.mod index 05bb1fa..c918db2 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/stefanfritsch/goldmark-fences v1.0.0 github.com/yuin/goldmark v1.7.4 gitlab.com/tozd/go/errors v0.8.1 - go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 + go.alanpearce.eu/x v0.0.0-20240702094403-3af026a42747 ) require ( diff --git a/go.sum b/go.sum index bbafba5..97c11e9 100644 --- a/go.sum +++ b/go.sum @@ -225,8 +225,8 @@ github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo= github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= gitlab.com/tozd/go/errors v0.8.1 h1:RfylffRAsl3PbDdHNUBEkTleTCiL/RIT+Ef8p0HRNCI= gitlab.com/tozd/go/errors v0.8.1/go.mod h1:PvIdUMLpPwxr+KEBxghQaCMydHXGYdJQn/PhdMqYREY= -go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 h1:a4PJum9vqboETepA81QlBXAqgC9Zeu3F9EojnGiHauA= -go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60/go.mod h1:GaYgUfXSlaHBvdrInLYyKDMKo2Bmx1+IIFrlnZkZW+A= +go.alanpearce.eu/x v0.0.0-20240702094403-3af026a42747 h1:046LS5Vs9qRJLlKttgkAxFfl97jhYJY+SQRxaQO+/lM= +go.alanpearce.eu/x v0.0.0-20240702094403-3af026a42747/go.mod h1:FRM6J9HMQ/RV2Q5j+6RKBYWh/YNeEUriGSqDRchiHuQ= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.8.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= diff --git a/internal/listenfd/listenfd.go b/internal/listenfd/listenfd.go deleted file mode 100644 index 5287898..0000000 --- a/internal/listenfd/listenfd.go +++ /dev/null @@ -1,67 +0,0 @@ -package listenfd - -import ( - "crypto/tls" - "net" - "os" - "strconv" - - "go.alanpearce.eu/x/log" - - "gitlab.com/tozd/go/errors" -) - -const fdStart = 3 - -func GetListener(i uint64, addr string, log *log.Logger) (l net.Listener, err error) { - l, err = getFDSocket(i) - if err != nil { - log.Warn("could not create listener from listenfd", "error", err) - } - - log.Debug("listener from listenfd?", "passed", l != nil) - if l == nil { - l, err = net.Listen("tcp", addr) - if err != nil { - return nil, errors.Wrap(err, "could not create listener") - } - } - - return -} - -func GetListenerTLS( - i uint64, - addr string, - config *tls.Config, - log *log.Logger, -) (l net.Listener, err error) { - l, err = GetListener(i, addr, log) - if err != nil { - return nil, err - } - - return tls.NewListener(l, config), nil -} - -func getFDSocket(i uint64) (net.Listener, error) { - lfds, present := os.LookupEnv("LISTEN_FDS") - if !present { - return nil, nil - } - - fds, err := strconv.ParseUint(lfds, 10, 32) - if err != nil { - return nil, errors.Wrap(err, "could not parse LISTEN_FDS") - } - if i >= fds { - return nil, errors.Errorf("only %d fds available, requested index %d", fds, i) - } - - l, err := net.FileListener(os.NewFile(uintptr(i+fdStart), "")) - if err != nil { - return nil, errors.Wrap(err, "could not create listener") - } - - return l, nil -} diff --git a/internal/server/tcp.go b/internal/server/tcp.go index 12fdeb2..1627854 100644 --- a/internal/server/tcp.go +++ b/internal/server/tcp.go @@ -1,7 +1,7 @@ package server import ( - "go.alanpearce.eu/website/internal/listenfd" + "go.alanpearce.eu/x/listenfd" ) func (s *Server) serveTCP() error { diff --git a/internal/server/tls.go b/internal/server/tls.go index cd2bfb8..6b64a79 100644 --- a/internal/server/tls.go +++ b/internal/server/tls.go @@ -7,7 +7,7 @@ import ( "net/http" "strconv" - "go.alanpearce.eu/website/internal/listenfd" + "go.alanpearce.eu/x/listenfd" "github.com/ardanlabs/conf/v3" "github.com/caddyserver/caddy/v2" -- cgit 1.4.1