fix a race and optimize hello messages

LifecycleCtx can _only_ be called during startup as it appends an fx hook.
Worse, this was causing us to append an fx hook on every single hello message,
leaking memory (and probably causing other shutdown issues...).
This commit is contained in:
Steven Allen 2020-10-10 08:33:06 -07:00
parent 7245ac2b69
commit c463582528

View File

@ -42,11 +42,13 @@ func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello.
return xerrors.Errorf("failed to subscribe to event bus: %w", err) return xerrors.Errorf("failed to subscribe to event bus: %w", err)
} }
ctx := helpers.LifecycleCtx(mctx, lc)
go func() { go func() {
for evt := range sub.Out() { for evt := range sub.Out() {
pic := evt.(event.EvtPeerIdentificationCompleted) pic := evt.(event.EvtPeerIdentificationCompleted)
go func() { go func() {
if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), pic.Peer); err != nil { if err := svc.SayHello(ctx, pic.Peer); err != nil {
protos, _ := h.Peerstore().GetProtocols(pic.Peer) protos, _ := h.Peerstore().GetProtocols(pic.Peer)
agent, _ := h.Peerstore().Get(pic.Peer, "AgentVersion") agent, _ := h.Peerstore().Get(pic.Peer, "AgentVersion")
if protosContains(protos, hello.ProtocolID) { if protosContains(protos, hello.ProtocolID) {