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

Add the possible to make chain of chains via pkg/next #173

Merged
merged 5 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
39 changes: 23 additions & 16 deletions pkg/networkservice/core/next/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
)

type nextClient struct {
clients []networkservice.NetworkServiceClient
index int
clients []networkservice.NetworkServiceClient
index int
nextParent networkservice.NetworkServiceClient
}

// ClientWrapper - a function that wraps around a networkservice.NetworkServiceClient
Expand All @@ -40,11 +41,9 @@ type ClientChainer func(...networkservice.NetworkServiceClient) networkservice.N

// NewWrappedNetworkServiceClient chains together clients with wrapper wrapped around each one
func NewWrappedNetworkServiceClient(wrapper ClientWrapper, clients ...networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
rv := &nextClient{
clients: clients,
}
for i := range rv.clients {
rv.clients[i] = wrapper(rv.clients[i])
rv := &nextClient{}
for _, c := range clients {
rv.clients = append(rv.clients, wrapper(c))
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
}
return rv
}
Expand All @@ -54,23 +53,31 @@ func NewNetworkServiceClient(clients ...networkservice.NetworkServiceClient) net
if len(clients) == 0 {
return &tailClient{}
}
return NewWrappedNetworkServiceClient(notWrapClient, clients...)
return NewWrappedNetworkServiceClient(func(client networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
return client
}, clients...)
}

func (n *nextClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if n.index == 0 {
if nextParent := Client(ctx); nextParent != nil {
n.nextParent = nextParent
}
}
if n.index+1 < len(n.clients) {
return n.clients[n.index].Request(withNextClient(ctx, &nextClient{clients: n.clients, index: n.index + 1}), request, opts...)
return n.clients[n.index].Request(withNextClient(ctx, &nextClient{nextParent: n.nextParent, clients: n.clients, index: n.index + 1}), request, opts...)
}
return n.clients[n.index].Request(withNextClient(ctx, nil), request, opts...)
return n.clients[n.index].Request(withNextClient(ctx, n.nextParent), request, opts...)
}

func (n *nextClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
if n.index == 0 {
if nextParent := Client(ctx); nextParent != nil {
n.nextParent = nextParent
}
}
if n.index+1 < len(n.clients) {
return n.clients[n.index].Close(withNextClient(ctx, &nextClient{clients: n.clients, index: n.index + 1}), conn, opts...)
return n.clients[n.index].Close(withNextClient(ctx, &nextClient{nextParent: n.nextParent, clients: n.clients, index: n.index + 1}), conn, opts...)
}
return n.clients[n.index].Close(withNextClient(ctx, nil), conn, opts...)
}

