2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2021-10-27 00:57:12 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
2024-04-23 00:24:46 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2019-06-03 20:44:43 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Implements the streamer interface
|
|
|
|
type grpcStream struct {
|
2020-08-09 17:43:41 +03:00
|
|
|
grpc.ClientStream
|
2021-04-26 19:17:29 +03:00
|
|
|
context context.Context
|
2019-06-11 11:52:35 +03:00
|
|
|
err error
|
|
|
|
request client.Request
|
2019-06-08 21:40:44 +03:00
|
|
|
response client.Response
|
2020-09-17 16:08:21 +03:00
|
|
|
close func(err error)
|
2024-02-14 19:14:56 +03:00
|
|
|
conn *PoolConn
|
2021-04-26 19:17:29 +03:00
|
|
|
sync.RWMutex
|
|
|
|
closed bool
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcStream) Context() context.Context {
|
|
|
|
return g.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcStream) Request() client.Request {
|
|
|
|
return g.request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcStream) Response() client.Response {
|
2019-06-08 21:40:44 +03:00
|
|
|
return g.response
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcStream) Send(msg interface{}) error {
|
2020-08-09 17:43:41 +03:00
|
|
|
if err := g.ClientStream.SendMsg(msg); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
g.setError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:40:58 +03:00
|
|
|
func (g *grpcStream) SendMsg(msg interface{}) error {
|
|
|
|
if err := g.ClientStream.SendMsg(msg); err != nil {
|
|
|
|
g.setError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
func (g *grpcStream) Recv(msg interface{}) (err error) {
|
|
|
|
defer g.setError(err)
|
2020-08-09 17:43:41 +03:00
|
|
|
|
|
|
|
if err = g.ClientStream.RecvMsg(msg); err != nil {
|
2019-11-25 22:02:24 +03:00
|
|
|
// #202 - inconsistent gRPC stream behavior
|
|
|
|
// the only way to tell if the stream is done is when we get a EOF on the Recv
|
|
|
|
// here we should close the underlying gRPC ClientConn
|
2020-01-09 20:00:14 +03:00
|
|
|
closeErr := g.Close()
|
2019-11-25 22:02:24 +03:00
|
|
|
if err == io.EOF && closeErr != nil {
|
|
|
|
err = closeErr
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-08-09 17:43:41 +03:00
|
|
|
|
|
|
|
return err
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-08-09 17:43:41 +03:00
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:40:58 +03:00
|
|
|
func (g *grpcStream) RecvMsg(msg interface{}) (err error) {
|
|
|
|
defer g.setError(err)
|
|
|
|
|
|
|
|
if err = g.ClientStream.RecvMsg(msg); err != nil {
|
|
|
|
// #202 - inconsistent gRPC stream behavior
|
|
|
|
// the only way to tell if the stream is done is when we get a EOF on the Recv
|
|
|
|
// here we should close the underlying gRPC ClientConn
|
|
|
|
closeErr := g.Close()
|
|
|
|
if err == io.EOF && closeErr != nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
func (g *grpcStream) Error() error {
|
|
|
|
g.RLock()
|
|
|
|
defer g.RUnlock()
|
|
|
|
return g.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcStream) setError(e error) {
|
|
|
|
g.Lock()
|
|
|
|
g.err = e
|
|
|
|
g.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the gRPC send stream
|
|
|
|
// #202 - inconsistent gRPC stream behavior
|
|
|
|
// The underlying gRPC stream should not be closed here since the
|
|
|
|
// stream should still be able to receive after this function call
|
|
|
|
// TODO: should the conn be closed in another way?
|
|
|
|
func (g *grpcStream) Close() error {
|
2020-01-09 20:00:14 +03:00
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
if g.closed {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-17 16:08:21 +03:00
|
|
|
|
2024-04-23 00:24:46 +03:00
|
|
|
if sp, ok := tracer.SpanFromContext(g.context); ok && sp != nil {
|
|
|
|
if g.err != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, g.err.Error())
|
|
|
|
}
|
|
|
|
sp.Finish()
|
|
|
|
}
|
2020-09-17 16:08:21 +03:00
|
|
|
// close the connection
|
2020-01-09 20:00:14 +03:00
|
|
|
g.closed = true
|
2020-09-17 16:08:21 +03:00
|
|
|
g.close(g.err)
|
|
|
|
return g.ClientStream.CloseSend()
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2022-01-12 17:40:58 +03:00
|
|
|
|
|
|
|
func (g *grpcStream) CloseSend() error {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
if g.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the connection
|
|
|
|
g.closed = true
|
|
|
|
g.close(g.err)
|
|
|
|
return g.ClientStream.CloseSend()
|
|
|
|
}
|