2021-03-12 11:17:16 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2021-12-24 10:16:55 +00:00
|
|
|
"context"
|
2021-03-12 11:17:16 +00:00
|
|
|
"io"
|
2021-12-24 10:16:55 +00:00
|
|
|
"time"
|
2021-03-12 11:17:16 +00:00
|
|
|
|
2021-11-16 17:30:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/common"
|
2021-03-12 11:17:16 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Message represents raw gRPC message.
|
|
|
|
type Message interface{}
|
|
|
|
|
|
|
|
// MessageReadWriter is a component interface
|
|
|
|
// for transmitting raw messages over gRPC protocol.
|
|
|
|
type MessageReadWriter interface {
|
|
|
|
// ReadMessage reads the next message from the remote server,
|
|
|
|
// and writes it to the argument.
|
|
|
|
ReadMessage(Message) error
|
|
|
|
|
|
|
|
// WriteMessage sends message from argument to remote server.
|
|
|
|
WriteMessage(Message) error
|
|
|
|
|
|
|
|
// Closes the communication session with the remote server.
|
|
|
|
//
|
|
|
|
// All calls to send/receive messages must be done before closing.
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
type streamWrapper struct {
|
|
|
|
grpc.ClientStream
|
2021-12-24 10:16:55 +00:00
|
|
|
timeout time.Duration
|
|
|
|
cancel context.CancelFunc
|
2021-03-12 11:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w streamWrapper) ReadMessage(m Message) error {
|
2021-12-24 10:16:55 +00:00
|
|
|
return w.withTimeout(func() error {
|
|
|
|
return w.ClientStream.RecvMsg(m)
|
|
|
|
})
|
2021-03-12 11:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w streamWrapper) WriteMessage(m Message) error {
|
2021-12-24 10:16:55 +00:00
|
|
|
return w.withTimeout(func() error {
|
|
|
|
return w.ClientStream.SendMsg(m)
|
|
|
|
})
|
2021-03-12 11:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamWrapper) Close() error {
|
2021-12-24 10:16:55 +00:00
|
|
|
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)
|
|
|
|
}()
|
|
|
|
|
2022-04-11 10:47:29 +00:00
|
|
|
tt := time.NewTimer(w.timeout)
|
|
|
|
|
2021-12-24 10:16:55 +00:00
|
|
|
select {
|
|
|
|
case err := <-ch:
|
2022-04-11 10:47:29 +00:00
|
|
|
tt.Stop()
|
2021-12-24 10:16:55 +00:00
|
|
|
return err
|
2022-04-11 10:47:29 +00:00
|
|
|
case <-tt.C:
|
2021-12-24 10:16:55 +00:00
|
|
|
w.cancel()
|
|
|
|
return context.DeadlineExceeded
|
|
|
|
}
|
2021-03-12 11:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init initiates a messaging session within the RPC configured by options.
|
|
|
|
func (c *Client) Init(info common.CallMethodInfo, opts ...CallOption) (MessageReadWriter, error) {
|
|
|
|
prm := defaultCallParameters()
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(prm)
|
|
|
|
}
|
|
|
|
|
2021-12-24 10:16:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(prm.ctx)
|
|
|
|
stream, err := c.con.NewStream(ctx, &grpc.StreamDesc{
|
2021-03-12 11:17:16 +00:00
|
|
|
StreamName: info.Name,
|
|
|
|
ServerStreams: info.ServerStream(),
|
|
|
|
ClientStreams: info.ClientStream(),
|
|
|
|
}, toMethodName(info))
|
|
|
|
if err != nil {
|
2021-12-24 10:16:55 +00:00
|
|
|
cancel()
|
2021-03-12 11:17:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &streamWrapper{
|
|
|
|
ClientStream: stream,
|
2021-12-24 10:16:55 +00:00
|
|
|
cancel: cancel,
|
|
|
|
timeout: c.rwTimeout,
|
2021-03-12 11:17:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|