mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
Support getting listeners from systemd
Add a new module for getting listener sockets via systemd's file descriptor passing (see sd_listen_fds(3) for more details), and make the main daemon use it when "systemd" is given an address.
This commit is contained in:
32
chasquid.go
32
chasquid.go
@@ -17,6 +17,7 @@ import (
|
||||
"time"
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/config"
|
||||
"blitiri.com.ar/go/chasquid/internal/systemd"
|
||||
|
||||
_ "net/http/pprof"
|
||||
|
||||
@@ -69,14 +70,28 @@ func main() {
|
||||
}
|
||||
|
||||
// Load addresses.
|
||||
acount := 0
|
||||
for _, addr := range conf.Address {
|
||||
// The "systemd" address indicates we get listeners via systemd.
|
||||
if addr == "systemd" {
|
||||
// TODO
|
||||
ls, err := systemd.Listeners()
|
||||
if err != nil {
|
||||
glog.Fatalf("Error getting listeners via systemd: %v", err)
|
||||
}
|
||||
s.AddListeners(ls)
|
||||
acount += len(ls)
|
||||
} else {
|
||||
s.AddAddr(addr)
|
||||
acount++
|
||||
}
|
||||
}
|
||||
|
||||
if acount == 0 {
|
||||
glog.Errorf("No addresses/listeners configured")
|
||||
glog.Errorf("If using systemd, check that you started chasquid.socket")
|
||||
glog.Fatalf("Exiting")
|
||||
}
|
||||
|
||||
s.ListenAndServe()
|
||||
}
|
||||
|
||||
@@ -93,6 +108,9 @@ type Server struct {
|
||||
// Addresses.
|
||||
addrs []string
|
||||
|
||||
// Listeners (that came via systemd).
|
||||
listeners []net.Listener
|
||||
|
||||
// TLS config.
|
||||
tlsConfig *tls.Config
|
||||
|
||||
@@ -119,6 +137,10 @@ func (s *Server) AddAddr(a string) {
|
||||
s.addrs = append(s.addrs, a)
|
||||
}
|
||||
|
||||
func (s *Server) AddListeners(ls []net.Listener) {
|
||||
s.listeners = append(s.listeners, ls...)
|
||||
}
|
||||
|
||||
func (s *Server) getTLSConfig() (*tls.Config, error) {
|
||||
var err error
|
||||
conf := &tls.Config{}
|
||||
@@ -159,6 +181,14 @@ func (s *Server) ListenAndServe() {
|
||||
go s.serve(l)
|
||||
}
|
||||
|
||||
for _, l := range s.listeners {
|
||||
defer l.Close()
|
||||
glog.Infof("Server listening on %s (via systemd)", l.Addr())
|
||||
|
||||
// Serve.
|
||||
go s.serve(l)
|
||||
}
|
||||
|
||||
// Never return. If the serve goroutines have problems, they will abort
|
||||
// execution.
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user