systemd.go 741 B

12345678910111213141516171819202122232425
  1. package guardiand
  2. import (
  3. "fmt"
  4. "github.com/coreos/go-systemd/activation"
  5. "net"
  6. )
  7. func getSDListeners() ([]net.Listener, error) {
  8. // We use systemd socket activation for (almost) zero downtime deployment -
  9. // systemd will keep the socket open even while we restart the process
  10. // (plus, it allows us to bind to unprivileged ports without extra capabilities).
  11. //
  12. // Read more: https://vincent.bernat.ch/en/blog/2018-systemd-golang-socket-activation
  13. listeners, err := activation.Listeners()
  14. if err != nil {
  15. return nil, fmt.Errorf("cannot retrieve listeners: %v", err)
  16. }
  17. if len(listeners) != 1 {
  18. return nil, fmt.Errorf("unexpected number of sockets passed by systemd (%d != 1)", len(listeners))
  19. }
  20. return listeners, nil
  21. }