From 25fcb446d9d2da3c707377e17d767c0acefb1ec5 Mon Sep 17 00:00:00 2001 From: u Date: Wed, 19 Apr 2023 14:13:26 +0200 Subject: [PATCH] feat: Drop unavailable peer connection when network connectivity change Signed-off-by: u --- pkg/ipfsutil/lifecycle.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/ipfsutil/lifecycle.go b/pkg/ipfsutil/lifecycle.go index 17b2f235..db05b1ba 100644 --- a/pkg/ipfsutil/lifecycle.go +++ b/pkg/ipfsutil/lifecycle.go @@ -15,6 +15,7 @@ import ( "berty.tech/weshnet/pkg/lifecycle" "berty.tech/weshnet/pkg/logutil" + "berty.tech/weshnet/pkg/netmanager" ) var ( @@ -36,7 +37,7 @@ type ConnLifecycle struct { lm *lifecycle.Manager } -func NewConnLifecycle(ctx context.Context, logger *zap.Logger, h host.Host, ps *PeeringService, lm *lifecycle.Manager) (*ConnLifecycle, error) { +func NewConnLifecycle(ctx context.Context, logger *zap.Logger, h host.Host, ps *PeeringService, lm *lifecycle.Manager, net *netmanager.NetManager) (*ConnLifecycle, error) { cl := &ConnLifecycle{ peering: ps, rootCtx: ctx, @@ -55,6 +56,25 @@ func NewConnLifecycle(ctx context.Context, logger *zap.Logger, h host.Host, ps * go cl.monitorAppState(ctx) cl.logger.Debug("lifecycle conn started") + + go func() { + currentState := net.GetCurrentState() + + for { + ok, _ := net.WaitForStateChange(ctx, ¤tState, netmanager.ConnectivityStateChanged) + + if !ok { + return + } + + currentState = net.GetCurrentState() + + if net.GetCurrentState().State == netmanager.ConnectivityStateOn { + go cl.dropUnavailableConn() + } + } + }() + return cl, nil }