-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
- Loading branch information
1 parent
f96fdf6
commit 45a8479
Showing
8 changed files
with
244 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package netsvcmonitor | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
type cancelFunctionKey struct{} | ||
|
||
func storeCancelFunction(ctx context.Context, cancel func()) { | ||
metadata.Map(ctx, false).Store(cancelFunctionKey{}, cancel) | ||
} | ||
func loadCancelFunction(ctx context.Context) (func(), bool) { | ||
v, ok := metadata.Map(ctx, false).Load(cancelFunctionKey{}) | ||
if ok { | ||
return v.(func()), true | ||
} | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright (c) 2020-2022 Cisco Systems, Inc. | ||
// | ||
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package monitor provides a NetworkServiceServer chain element to provide a monitor server that reflects | ||
// the connections actually in the NetworkServiceServer | ||
package netsvcmonitor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
"github.com/networkservicemesh/sdk/pkg/tools/matchutils" | ||
) | ||
|
||
type Sender interface { | ||
Send(event *networkservice.ConnectionEvent) (err error) | ||
} | ||
|
||
type monitorServer struct { | ||
chainCtx context.Context | ||
s *networkservice.MonitorConnectionServer | ||
nsClient registry.NetworkServiceRegistryClient | ||
nseClient registry.NetworkServiceEndpointRegistryClient | ||
} | ||
|
||
func NewServer(chainCtx context.Context, nsClient registry.NetworkServiceRegistryClient, nseClient registry.NetworkServiceEndpointRegistryClient, s *networkservice.MonitorConnectionServer) networkservice.NetworkServiceServer { | ||
return &monitorServer{ | ||
chainCtx: chainCtx, | ||
nsClient: nsClient, | ||
nseClient: nseClient, | ||
s: s, | ||
} | ||
} | ||
|
||
func (m *monitorServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
if cancel, ok := loadCancelFunction(ctx); ok { | ||
cancel() | ||
} | ||
|
||
resp, err := next.Server(ctx).Request(ctx, request) | ||
|
||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
var monitorCtx, cancel = context.WithCancel(context.Background()) | ||
|
||
storeCancelFunction(ctx, cancel) | ||
|
||
var logger = log.FromContext(ctx).WithField("monitorServer", "Find") | ||
|
||
var monitorNetworkServiceGoroutine func() | ||
|
||
monitorNetworkServiceGoroutine = func() { | ||
var errorsLimit = 100 | ||
var stream registry.NetworkServiceRegistry_FindClient | ||
var err error | ||
for errorsLimit > 0 { | ||
if monitorCtx.Err() != nil { | ||
return | ||
} | ||
stream, err = m.nsClient.Find(monitorCtx, ®istry.NetworkServiceQuery{ | ||
Watch: true, | ||
NetworkService: ®istry.NetworkService{ | ||
Name: resp.GetNetworkService(), | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
logger.Errorf("an error happened during finding network service: %v", err.Error()) | ||
errorsLimit-- | ||
time.Sleep(time.Millisecond * 100) | ||
continue | ||
} | ||
break | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
var networkServiceCh = registry.ReadNetworkServiceChannel(stream) | ||
|
||
for { | ||
select { | ||
case <-monitorCtx.Done(): | ||
return | ||
case netsvc, ok := <-networkServiceCh: | ||
if !ok { | ||
if monitorCtx.Err() == nil { | ||
//go monitorNetworkServiceGoroutine() | ||
} | ||
return | ||
} | ||
if monitorCtx.Err() != nil { | ||
return | ||
} | ||
|
||
nseStream, err := m.nseClient.Find(monitorCtx, ®istry.NetworkServiceEndpointQuery{ | ||
NetworkServiceEndpoint: ®istry.NetworkServiceEndpoint{ | ||
Name: resp.GetNetworkServiceEndpointName(), | ||
}, | ||
}) | ||
if err != nil { | ||
logger.Errorf("an error happened during finding nse: %v", err.Error()) | ||
if monitorCtx.Err() == nil { | ||
//go monitorNetworkServiceGoroutine() | ||
} | ||
return | ||
} | ||
|
||
var nses = registry.ReadNetworkServiceEndpointList(nseStream) | ||
|
||
if len(matchutils.MatchEndpoint(resp.GetLabels(), netsvc.NetworkService, nses...)) == 0 { | ||
if v, ok := (*m.s).(Sender); ok { | ||
v.Send(&networkservice.ConnectionEvent{ | ||
Type: networkservice.ConnectionEventType_DELETE, | ||
Connections: map[string]*networkservice.Connection{ | ||
resp.GetId(): request.GetConnection(), | ||
}, | ||
}) | ||
} | ||
|
||
} | ||
} | ||
time.Sleep(time.Millisecond * 50) | ||
} | ||
|
||
} | ||
|
||
go monitorNetworkServiceGoroutine() | ||
|
||
return resp, err | ||
|
||
} | ||
|
||
func (m *monitorServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { | ||
if cancel, ok := loadCancelFunction(ctx); ok { | ||
cancel() | ||
} | ||
|
||
return next.Server(ctx).Close(ctx, conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters