forked from TrueCloudLab/frostfs-api-go
30c6ca0714
Implement generic `Client` that can communicate with the remote server via protobuf `Message`'s. The client can uniformly execute any protobuf RPC on the remote server using any of the supported transport protocols. Currently only gRPC protocol is supported. Additionally implement helpful functions to transmit messages by one of the flow types: unary, client- or server-side stream. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
41 lines
731 B
Go
41 lines
731 B
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/grpc"
|
|
grpcstd "google.golang.org/grpc"
|
|
)
|
|
|
|
func (c *Client) createGRPCClient() (err error) {
|
|
c.gRPCClientOnce.Do(func() {
|
|
if err = c.openGRPCConn(); err != nil {
|
|
return
|
|
}
|
|
|
|
c.gRPCClient = grpc.New(grpc.WithClientConnection(c.conn))
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
var errInvalidEndpoint = errors.New("invalid endpoint options")
|
|
|
|
func (c *Client) openGRPCConn() error {
|
|
if c.conn != nil {
|
|
return nil
|
|
}
|
|
|
|
if c.addr == "" {
|
|
return errInvalidEndpoint
|
|
}
|
|
|
|
var err error
|
|
|
|
dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout)
|
|
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, grpcstd.WithInsecure())
|
|
cancel()
|
|
|
|
return err
|
|
}
|