[#2] rpc/client: Remove additional wrapper

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-02-03 15:22:00 +03:00 committed by fyrchik
parent 513e3e137d
commit cc8da15242
12 changed files with 82 additions and 268 deletions

View file

@ -1,29 +1,19 @@
package client
import (
"sync"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
)
// Client represents client for exchanging messages
// with a remote server using Protobuf RPC.
type Client struct {
*cfg
gRPCClientOnce sync.Once
gRPCClient *grpc.Client
cfg
}
// New creates, configures via options and returns new Client instance.
func New(opts ...Option) *Client {
c := defaultCfg()
var c Client
c.initDefault()
for _, opt := range opts {
opt(c)
opt(&c.cfg)
}
return &Client{
cfg: c,
}
return &c
}

View file

@ -9,14 +9,8 @@ import (
// Returns non-nil result after the first Init() call
// completed without a connection error.
//
// Conn is NPE-safe: returns nil if Client is nil.
//
// Client should not be used after Close() call
// on the connection: behavior is undefined.
func (c *Client) Conn() io.Closer {
if c != nil {
return c.gRPCClient.Conn()
}
return nil
return c.conn
}

View file

@ -7,28 +7,11 @@ import (
"net"
"net/url"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
grpcstd "google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
func (c *Client) createGRPCClient(ctx context.Context) (err error) {
c.gRPCClientOnce.Do(func() {
if err = c.openGRPCConn(ctx); err != nil {
err = fmt.Errorf("open gRPC connection: %w", err)
return
}
c.gRPCClient = grpc.New(
grpc.WithClientConnection(c.conn),
grpc.WithRWTimeout(c.rwTimeout),
)
})
return
}
var errInvalidEndpoint = errors.New("invalid endpoint options")
func (c *Client) openGRPCConn(ctx context.Context) error {

View file

@ -1,11 +1,12 @@
package client
import (
"context"
"io"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/common"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"google.golang.org/grpc"
)
// MessageReader is an interface of the Message reader.
@ -45,39 +46,24 @@ func (c *Client) Init(info common.CallMethodInfo, opts ...CallOption) (MessageRe
opt(prm)
}
return c.initGRPC(info, prm)
}
type rwGRPC struct {
grpc.MessageReadWriter
}
func (g rwGRPC) ReadMessage(m message.Message) error {
// Can be optimized: we can create blank message here.
gm := m.ToGRPCMessage()
if err := g.MessageReadWriter.ReadMessage(gm); err != nil {
return err
}
return m.FromGRPCMessage(gm)
}
func (g rwGRPC) WriteMessage(m message.Message) error {
return g.MessageReadWriter.WriteMessage(m.ToGRPCMessage())
}
func (c *Client) initGRPC(info common.CallMethodInfo, prm *callParameters) (MessageReadWriter, error) {
if err := c.createGRPCClient(prm.ctx); err != nil {
if err := c.openGRPCConn(prm.ctx); err != nil {
return nil, err
}
rw, err := c.gRPCClient.Init(info, grpc.WithContext(prm.ctx))
ctx, cancel := context.WithCancel(prm.ctx)
stream, err := c.conn.NewStream(ctx, &grpc.StreamDesc{
StreamName: info.Name,
ServerStreams: info.ServerStream(),
ClientStreams: info.ClientStream(),
}, toMethodName(info))
if err != nil {
cancel()
return nil, err
}
return &rwGRPC{
MessageReadWriter: rw,
return &streamWrapper{
ClientStream: stream,
cancel: cancel,
timeout: c.rwTimeout,
}, nil
}

View file

@ -31,11 +31,9 @@ const (
defaultRWTimeout = 1 * time.Minute
)
func defaultCfg() *cfg {
return &cfg{
dialTimeout: defaultDialTimeout,
rwTimeout: defaultRWTimeout,
}
func (c *cfg) initDefault() {
c.dialTimeout = defaultDialTimeout
c.rwTimeout = defaultRWTimeout
}
// WithNetworkAddress returns option to specify

View file

@ -0,0 +1,58 @@
package client
import (
"context"
"time"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"google.golang.org/grpc"
)
type streamWrapper struct {
grpc.ClientStream
timeout time.Duration
cancel context.CancelFunc
}
func (w streamWrapper) ReadMessage(m message.Message) error {
// Can be optimized: we can create blank message here.
gm := m.ToGRPCMessage()
err := w.withTimeout(func() error {
return w.ClientStream.RecvMsg(gm)
})
if err != nil {
return err
}
return m.FromGRPCMessage(gm)
}
func (w streamWrapper) WriteMessage(m message.Message) error {
return w.withTimeout(func() error {
return w.ClientStream.SendMsg(m.ToGRPCMessage())
})
}
func (w *streamWrapper) Close() error {
return w.withTimeout(w.ClientStream.CloseSend)
}
func (w *streamWrapper) withTimeout(closure func() error) error {
ch := make(chan error, 1)
go func() {
ch <- closure()
close(ch)
}()
tt := time.NewTimer(w.timeout)
select {
case err := <-ch:
tt.Stop()
return err
case <-tt.C:
w.cancel()
return context.DeadlineExceeded
}
}

13
rpc/client/util.go Normal file
View file

@ -0,0 +1,13 @@
package client
import (
"fmt"
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/common"
)
const methodNameFmt = "/%s/%s"
func toMethodName(p common.CallMethodInfo) string {
return fmt.Sprintf(methodNameFmt, p.Service, p.Name)
}