multiplexing cruft

This commit is contained in:
Asim Aslam
2019-08-15 20:08:49 +01:00
parent f6b8045dd5
commit ef04331b86
6 changed files with 209 additions and 66 deletions

View File

@@ -108,7 +108,7 @@ func (r *rpcClient) call(ctx context.Context, node *registry.Node, req Request,
seq := atomic.LoadUint64(&r.seq)
atomic.AddUint64(&r.seq, 1)
codec := newRpcCodec(msg, c, cf)
codec := newRpcCodec(msg, c, cf, "")
rsp := &rpcResponse{
socket: c,
@@ -206,7 +206,13 @@ func (r *rpcClient) stream(ctx context.Context, node *registry.Node, req Request
return nil, errors.InternalServerError("go.micro.client", "connection error: %v", err)
}
codec := newRpcCodec(msg, c, cf)
// increment the sequence number
seq := atomic.LoadUint64(&r.seq)
atomic.AddUint64(&r.seq, 1)
id := fmt.Sprintf("%v", seq)
// create codec with stream id
codec := newRpcCodec(msg, c, cf, id)
rsp := &rpcResponse{
socket: c,
@@ -224,6 +230,9 @@ func (r *rpcClient) stream(ctx context.Context, node *registry.Node, req Request
response: rsp,
closed: make(chan bool),
codec: codec,
id: id,
// signal the end of stream,
eos: true,
}
ch := make(chan error, 1)

View File

@@ -39,6 +39,9 @@ type rpcCodec struct {
req *transport.Message
buf *readWriteCloser
// signify if its a stream
stream string
}
type readWriteCloser struct {
@@ -113,7 +116,7 @@ func getHeaders(m *codec.Message) {
}
}
func setHeaders(m *codec.Message) {
func setHeaders(m *codec.Message, stream string) {
set := func(hdr, v string) {
if len(v) == 0 {
return
@@ -127,6 +130,10 @@ func setHeaders(m *codec.Message) {
set("Micro-Method", m.Method)
set("Micro-Endpoint", m.Endpoint)
set("Micro-Error", m.Error)
if len(stream) > 0 {
set("Micro-Stream", stream)
}
}
// setupProtocol sets up the old protocol
@@ -150,7 +157,7 @@ func setupProtocol(msg *transport.Message, node *registry.Node) codec.NewCodec {
return defaultCodecs[msg.Header["Content-Type"]]
}
func newRpcCodec(req *transport.Message, client transport.Client, c codec.NewCodec) codec.Codec {
func newRpcCodec(req *transport.Message, client transport.Client, c codec.NewCodec, stream string) codec.Codec {
rwc := &readWriteCloser{
wbuf: bytes.NewBuffer(nil),
rbuf: bytes.NewBuffer(nil),
@@ -160,6 +167,7 @@ func newRpcCodec(req *transport.Message, client transport.Client, c codec.NewCod
client: client,
codec: c(rwc),
req: req,
stream: stream,
}
return r
}
@@ -178,7 +186,7 @@ func (c *rpcCodec) Write(m *codec.Message, body interface{}) error {
}
// set the mucp headers
setHeaders(m)
setHeaders(m, c.stream)
// if body is bytes Frame don't encode
if body != nil {

View File

@@ -18,6 +18,9 @@ type rpcStream struct {
response Response
codec codec.Codec
context context.Context
// signal whether we should send EOS
eos bool
}
func (r *rpcStream) isClosed() bool {
@@ -120,6 +123,20 @@ func (r *rpcStream) Close() error {
return nil
default:
close(r.closed)
// send the end of stream message
if r.eos {
// no need to check for error
r.codec.Write(&codec.Message{
Id: r.id,
Target: r.request.Service(),
Method: r.request.Method(),
Endpoint: r.request.Endpoint(),
Type: codec.Error,
Error: lastStreamResponseError,
}, nil)
}
return r.codec.Close()
}
}