Skip to content

Commit

Permalink
add mutex
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik committed May 25, 2022
1 parent b519e2c commit cd7d7ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/networkservice/common/dial/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ func (d *dialClient) Request(ctx context.Context, request *networkservice.Networ
return next.Client(ctx).Request(ctx, request, opts...)
}

grpcutils.UrlMut.Lock()
condition := di.clientURL != nil && di.clientURL.String() != clientURL.String()
grpcutils.UrlMut.Unlock()

// If our existing dialer has a different URL close down the chain
if di.clientURL != nil && di.clientURL.String() != clientURL.String() {
if condition {
closeCtx, closeCancel := closeContextFunc()
defer closeCancel()
err := di.Dial(closeCtx, di.clientURL)
Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/common/dial/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ func (c *dialNSClient) Register(ctx context.Context, in *registry.NetworkService
return next.NetworkServiceRegistryClient(ctx).Register(ctx, in, opts...)
}

grpcutils.UrlMut.Lock()
condition := di.clientURL != nil && di.clientURL.String() != clientURL.String()
grpcutils.UrlMut.Unlock()

// If our existing dialer has a different URL close down the chain
if di.clientURL != nil && di.clientURL.String() != clientURL.String() {
if condition {
closeCtx, closeCancel := closeContextFunc()
defer closeCancel()
err := di.Dial(closeCtx, di.clientURL)
Expand Down
5 changes: 5 additions & 0 deletions pkg/tools/grpcutils/listen_and_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/url"
"os"
"path"
"sync"

"github.com/networkservicemesh/sdk/pkg/tools/log"

Expand All @@ -37,6 +38,8 @@ const (
tcpScheme = "tcp"
)

var UrlMut sync.Mutex

// ListenAndServe listens on address with server. Returns an chan err which will
// receive an error and then be closed in the event that server.Serve(listener) returns an error.
func ListenAndServe(ctx context.Context, address *url.URL, server *grpc.Server) <-chan error {
Expand Down Expand Up @@ -66,8 +69,10 @@ func ListenAndServe(ctx context.Context, address *url.URL, server *grpc.Server)
ln, err := net.Listen(network, target)

if ln != nil {
UrlMut.Lock()
// We need to pass a real listener address into context, since we could specify random port.
*address = *AddressToURL(ln.Addr())
UrlMut.Unlock()
}

// Serve
Expand Down

0 comments on commit cd7d7ef

Please sign in to comment.