From b1e54dec9fd45e872760caae1ffe1f331bb2cb91 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 17 Jun 2022 10:36:23 +0300 Subject: [PATCH] [#1461] cli: Add context to `GetCurrentEpoch` helper Signed-off-by: Pavel Karpy --- cmd/neofs-cli/internal/client/sdk.go | 6 +----- cmd/neofs-cli/modules/bearer/create.go | 7 ++++++- cmd/neofs-cli/modules/object/lock.go | 7 ++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go index b79001d95..576b570bf 100644 --- a/cmd/neofs-cli/internal/client/sdk.go +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -7,7 +7,6 @@ import ( "crypto/rand" "errors" "fmt" - "time" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/pkg/network" @@ -60,7 +59,7 @@ func GetSDKClient(key *ecdsa.PrivateKey, addr network.Address) (*client.Client, } // GetCurrentEpoch returns current epoch. -func GetCurrentEpoch(endpoint string) (uint64, error) { +func GetCurrentEpoch(ctx context.Context, endpoint string) (uint64, error) { var addr network.Address if err := addr.FromString(endpoint); err != nil { @@ -77,9 +76,6 @@ func GetCurrentEpoch(endpoint string) (uint64, error) { return 0, err } - ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) - defer cancel() - ni, err := c.NetworkInfo(ctx, client.PrmNetworkInfo{}) if err != nil { return 0, err diff --git a/cmd/neofs-cli/modules/bearer/create.go b/cmd/neofs-cli/modules/bearer/create.go index 4b3c5ff90..e41539d94 100644 --- a/cmd/neofs-cli/modules/bearer/create.go +++ b/cmd/neofs-cli/modules/bearer/create.go @@ -1,9 +1,11 @@ package bearer import ( + "context" "encoding/json" "fmt" "io/ioutil" + "time" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" @@ -69,8 +71,11 @@ func createToken(cmd *cobra.Command, _ []string) error { return err } if iatRelative || expRelative || nvbRelative { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + endpoint, _ := cmd.Flags().GetString(commonflags.RPC) - currEpoch, err := internalclient.GetCurrentEpoch(endpoint) + currEpoch, err := internalclient.GetCurrentEpoch(ctx, endpoint) if err != nil { return err } diff --git a/cmd/neofs-cli/modules/object/lock.go b/cmd/neofs-cli/modules/object/lock.go index 4974a3f72..564375ae6 100644 --- a/cmd/neofs-cli/modules/object/lock.go +++ b/cmd/neofs-cli/modules/object/lock.go @@ -1,8 +1,10 @@ package object import ( + "context" "fmt" "strconv" + "time" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" @@ -52,9 +54,12 @@ var objectLockCmd = &cobra.Command{ common.ExitOnErr(cmd, "Parsing expiration epoch: %w", err) if relative { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + endpoint, _ := cmd.Flags().GetString(commonflags.RPC) - currEpoch, err := internalclient.GetCurrentEpoch(endpoint) + currEpoch, err := internalclient.GetCurrentEpoch(ctx, endpoint) common.ExitOnErr(cmd, "Request current epoch: %w", err) exp += currEpoch