diff options
author | Alan Pearce | 2024-07-02 11:47:57 +0200 |
---|---|---|
committer | Alan Pearce | 2024-07-02 11:47:57 +0200 |
commit | fa4761a9e437e896719246a8840b8856a48a199c (patch) | |
tree | cf42d8a0039bef36ff869466526a5580d384ef16 /internal/listenfd | |
parent | b22ceb6c830f07061e5e16e1428438b495efe9d6 (diff) | |
download | website-fa4761a9e437e896719246a8840b8856a48a199c.tar.lz website-fa4761a9e437e896719246a8840b8856a48a199c.tar.zst website-fa4761a9e437e896719246a8840b8856a48a199c.zip |
extract listenfd library
Diffstat (limited to 'internal/listenfd')
-rw-r--r-- | internal/listenfd/listenfd.go | 67 |
1 files changed, 0 insertions, 67 deletions
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 -} |