Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework adapters #543

Merged
merged 4 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/networkservice/common/updatetoken/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (f *updateTokenServerSuite) TestChain() {

got, err := server.Request(context.Background(), request)
require.Equal(t, 3, len(got.Path.PathSegments))
require.Equal(t, 2, int(got.Path.Index))
require.Equal(t, 0, int(got.Path.Index))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context:

elements := []networkservice.NetworkServiceServer{
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("nsc-1"), updatetoken.NewClient(TokenGenerator))),
updatepath.NewServer("local-nsm-1"),
updatetoken.NewServer(TokenGenerator),
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("local-nsm-1"), updatetoken.NewClient(TokenGenerator))),
updatepath.NewServer("remote-nsm-1"),
updatetoken.NewServer(TokenGenerator),
adapters.NewClientToServer(next.NewNetworkServiceClient(updatepath.NewClient("remote-nsm-1"), updatetoken.NewClient(TokenGenerator)))}
server := next.NewNetworkServiceServer(elements...)
got, err := server.Request(context.Background(), request)
require.Equal(t, 3, len(got.Path.PathSegments))
require.Equal(t, 0, int(got.Path.Index))

Path.Index is now restored by updatetoken on line 215.
This is changed because of the restored nesting order.

for i, s := range got.Path.PathSegments {
require.Equal(t, want.Path.PathSegments[i].Name, s.Name)
require.Equal(t, want.Path.PathSegments[i].Token, s.Token)
Expand Down
11 changes: 11 additions & 0 deletions pkg/networkservice/core/adapters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Intro

This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xzfc Could you add the same README.md file for registry adapters?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.
## Intro
This package provides adapters to translate between `NetworkServiceServer` and `NetworkServiceClient` interfaces.


## Benchmarks

| Benchmark | ns/op | allocs/op |
|-------------------------------------|------:|----------:|
| `BenchmarkServerToClient_Request` | 288 | 6 |
| `BenchmarkClientToServer_Request` | 291 | 6 |
| `BenchmarkNested20Adapters_Request` | 6159 | 120 |
40 changes: 0 additions & 40 deletions pkg/networkservice/core/adapters/capturecontext_server.go

This file was deleted.

58 changes: 0 additions & 58 deletions pkg/networkservice/core/adapters/client_test.go

This file was deleted.

51 changes: 21 additions & 30 deletions pkg/networkservice/core/adapters/client_to_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,38 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"

"github.com/networkservicemesh/api/pkg/api/networkservice"
)

type clientToServer struct {
client networkservice.NetworkServiceClient
}
type (
clientToServer struct {
client networkservice.NetworkServiceClient
}
callNextServer struct {
server networkservice.NetworkServiceServer
}
)

// NewClientToServer - returns a networkservice.NetworkServiceServer wrapped around the supplied client
func NewClientToServer(client networkservice.NetworkServiceClient) networkservice.NetworkServiceServer {
return &clientToServer{client: next.NewNetworkServiceClient(client, &contextClient{})}
return &clientToServer{client: client}
}

func (c *clientToServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
doneCtx := withCapturedContext(ctx)
conn, err := c.client.Request(doneCtx, request)
if err != nil {
return nil, err
}
lastCtx := getCapturedContext(doneCtx)
if lastCtx == nil {
return conn, nil
}
if request == nil {
request = &networkservice.NetworkServiceRequest{}
}
request.Connection = conn
return next.Server(ctx).Request(lastCtx, request)
return next.NewNetworkServiceClient(c.client, &callNextServer{server: next.Server(ctx)}).Request(ctx, request)
}

func (c *clientToServer) Close(ctx context.Context, request *networkservice.Connection) (*empty.Empty, error) {
doneCtx := withCapturedContext(ctx)
conn, err := c.client.Close(doneCtx, request)
if err != nil {
return nil, err
}
lastCtx := getCapturedContext(doneCtx)
if lastCtx == nil {
return conn, nil
}
return next.Server(ctx).Close(lastCtx, request)
return next.NewNetworkServiceClient(c.client, &callNextServer{server: next.Server(ctx)}).Close(ctx, request)
}

func (c *callNextServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, _ ...grpc.CallOption) (*networkservice.Connection, error) {
return c.server.Request(ctx, request)
}

func (c *callNextServer) Close(ctx context.Context, request *networkservice.Connection, _ ...grpc.CallOption) (*empty.Empty, error) {
return c.server.Close(ctx, request)
}
53 changes: 0 additions & 53 deletions pkg/networkservice/core/adapters/context.go

This file was deleted.

Loading