@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "time"
2222
23+ "github.com/edwarnicke/genericsync"
2324 "github.com/networkservicemesh/api/pkg/api/networkservice"
2425 "google.golang.org/grpc"
2526 "google.golang.org/protobuf/types/known/emptypb"
@@ -32,8 +33,13 @@ import (
3233 "github.com/networkservicemesh/sdk/pkg/tools/postpone"
3334)
3435
36+ type contextAndCancel struct {
37+ context.Context
38+ cancel func ()
39+ }
3540type healClient struct {
3641 chainCtx context.Context
42+ contextMap genericsync.Map [string , contextAndCancel ]
3743 livenessCheck LivenessCheck
3844 livenessCheckInterval time.Duration
3945 livenessCheckTimeout time.Duration
@@ -69,8 +75,12 @@ func (h *healClient) Request(ctx context.Context, request *networkservice.Networ
6975
7076 conn , err := next .Client (ctx ).Request (ctx , request , opts ... )
7177 if err != nil {
78+
7279 if len (request .GetConnection ().GetPath ().GetPathSegments ()) > 1 && h .livenessCheck == nil && ! loopHandle .healingStarted {
73- _ = begin .FromContext (ctx ).Request (begin .WithReselect ())
80+ var healCtx , cancel = context .WithCancel (h .chainCtx )
81+ var ctxAndCancel = contextAndCancel {Context : healCtx , cancel : cancel }
82+ ctxAndCancel , _ = h .contextMap .LoadOrStore (request .GetConnection ().GetId (), ctxAndCancel )
83+ _ = begin .FromContext (ctx ).Request (begin .WithReselect (), begin .CancelContext (ctxAndCancel .Context ))
7484 }
7585 if loaded && ! loopHandle .healingStarted {
7686 eventLoopErr := h .startEventLoop (ctx , request .GetConnection ())
@@ -90,6 +100,9 @@ func (h *healClient) Request(ctx context.Context, request *networkservice.Networ
90100 _ , _ = next .Client (closeCtx ).Close (closeCtx , conn )
91101 return nil , eventLoopErr
92102 }
103+ if v , ok := h .contextMap .LoadAndDelete (request .GetConnection ().GetId ()); ok {
104+ v .cancel ()
105+ }
93106 return conn , nil
94107}
95108
0 commit comments