clean shutdown logic

This commit is contained in:
vyzo 2019-05-03 12:05:43 +03:00
parent ed58a77cf0
commit 080b7e28e9
1 changed files with 17 additions and 0 deletions

View File

@ -36,6 +36,8 @@ type Daemon struct {
mx sync.Mutex
// stream handlers: map of protocol.ID to multi-address
handlers map[proto.ID]ma.Multiaddr
// closed is set when the daemon is shutting down
closed bool
}
func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtMode string, opts ...libp2p.Option) (*Daemon, error) {
@ -137,9 +139,14 @@ func (d *Daemon) Addrs() []ma.Multiaddr {
func (d *Daemon) listen() {
for {
if d.isClosed() {
return
}
c, err := d.listener.Accept()
if err != nil {
log.Errorf("error accepting connection: %s", err.Error())
continue
}
log.Debug("incoming connection")
@ -147,6 +154,12 @@ func (d *Daemon) listen() {
}
}
func (d *Daemon) isClosed() bool {
d.mx.Lock()
defer d.mx.Unlock()
return d.closed
}
func clearUnixSockets(path ma.Multiaddr) error {
c, _ := ma.SplitFirst(path)
if c.Protocol().Code != ma.P_UNIX {
@ -161,6 +174,10 @@ func clearUnixSockets(path ma.Multiaddr) error {
}
func (d *Daemon) Close() error {
d.mx.Lock()
d.closed = true
d.mx.Unlock()
var merr *multierror.Error
if err := d.host.Close(); err != nil {
merr = multierror.Append(err)