Skip to content

Commit

Permalink
grpc: fix deprecate grpc.DialContext and grpc.WithBlock
Browse files Browse the repository at this point in the history
Use `grpc.NewClient` instead of `grpc.DialContext`.
According
https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md
don't use `WithBlock`.

Signed-off-by: Andrey Butusov <[email protected]>
  • Loading branch information
End-rey committed Sep 17, 2024
1 parent 9c03eca commit e0af43c
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/tree/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func add(cmd *cobra.Command, _ []string) {
meta, err := parseMeta(cmd)
common.ExitOnErr(cmd, "meta data parsing: %w", err)

cli, err := _client(ctx)
cli, err := _client()

Check warning on line 53 in cmd/neofs-cli/modules/tree/add.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/add.go#L53

Added line #L53 was not covered by tests
common.ExitOnErr(cmd, "client: %w", err)

rawCID := make([]byte, sha256.Size)
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/tree/add_by_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func addByPath(cmd *cobra.Command, _ []string) {

tid, _ := cmd.Flags().GetString(treeIDFlagKey)

cli, err := _client(ctx)
cli, err := _client()

Check warning on line 57 in cmd/neofs-cli/modules/tree/add_by_path.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/add_by_path.go#L57

Added line #L57 was not covered by tests
common.ExitOnErr(cmd, "client: %w", err)

rawCID := make([]byte, sha256.Size)
Expand Down
14 changes: 3 additions & 11 deletions cmd/neofs-cli/modules/tree/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tree

import (
"context"
"strings"
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/network"
Expand All @@ -15,26 +13,20 @@ import (

// _client returns grpc Tree service client. Should be removed
// after making Tree API public.
func _client(ctx context.Context) (tree.TreeServiceClient, error) {
func _client() (tree.TreeServiceClient, error) {

Check warning on line 16 in cmd/neofs-cli/modules/tree/client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/client.go#L16

Added line #L16 was not covered by tests
var netAddr network.Address
err := netAddr.FromString(viper.GetString(commonflags.RPC))
if err != nil {
return nil, err
}

opts := make([]grpc.DialOption, 1, 2)
opts[0] = grpc.WithBlock()
opts := make([]grpc.DialOption, 0, 1)

Check warning on line 23 in cmd/neofs-cli/modules/tree/client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/client.go#L23

Added line #L23 was not covered by tests

if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

// a default connection establishing timeout
const defaultClientConnectTimeout = time.Second * 2

ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...)
cancel()
cc, err := grpc.NewClient(netAddr.URIAddr(), opts...)

Check warning on line 29 in cmd/neofs-cli/modules/tree/client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/client.go#L29

Added line #L29 was not covered by tests

return tree.NewTreeServiceClient(cc), err
}
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/tree/get_by_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func getByPath(cmd *cobra.Command, _ []string) {

tid, _ := cmd.Flags().GetString(treeIDFlagKey)

cli, err := _client(ctx)
cli, err := _client()

Check warning on line 57 in cmd/neofs-cli/modules/tree/get_by_path.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/get_by_path.go#L57

Added line #L57 was not covered by tests
common.ExitOnErr(cmd, "client: %w", err)

rawCID := make([]byte, sha256.Size)
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/tree/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func list(cmd *cobra.Command, _ []string) {
err := cnr.DecodeString(cidString)
common.ExitOnErr(cmd, "decode container ID string: %w", err)

cli, err := _client(ctx)
cli, err := _client()

Check warning on line 45 in cmd/neofs-cli/modules/tree/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/tree/list.go#L45

Added line #L45 was not covered by tests
common.ExitOnErr(cmd, "client: %w", err)

rawCID := make([]byte, sha256.Size)
Expand Down
21 changes: 7 additions & 14 deletions pkg/services/tree/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tree

import (
"context"
"errors"
"fmt"
"strings"
Expand All @@ -26,9 +25,8 @@ type cacheItem struct {
}

const (
defaultClientCacheSize = 10
defaultClientConnectTimeout = time.Second * 2
defaultReconnectInterval = time.Second * 15
defaultClientCacheSize = 10
defaultReconnectInterval = time.Second * 15
)

var errRecentlyFailed = errors.New("client has recently failed")
Expand All @@ -42,7 +40,7 @@ func (c *clientCache) init() {
c.LRU = *l
}

func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceClient, error) {
func (c *clientCache) get(netmapAddr string) (TreeServiceClient, error) {

Check warning on line 43 in pkg/services/tree/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/cache.go#L43

Added line #L43 was not covered by tests
c.Lock()
ccInt, ok := c.LRU.Get(netmapAddr)
c.Unlock()
Expand All @@ -61,7 +59,7 @@ func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceCl
}
}

cc, err := dialTreeService(ctx, netmapAddr)
cc, err := dialTreeService(netmapAddr)

Check warning on line 62 in pkg/services/tree/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/cache.go#L62

Added line #L62 was not covered by tests
lastTry := time.Now()

c.Lock()
Expand All @@ -79,23 +77,18 @@ func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceCl
return NewTreeServiceClient(cc), nil
}

func dialTreeService(ctx context.Context, netmapAddr string) (*grpc.ClientConn, error) {
func dialTreeService(netmapAddr string) (*grpc.ClientConn, error) {

Check warning on line 80 in pkg/services/tree/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/cache.go#L80

Added line #L80 was not covered by tests
var netAddr network.Address
if err := netAddr.FromString(netmapAddr); err != nil {
return nil, err
}

opts := make([]grpc.DialOption, 1, 2)
opts[0] = grpc.WithBlock()
opts := make([]grpc.DialOption, 0, 1)

Check warning on line 86 in pkg/services/tree/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/cache.go#L86

Added line #L86 was not covered by tests

// FIXME(@fyrchik): ugly hack #1322
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...)
cancel()

return cc, err
return grpc.NewClient(netAddr.URIAddr(), opts...)

Check warning on line 93 in pkg/services/tree/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/cache.go#L93

Added line #L93 was not covered by tests
}
2 changes: 1 addition & 1 deletion pkg/services/tree/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *Service) forEachNode(ctx context.Context, cntNodes []netmapSDK.NodeInfo
for _, n := range cntNodes {
var stop bool
n.IterateNetworkEndpoints(func(endpoint string) bool {
c, err := s.cache.get(ctx, endpoint)
c, err := s.cache.get(endpoint)

Check warning on line 27 in pkg/services/tree/redirect.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/redirect.go#L27

Added line #L27 was not covered by tests
if err != nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/tree/replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *Service) replicationWorker(ctx context.Context) {
task.n.IterateNetworkEndpoints(func(addr string) bool {
lastAddr = addr

c, err := s.cache.get(ctx, addr)
c, err := s.cache.get(addr)

Check warning on line 69 in pkg/services/tree/replicator.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/replicator.go#L69

Added line #L69 was not covered by tests
if err != nil {
lastErr = fmt.Errorf("can't create client: %w", err)
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/tree/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *Service) synchronizeTree(ctx context.Context, d pilorama.CIDDescriptor,
return false
}

cc, err := grpc.DialContext(ctx, a.URIAddr(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := grpc.NewClient(a.URIAddr(), grpc.WithTransportCredentials(insecure.NewCredentials()))

Check warning on line 159 in pkg/services/tree/sync.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/tree/sync.go#L159

Added line #L159 was not covered by tests
if err != nil {
// Failed to connect, try the next address.
return false
Expand Down

0 comments on commit e0af43c

Please sign in to comment.