func notWrapClient(c networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
return c
return n.clients[n.index].Close(withNextClient(ctx, n.nextParent), conn, opts...)
}
6 changes: 6 additions & 0 deletions pkg/networkservice/core/next/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func withNextServer(parent context.Context, next networkservice.NetworkServiceSe
// Server -
// Returns the Server networkservice.NetworkServiceServer to be called in the chain from the context.Context
func Server(ctx context.Context) networkservice.NetworkServiceServer {
if ctx == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Question... why return nil here instead of tailServer?

Copy link
Member Author

@denis-tingaikin denis-tingaikin Apr 1, 2020

Choose a reason for hiding this comment

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

actually it can be nil in two cases:

  1. At the first call of method request/close with ctx = nil. In this case, we looking for the next parent chain and value can be nil
  2. Call this method outside of the chain. I.e. user not used next.NewServer() and passed nil context. Not sure that we should return tailServer in this case

Copy link
Member Author

@denis-tingaikin denis-tingaikin Apr 1, 2020

Choose a reason for hiding this comment

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

Finally, I think it will be better just throw nil panic exception for the case №2.
Fixed by 1d5588c

Copy link
Member

Choose a reason for hiding this comment

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

Why would we panic if called outside of a chain?

Copy link
Member Author

@denis-tingaikin denis-tingaikin Apr 2, 2020

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

That's fair :)

return nil
}
rv, ok := ctx.Value(nextServerKey).(networkservice.NetworkServiceServer)
if !ok {
client, ok := ctx.Value(nextClientKey).(networkservice.NetworkServiceClient)
Expand All @@ -74,6 +77,9 @@ func withNextClient(parent context.Context, next networkservice.NetworkServiceCl
// Client -
// Returns the Client networkservice.NetworkServiceClient to be called in the chain from the context.Context
func Client(ctx context.Context) networkservice.NetworkServiceClient {
if ctx == nil {
return nil
}
rv, ok := ctx.Value(nextClientKey).(networkservice.NetworkServiceClient)
if !ok {
server, ok := ctx.Value(nextServerKey).(networkservice.NetworkServiceServer)
Expand Down
39 changes: 23 additions & 16 deletions pkg/networkservice/core/next/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
)

type nextServer struct {
servers []networkservice.NetworkServiceServer
index int
nextParent networkservice.NetworkServiceServer
servers []networkservice.NetworkServiceServer
index int
}

// ServerWrapper - A function that wraps a networkservice.NetworkServiceServer
Expand All @@ -39,11 +40,9 @@ type ServerChainer func(...networkservice.NetworkServiceServer) networkservice.N

// NewWrappedNetworkServiceServer - chains together the servers provides with the wrapper wrapped around each one in turn.
func NewWrappedNetworkServiceServer(wrapper ServerWrapper, servers ...networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
rv := &nextServer{
servers: servers,
}
for i := range rv.servers {
rv.servers[i] = wrapper(rv.servers[i])
rv := &nextServer{}
for _, s := range servers {
rv.servers = append(rv.servers, wrapper(s))
}
return rv
}
Expand All @@ -54,23 +53,31 @@ func NewNetworkServiceServer(servers ...networkservice.NetworkServiceServer) net
if len(servers) == 0 {
return &tailServer{}
}
return NewWrappedNetworkServiceServer(notWrapServer, servers...)
return NewWrappedNetworkServiceServer(func(server networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
return server
}, servers...)
}

func (n *nextServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if n.index == 0 {
if nextParent := Server(ctx); nextParent != nil {
n.nextParent = nextParent
}
}
if n.index+1 < len(n.servers) {
return n.servers[n.index].Request(withNextServer(ctx, &nextServer{servers: n.servers, index: n.index + 1}), request)
return n.servers[n.index].Request(withNextServer(ctx, &nextServer{nextParent: n.nextParent, servers: n.servers, index: n.index + 1}), request)
}
return n.servers[n.index].Request(withNextServer(ctx, nil), request)
return n.servers[n.index].Request(withNextServer(ctx, n.nextParent), request)
}

func (n *nextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
if n.index == 0 {
if nextParent := Server(ctx); nextParent != nil {
n.nextParent = nextParent
}
}
if n.index+1 < len(n.servers) {
return n.servers[n.index].Close(withNextServer(ctx, &nextServer{servers: n.servers, index: n.index + 1}), conn)
return n.servers[n.index].Close(withNextServer(ctx, &nextServer{nextParent: n.nextParent, servers: n.servers, index: n.index + 1}), conn)
}
return n.servers[n.index].Close(withNextServer(ctx, nil), conn)
}

func notWrapServer(server networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
return server
return n.servers[n.index].Close(withNextServer(ctx, n.nextParent), conn)
}
4 changes: 3 additions & 1 deletion pkg/networkservice/core/next/tests/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func TestClientBranches(t *testing.T) {
{visitClient(), visitClient(), adapters.NewServerToClient(visitServer())},
{visitClient(), adapters.NewServerToClient(emptyServer()), visitClient()},
{visitClient(), adapters.NewServerToClient(visitServer()), emptyClient()},
{visitClient(), next.NewNetworkServiceClient(next.NewNetworkServiceClient(visitClient())), visitClient()},
{visitClient(), next.NewNetworkServiceClient(next.NewNetworkServiceClient(emptyClient())), visitClient()},
}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2, 3, 1}
for i, sample := range samples {
msg := fmt.Sprintf("sample index: %v", i)
ctx := visit(context.Background())
Expand Down
4 changes: 3 additions & 1 deletion pkg/networkservice/core/next/tests/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func TestServerBranches(t *testing.T) {
{visitServer(), visitServer(), adapters.NewClientToServer(visitClient())},
{visitServer(), adapters.NewClientToServer(emptyClient()), visitServer()},
{visitServer(), adapters.NewClientToServer(visitClient()), emptyServer()},
{visitServer(), next.NewNetworkServiceServer(next.NewNetworkServiceServer(visitServer())), visitServer()},
{visitServer(), next.NewNetworkServiceServer(next.NewNetworkServiceServer(emptyServer())), visitServer()},
}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2}
expects := []int{1, 2, 3, 0, 1, 2, 1, 2, 3, 3, 1, 2, 3, 1}
for i, sample := range servers {
ctx := visit(context.Background())
s := next.NewNetworkServiceServer(sample...)
Expand Down