From 44902a13be29836857ac1186a864474a0a47d701 Mon Sep 17 00:00:00 2001 From: NikitaSkrynnik Date: Wed, 26 Jun 2024 19:45:28 +0700 Subject: [PATCH] Some changes that fix interface leaks Signed-off-by: NikitaSkrynnik --- .../common/monitor/eventloop.go | 18 ++++++----- pkg/networkservice/common/monitor/server.go | 30 +++++++++---------- pkg/networkservice/common/timeout/server.go | 18 ++++++++++- .../common/updatepath/client.go | 11 +++++-- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/pkg/networkservice/common/monitor/eventloop.go b/pkg/networkservice/common/monitor/eventloop.go index 0519ab8f4..884dc5a41 100644 --- a/pkg/networkservice/common/monitor/eventloop.go +++ b/pkg/networkservice/common/monitor/eventloop.go @@ -78,15 +78,17 @@ func (cev *eventLoop) eventLoop() { } if err != nil { // If we get an error, we've lost our connection... Send Down update - connOut := cev.conn.Clone() - connOut.State = networkservice.State_DOWN - eventOut := &networkservice.ConnectionEvent{ - Type: networkservice.ConnectionEventType_UPDATE, - Connections: map[string]*networkservice.Connection{ - cev.conn.GetId(): connOut, - }, + connOut := cev.conn.Clon e() + if connOut != nil { + connOut.State = networkservice.State_DOWN + eventOut := &networkservice.ConnectionEvent{ + Type: networkservice.ConnectionEventType_UPDATE, + Connections: map[string]*networkservice.Connection{ + cev.conn.GetId(): connOut, + }, + } + _ = cev.eventConsumer.Send(eventOut) } - _ = cev.eventConsumer.Send(eventOut) return } _ = cev.eventConsumer.Send(eventIn) diff --git a/pkg/networkservice/common/monitor/server.go b/pkg/networkservice/common/monitor/server.go index af496ddda..73432f6a9 100644 --- a/pkg/networkservice/common/monitor/server.go +++ b/pkg/networkservice/common/monitor/server.go @@ -24,11 +24,8 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" - "github.com/pkg/errors" - "github.com/networkservicemesh/sdk/pkg/networkservice/common/clientconn" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" - "github.com/networkservicemesh/sdk/pkg/tools/postpone" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -58,7 +55,7 @@ func NewServer(chainCtx context.Context, monitorServerPtr *networkservice.Monito } func (m *monitorServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { - closeCtxFunc := postpone.ContextWithValues(ctx) + // closeCtxFunc := postpone.ContextWithValues(ctx) // Cancel any existing eventLoop cancelEventLoop, loaded := loadAndDelete(ctx, metadata.IsClient(m)) if loaded { @@ -86,17 +83,20 @@ func (m *monitorServer) Request(ctx context.Context, request *networkservice.Net // If we have a clientconn ... we must be part of a passthrough server, and have a client to pass // events through from, so start an eventLoop - cc, ccLoaded := clientconn.Load(ctx) - if ccLoaded { - cancelEventLoop, eventLoopErr := newEventLoop(m.chainCtx, m.MonitorConnectionServer.(EventConsumer), cc, conn) - if eventLoopErr != nil { - closeCtx, closeCancel := closeCtxFunc() - defer closeCancel() - _, _ = next.Client(closeCtx).Close(closeCtx, conn) - return nil, errors.Wrap(eventLoopErr, "unable to monitor") - } - store(ctx, metadata.IsClient(m), cancelEventLoop) - } + // cc, ccLoaded := clientconn.Load(ctx) + // log.FromContext(ctx).Infof("ccLoaded") + // if ccLoaded { + // log.FromContext(ctx).Infof("newEventLoop") + // cancelEventLoop, eventLoopErr := newEventLoop(m.chainCtx, m.MonitorConnectionServer.(EventConsumer), cc, conn) + // if eventLoopErr != nil { + // closeCtx, closeCancel := closeCtxFunc() + // defer closeCancel() + // _, _ = next.Client(closeCtx).Close(closeCtx, conn) + // return nil, errors.Wrap(eventLoopErr, "unable to monitor") + // } + // log.FromContext(ctx).Infof("STORE") + // store(ctx, metadata.IsClient(m), cancelEventLoop) + // } return conn, nil } diff --git a/pkg/networkservice/common/timeout/server.go b/pkg/networkservice/common/timeout/server.go index 277028fea..233e7af88 100644 --- a/pkg/networkservice/common/timeout/server.go +++ b/pkg/networkservice/common/timeout/server.go @@ -30,6 +30,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/common/begin" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" "github.com/networkservicemesh/sdk/pkg/tools/clock" + "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -59,9 +60,12 @@ func (s *timeoutServer) Request(ctx context.Context, request *networkservice.Net } conn, err = next.Server(ctx).Request(ctx, request) + logger := log.FromContext(ctx).WithField("TIMEOUT", "") + logger.Infof("BACKWARD") if err != nil { return nil, err } + logger.Infof("NO ERROR, CALCULATING TIMEOUT") expirationTimestamp := conn.GetPrevPathSegment().GetExpires() if expirationTimestamp == nil { @@ -72,14 +76,26 @@ func (s *timeoutServer) Request(ctx context.Context, request *networkservice.Net if oldCancel, loaded := loadAndDelete(ctx, metadata.IsClient(s)); loaded { oldCancel() } + + logger.Infof("EXPIRATION TIME: %v", expirationTime.String()) + + logger.Infof("STORE CANCEL TIMEOUT") + store(ctx, metadata.IsClient(s), cancel) eventFactory := begin.FromContext(ctx) - afterCh := timeClock.After(timeClock.Until(expirationTime) - requestTimeout) + after := timeClock.Until(expirationTime) - requestTimeout + + // after /= 3 + + logger.Infof("TIMEOUT SHOULD OCCUR AFTER: %v", after.String()) + afterCh := timeClock.After(after) go func(cancelCtx context.Context, afterCh <-chan time.Time) { select { case <-cancelCtx.Done(): + logger.Infof("<-cancelCtx.Done()") case <-afterCh: + logger.Infof("<-afterCh") eventFactory.Close(begin.CancelContext(cancelCtx)) } }(cancelCtx, afterCh) diff --git a/pkg/networkservice/common/updatepath/client.go b/pkg/networkservice/common/updatepath/client.go index 145c137a7..d97f77a26 100644 --- a/pkg/networkservice/common/updatepath/client.go +++ b/pkg/networkservice/common/updatepath/client.go @@ -56,8 +56,15 @@ func (i *updatePathClient) Request(ctx context.Context, request *networkservice. return nil, err } - conn.Id = conn.Path.PathSegments[index].Id - conn.Path.Index = index + segments := conn.GetPath().GetPathSegments() + if segments != nil && len(segments) > int(index) { + conn.Id = segments[index].Id + } + + path := conn.GetPath() + if path != nil { + path.Index = index + } return conn, nil }