Skip to content

Commit

Permalink
rework retry
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin committed Dec 14, 2023
1 parent 1979cc1 commit 337314d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 2 additions & 0 deletions pkg/networkservice/chains/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewClient(ctx context.Context, clientOpts ...Option) networkservice.Network
var opts = &clientOptions{
name: "client-" + uuid.New().String(),
authorizeClient: null.NewClient(),
retryClient: null.NewClient(),
healClient: null.NewClient(),
refreshClient: refresh.NewClient(ctx),
}
Expand All @@ -54,6 +55,7 @@ func NewClient(ctx context.Context, clientOpts ...Option) networkservice.Network
[]networkservice.NetworkServiceClient{
updatepath.NewClient(opts.name),
begin.NewClient(),
opts.retryClient,
metadata.NewClient(),
opts.refreshClient,
clienturl.NewClient(opts.clientURL),
Expand Down
7 changes: 7 additions & 0 deletions pkg/networkservice/chains/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ type clientOptions struct {
authorizeClient networkservice.NetworkServiceClient
refreshClient networkservice.NetworkServiceClient
healClient networkservice.NetworkServiceClient
retryClient networkservice.NetworkServiceClient
dialOptions []grpc.DialOption
dialTimeout time.Duration
}

// Option modifies default client chain values.
type Option func(c *clientOptions)

func WithRetry(client networkservice.NetworkServiceClient) Option {

Check failure on line 45 in pkg/networkservice/chains/client/options.go

View workflow job for this annotation

GitHub Actions / golangci-lint / golangci-lint

exported function `WithRetry` should have comment or be unexported (golint)
return func(c *clientOptions) {
c.retryClient = client
}
}

// WithName sets name for the client.
func WithName(name string) Option {
return Option(func(c *clientOptions) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/networkservice/common/retry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)
Expand Down Expand Up @@ -53,11 +54,10 @@ func WithInterval(interval time.Duration) Option {
}

// NewClient - returns a connect chain element
func NewClient(client networkservice.NetworkServiceClient, opts ...Option) networkservice.NetworkServiceClient {
func NewClient(opts ...Option) networkservice.NetworkServiceClient {
var result = &retryClient{
interval: time.Millisecond * 200,
tryTimeout: time.Second * 15,
client: client,
}

for _, opt := range opts {
Expand All @@ -73,7 +73,7 @@ func (r *retryClient) Request(ctx context.Context, request *networkservice.Netwo

for ctx.Err() == nil {
requestCtx, cancel := c.WithTimeout(ctx, r.tryTimeout)
resp, err := r.client.Request(requestCtx, request.Clone(), opts...)
resp, err := next.Client(ctx).Request(requestCtx, request.Clone(), opts...)
cancel()

if err != nil {
Expand All @@ -100,7 +100,7 @@ func (r *retryClient) Close(ctx context.Context, conn *networkservice.Connection
for ctx.Err() == nil {
closeCtx, cancel := c.WithTimeout(ctx, r.tryTimeout)

resp, err := r.client.Close(closeCtx, conn.Clone(), opts...)
resp, err := next.Client(ctx).Close(closeCtx, conn.Clone(), opts...)
cancel()

if err != nil {
Expand Down
33 changes: 17 additions & 16 deletions pkg/networkservice/common/retry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ func Test_RetryClient_Request(t *testing.T) {

var counter = new(count.Client)

var client = retry.NewClient(
var client = chain.NewNetworkServiceClient(
chain.NewNetworkServiceClient(
retry.NewClient(retry.WithInterval(time.Millisecond*10), retry.WithTryTimeout(time.Second/30)),
counter,
&remoteSideClient{
delay: time.Millisecond * 10,
failRequestCount: 5,
},
),
retry.WithInterval(time.Millisecond*10),
retry.WithTryTimeout(time.Second/30),
)

var _, err = client.Request(context.Background(), nil)
Expand All @@ -99,13 +98,14 @@ func Test_RetryClient_Request_ContextHasCorrectDeadline(t *testing.T) {

expectedDeadline := clockMock.Now().Add(time.Hour)

var client = retry.NewClient(chain.NewNetworkServiceClient(
var client = chain.NewNetworkServiceClient(
retry.NewClient(retry.WithTryTimeout(time.Hour)),
checkcontext.NewClient(t, func(t *testing.T, c context.Context) {
v, ok := c.Deadline()
require.True(t, ok)
require.Equal(t, expectedDeadline, v)
}),
), retry.WithTryTimeout(time.Hour))
)

var _, err = client.Request(ctx, nil)
require.NoError(t, err)
Expand All @@ -124,13 +124,14 @@ func Test_RetryClient_Close_ContextHasCorrectDeadline(t *testing.T) {

expectedDeadline := clockMock.Now().Add(time.Hour)

var client = retry.NewClient(chain.NewNetworkServiceClient(
var client = chain.NewNetworkServiceClient(
retry.NewClient(retry.WithTryTimeout(time.Hour)),
checkcontext.NewClient(t, func(t *testing.T, c context.Context) {
v, ok := c.Deadline()
require.True(t, ok)
require.Equal(t, expectedDeadline, v)
}),
), retry.WithTryTimeout(time.Hour))
)

var _, err = client.Close(ctx, nil)
require.NoError(t, err)
Expand All @@ -141,16 +142,16 @@ func Test_RetryClient_Close(t *testing.T) {

var counter = new(count.Client)

var client = retry.NewClient(
chain.NewNetworkServiceClient(
counter,
&remoteSideClient{
delay: time.Millisecond * 10,
failCloseCount: 5,
},
var client = chain.NewNetworkServiceClient(
retry.NewClient(
retry.WithInterval(time.Millisecond*10),
retry.WithTryTimeout(time.Second/30),
),
retry.WithInterval(time.Millisecond*10),
retry.WithTryTimeout(time.Second/30),
counter,
&remoteSideClient{
delay: time.Millisecond * 10,
failCloseCount: 5,
},
)

var _, err = client.Close(context.Background(), nil)
Expand Down
11 changes: 3 additions & 8 deletions pkg/tools/sandbox/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,12 @@ func (n *Node) NewClient(
generatorFunc token.GeneratorFunc,
additionalOpts ...client.Option,
) networkservice.NetworkServiceClient {
opts := []client.Option{
return client.NewClient(ctx,
client.WithClientURL(CloneURL(n.NSMgr.URL)),
client.WithDialOptions(DialOptions(WithTokenGenerator(generatorFunc))...),
client.WithAuthorizeClient(authorize.NewClient(authorize.Any())),
client.WithHealClient(heal.NewClient(ctx)),
client.WithRetry(retry.NewClient()),
client.WithDialTimeout(DialTimeout),
}

opts = append(opts, additionalOpts...)
return retry.NewClient(client.NewClient(
ctx,
opts...,
))
)
}

0 comments on commit 337314d

Please sign in to comment.