提交 0d0b4fc8 作者: Steven Allen

feat(systemd): extract socket activation logic

上级 37c19cc6
......@@ -15,7 +15,6 @@ import (
version "github.com/ipfs/go-ipfs"
config "github.com/ipfs/go-ipfs-config"
cserial "github.com/ipfs/go-ipfs-config/serialize"
sockets "github.com/ipfs/go-ipfs/cmd/ipfs/sockets"
utilmain "github.com/ipfs/go-ipfs/cmd/ipfs/util"
oldcmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
......@@ -27,6 +26,7 @@ import (
nodeMount "github.com/ipfs/go-ipfs/fuse/node"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
sockets "github.com/libp2p/go-socket-activation"
"github.com/hashicorp/go-multierror"
cmds "github.com/ipfs/go-ipfs-cmds"
......@@ -440,7 +440,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
return nil, fmt.Errorf("serveHTTPApi: GetConfig() failed: %s", err)
}
listeners, err := sockets.TakeSockets("io.ipfs.api")
listeners, err := sockets.TakeListeners("io.ipfs.api")
if err != nil {
return nil, fmt.Errorf("serveHTTPApi: socket activation failed: %s", err)
}
......@@ -580,7 +580,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
writable = cfg.Gateway.Writable
}
listeners, err := sockets.TakeSockets("io.ipfs.gateway")
listeners, err := sockets.TakeListeners("io.ipfs.gateway")
if err != nil {
return nil, fmt.Errorf("serveHTTPGateway: socket activation failed: %s", err)
}
......
// +build !linux
package sockets
import (
manet "github.com/multiformats/go-multiaddr-net"
)
// TakeSockets takes the sockets associated with the given name.
func TakeSockets(name string) ([]manet.Listener, error) {
return nil, nil
}
// +build linux
package sockets
import (
"sync"
activation "github.com/coreos/go-systemd/activation"
logging "github.com/ipfs/go-log"
manet "github.com/multiformats/go-multiaddr-net"
)
var log = logging.Logger("socket-activation")
var (
socketsMu sync.Mutex
sockets map[string][]manet.Listener
)
func initSockets() {
if sockets != nil {
return
}
nlisteners, err := activation.ListenersWithNames()
// Do this before checking the error. We need this to be non-nil so we
// don't try again.
sockets = make(map[string][]manet.Listener, len(nlisteners))
if err != nil {
log.Errorf("error parsing systemd sockets: %s", err)
return
}
for name, nls := range nlisteners {
mls := make([]manet.Listener, 0, len(nls))
for _, nl := range nls {
ml, err := manet.WrapNetListener(nl)
if err != nil {
log.Errorf("error converting a systemd-socket to a multiaddr listener: %s", err)
nl.Close()
continue
}
mls = append(mls, ml)
}
sockets[name] = mls
}
}
// TakeSockets takes the sockets associated with the given name.
func TakeSockets(name string) ([]manet.Listener, error) {
socketsMu.Lock()
defer socketsMu.Unlock()
initSockets()
s := sockets[name]
delete(sockets, name)
return s, nil
}
......@@ -79,6 +79,7 @@ require (
github.com/libp2p/go-libp2p-tls v0.1.1
github.com/libp2p/go-libp2p-yamux v0.2.1
github.com/libp2p/go-maddr-filter v0.0.5
github.com/libp2p/go-socket-activation v0.0.1
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mr-tron/base58 v1.1.2
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论