Add global idler and other helpers #2

Merged
balki merged 5 commits from globalIdler into main 2023-09-11 20:45:52 -04:00
Showing only changes of commit 5c43a1a4fc - Show all commits

View File

@ -14,12 +14,17 @@ import (
"syscall"
)
// AddressType of the address passed
type AddressType string
var (
// UnixSocket - address is a unix socket, e.g. unix//run/foo.sock
UnixSocket AddressType = "UnixSocket"
// SystemdFD - address is a systemd fd, e.g. sysd/fdname/myapp.socket
SystemdFD AddressType = "SystemdFD"
// TCP - address is a TCP address, e.g. :1234
TCP AddressType = "TCP"
// Unknown - address is not recognized
Unknown AddressType = "Unknown"
)
@ -231,22 +236,30 @@ func GetListener(addr string) (AddressType, net.Listener, error) {
return TCP, l, err
}
func ListenAndServeHTTP(addr string, h http.Handler) (AddressType, *http.Server, error) {
// 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)
if err != nil {
return addrType, nil, err
return addrType, nil, nil, err
}
srv := &http.Server{Handler: h}
err = srv.Serve(listener)
return addrType, srv, err
done := make(chan error)
go func() {
done <- srv.Serve(listener)
close(done)
}()
return addrType, srv, done, nil
}
// ListenAndServe is the drop-in replacement for `http.ListenAndServe`.
// Supports unix and systemd sockets in addition
func ListenAndServe(addr string, h http.Handler) error {
_, _, err := ListenAndServeHTTP(addr, h)
_, _, done, err := Serve(addr, h)
if err != nil {
return err
}
return <-done
}
// UnsetSystemdListenVars unsets the LISTEN* environment variables so they are not passed to any child processes
func UnsetSystemdListenVars() {