Skip to content

Commit

Permalink
encoding/thrift: Plumb the outbounds calling logic into the generated…
Browse files Browse the repository at this point in the history
… yarpc client code

@smyth provided the initial logic for making oubounds thrift calls using the
thriftrw 'no-wire' (read: streaming) implementation.  Leverage this logic in
the generated yarpc client code.

This is done through generating a completely independent client and leveraging
that code path iff the new no-wire client was generated and if it was enabled.
  • Loading branch information
witriew authored and abhinav committed Aug 18, 2021
1 parent ad935dd commit 3a44234
Show file tree
Hide file tree
Showing 25 changed files with 1,007 additions and 550 deletions.
43 changes: 43 additions & 0 deletions encoding/thrift/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"

"go.uber.org/thriftrw/protocol"
"go.uber.org/thriftrw/protocol/stream"
"go.uber.org/thriftrw/wire"
)

Expand Down Expand Up @@ -55,3 +56,45 @@ func (ev disableEnvelopingProtocol) DecodeEnveloped(r io.ReaderAt) (wire.Envelop
Value: value,
}, err
}

type disableEnvelopingNoWireProtocol struct {
stream.Protocol

Type wire.EnvelopeType
}

func (evsp disableEnvelopingNoWireProtocol) Reader(r io.Reader) stream.Reader {
return disableEnvelopingReader{
Reader: evsp.Protocol.Reader(r),
Type: evsp.Type,
}
}

func (evsp disableEnvelopingNoWireProtocol) Writer(w io.Writer) stream.Writer {
return disableEnvelopingWriter{
Writer: evsp.Protocol.Writer(w),
}
}

type disableEnvelopingReader struct {
stream.Reader

Type wire.EnvelopeType
}

func (evr disableEnvelopingReader) ReadEnvelopeBegin() (stream.EnvelopeHeader, error) {
return stream.EnvelopeHeader{
Name: "",
Type: evr.Type,
SeqID: 1,
}, nil
}

func (evr disableEnvelopingReader) ReadEnvelopeEnd() error { return nil }

type disableEnvelopingWriter struct {
stream.Writer
}

func (evw disableEnvelopingWriter) WriteEnvelopeBegin(stream.EnvelopeHeader) error { return nil }
func (evw disableEnvelopingWriter) WriteEnvelopeEnd() error { return nil }
2 changes: 2 additions & 0 deletions encoding/thrift/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func ClientBuilderOptions(_ transport.ClientConfig, f reflect.StructField) []Cli
opts = append(opts, Multiplexed)
case "enveloped":
opts = append(opts, Enveloped)
case "nowire":
opts = append(opts, NoWire(true))
default:
// Ignore unknown options
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions encoding/thrift/multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"go.uber.org/thriftrw/protocol"
"go.uber.org/thriftrw/protocol/stream"
"go.uber.org/thriftrw/wire"
)

Expand All @@ -48,3 +49,46 @@ func (m multiplexedOutboundProtocol) DecodeEnveloped(r io.ReaderAt) (wire.Envelo
e.Name = strings.TrimPrefix(e.Name, m.Service+":")
return e, err
}

type multiplexedOutboundNoWireProtocol struct {
stream.Protocol

Service string
}

func (m multiplexedOutboundNoWireProtocol) Writer(w io.Writer) stream.Writer {
return multiplexedWriter{
Writer: m.Protocol.Writer(w),
Service: m.Service,
}
}

func (m multiplexedOutboundNoWireProtocol) Reader(r io.Reader) stream.Reader {
return multiplexedReader{
Reader: m.Protocol.Reader(r),
Service: m.Service,
}
}

type multiplexedWriter struct {
stream.Writer

Service string
}

func (w multiplexedWriter) WriteEnvelopeBegin(eh stream.EnvelopeHeader) error {
eh.Name = w.Service + ":" + eh.Name
return w.Writer.WriteEnvelopeBegin(eh)
}

type multiplexedReader struct {
stream.Reader

Service string
}

func (r multiplexedReader) ReadEnvelopeBegin() (stream.EnvelopeHeader, error) {
eh, err := r.Reader.ReadEnvelopeBegin()
eh.Name = strings.TrimPrefix(eh.Name, r.Service+":")
return eh, err
}
Loading

0 comments on commit 3a44234

Please sign in to comment.