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

rpc/client: Use dial timeout properly #419

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions rpc/client/call_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ package client

import (
"context"

"github.com/nspcc-dev/neofs-api-go/v2/rpc/grpc"
)

// CallOption is a messaging session option within Protobuf RPC.
type CallOption func(*callParameters)

type callParameters struct {
callOpts []grpc.CallOption
ctx context.Context
}

func defaultCallParameters() *callParameters {
return &callParameters{
callOpts: make([]grpc.CallOption, 0, 1),
ctx: context.Background(),
}
}

// WithContext return options to specify call context.
// WithContext returns option to specify call context. If provided, all network
// communications will be based on this context. Otherwise, context.Background()
// is used.
//
// Context SHOULD NOT be nil.
func WithContext(ctx context.Context) CallOption {
return func(prm *callParameters) {
prm.callOpts = append(prm.callOpts, grpc.WithContext(ctx))
prm.ctx = ctx
}
}
22 changes: 16 additions & 6 deletions rpc/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func (c *Client) createGRPCClient() (err error) {
func (c *Client) createGRPCClient(ctx context.Context) (err error) {
c.gRPCClientOnce.Do(func() {
if err = c.openGRPCConn(); err != nil {
if err = c.openGRPCConn(ctx); err != nil {
err = fmt.Errorf("open gRPC connection: %w", err)
return
}

Expand All @@ -30,7 +31,7 @@ func (c *Client) createGRPCClient() (err error) {

var errInvalidEndpoint = errors.New("invalid endpoint options")

func (c *Client) openGRPCConn() error {
func (c *Client) openGRPCConn(ctx context.Context) error {
if c.conn != nil {
return nil
}
Expand All @@ -47,12 +48,21 @@ func (c *Client) openGRPCConn() error {
creds = insecure.NewCredentials()
}

dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout)
dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
var err error
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, grpcstd.WithTransportCredentials(creds))

c.conn, err = grpcstd.DialContext(dialCtx, c.addr,
grpcstd.WithTransportCredentials(creds),
grpcstd.WithBlock(),
)

cancel()

return err
if err != nil {
return fmt.Errorf("gRPC dial: %w", err)
}

return nil
}

// ParseURI parses s as address and returns a host and a flag
Expand Down
4 changes: 2 additions & 2 deletions rpc/client/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func (g rwGRPC) WriteMessage(m message.Message) error {
}

func (c *Client) initGRPC(info common.CallMethodInfo, prm *callParameters) (MessageReadWriter, error) {
if err := c.createGRPCClient(); err != nil {
if err := c.createGRPCClient(prm.ctx); err != nil {
return nil, err
}

rw, err := c.gRPCClient.Init(info, prm.callOpts...)
rw, err := c.gRPCClient.Init(info, grpc.WithContext(prm.ctx))
if err != nil {
return nil, err
}
Expand Down