Skip to content

use fx lifecycle hooks to start and stop services #1876

Open
@marten-seemann

Description

@marten-seemann

Starting and stopping services on the host has long been a pain point. By using fx's lifecycle hooks we could potentially dramatically simplify our code base.

This is the Close logic in the BasicHost:

func (h *BasicHost) Close() error {
h.closeSync.Do(func() {
h.ctxCancel()
if h.natmgr != nil {
h.natmgr.Close()
}
if h.cmgr != nil {
h.cmgr.Close()
}
if h.ids != nil {
h.ids.Close()
}
if h.autoNat != nil {
h.autoNat.Close()
}
if h.relayManager != nil {
h.relayManager.Close()
}
if h.hps != nil {
h.hps.Close()
}
_ = h.emitters.evtLocalProtocolsUpdated.Close()
_ = h.emitters.evtLocalAddrsUpdated.Close()
h.Network().Close()
h.psManager.Close()
if h.Peerstore() != nil {
h.Peerstore().Close()
}
h.refCount.Wait()
if h.Network().ResourceManager() != nil {
h.Network().ResourceManager().Close()
}
})
return nil
}

There also exist wrapped hosts only for hooking into the Close method, for example here:

type AutoRelayHost struct {
host.Host
ar *AutoRelay
}
func (h *AutoRelayHost) Close() error {
_ = h.ar.Close()
return h.Host.Close()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/daysEstimated to take multiple days, but less than a weekexp/expertHaving worked on the specific codebase is importantkind/enhancementA net-new feature or improvement to an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions