github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/rpc/client/init.go (about) 1 package client 2 3 import ( 4 "context" 5 "io" 6 7 "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/common" 8 "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message" 9 "google.golang.org/grpc" 10 ) 11 12 // MessageReader is an interface of the Message reader. 13 type MessageReader interface { 14 // ReadMessage reads the next Message. 15 // 16 // Returns io.EOF if there are no more messages to read. 17 // ReadMessage should not be called after io.EOF occasion. 18 ReadMessage(message.Message) error 19 } 20 21 // MessageWriter is an interface of the Message writer. 22 type MessageWriter interface { 23 // WriteMessage writers the next Message. 24 // 25 // WriteMessage should not be called after any error. 26 WriteMessage(message.Message) error 27 } 28 29 // MessageReadWriter is a component interface 30 // for transmitting raw Protobuf messages. 31 type MessageReadWriter interface { 32 MessageReader 33 MessageWriter 34 35 // Closes the communication session. 36 // 37 // All calls to send/receive messages must be done before closing. 38 io.Closer 39 } 40 41 // Init initiates a messaging session and returns the interface for message transmitting. 42 func (c *Client) Init(info common.CallMethodInfo, opts ...CallOption) (MessageReadWriter, error) { 43 prm := defaultCallParameters() 44 45 for _, opt := range opts { 46 opt(prm) 47 } 48 49 if err := c.openGRPCConn(prm.ctx); err != nil { 50 return nil, err 51 } 52 53 ctx, cancel := context.WithCancel(prm.ctx) 54 stream, err := c.conn.NewStream(ctx, &grpc.StreamDesc{ 55 StreamName: info.Name, 56 ServerStreams: info.ServerStream(), 57 ClientStreams: info.ClientStream(), 58 }, toMethodName(info)) 59 if err != nil { 60 cancel() 61 return nil, err 62 } 63 64 return &streamWrapper{ 65 ClientStream: stream, 66 cancel: cancel, 67 timeout: c.rwTimeout, 68 }, nil 69 }