Skip to content

Commit

Permalink
feat: Drop unavailable peer connection when network connectivity change
Browse files Browse the repository at this point in the history
Signed-off-by: u <hi@mathias.ninja>
  • Loading branch information
matubu committed Apr 19, 2023
1 parent 0c3c723 commit 25fcb44
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/ipfsutil/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"berty.tech/weshnet/pkg/lifecycle"
"berty.tech/weshnet/pkg/logutil"
"berty.tech/weshnet/pkg/netmanager"
)

var (
Expand All @@ -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,
Expand All @@ -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, &currentState, netmanager.ConnectivityStateChanged)

if !ok {
return
}

currentState = net.GetCurrentState()

if net.GetCurrentState().State == netmanager.ConnectivityStateOn {
go cl.dropUnavailableConn()
}
}
}()

return cl, nil
}

Expand Down

0 comments on commit 25fcb44

Please sign in to comment.