go.uber.org/yarpc@v1.72.1/encoding/thrift/inbound_nowire.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package thrift 22 23 import ( 24 "context" 25 "io" 26 27 "go.uber.org/multierr" 28 "go.uber.org/thriftrw/protocol/stream" 29 "go.uber.org/thriftrw/wire" 30 encodingapi "go.uber.org/yarpc/api/encoding" 31 "go.uber.org/yarpc/api/transport" 32 "go.uber.org/yarpc/pkg/errors" 33 ) 34 35 var _emptyResponse = NoWireResponse{} 36 37 // NoWireCall contains all of the required objects needed for an underlying 38 // Handle needs to unpack any given request. 39 type NoWireCall struct { 40 Reader io.Reader 41 RequestReader stream.RequestReader 42 EnvelopeType wire.EnvelopeType 43 } 44 45 // NoWireHandler is implemented by the generated code for each method that the 46 // server needs to implement. It is responsible for unpacking the request, 47 // executing it, and returning a NoWireResponse that contains information about 48 // how to construct a response as well as any relevant metadata while executing 49 // the request. 50 type NoWireHandler interface { 51 HandleNoWire(context.Context, *NoWireCall) (NoWireResponse, error) 52 } 53 54 // thriftNoWireHandler is similar to thriftUnaryHandler and thriftOnewayHandler 55 // except that thriftNoWireHandler implements both transport.UnaryHandler and 56 // transport.OnewayHandler through a single type, utilizing the "nowire" 57 // (streaming in the ThriftRW context, bypassing the intermediate "Wire" 58 // representation) implementation. 59 // 60 // The UnaryHandler and OnewayHandler implementations then call into type that 61 // implements NoWireHandler, which understands how to unpack and invoke the 62 // request, given the ThriftRW primitives for reading the raw representation. 63 type thriftNoWireHandler struct { 64 Handler NoWireHandler 65 RequestReader stream.RequestReader 66 } 67 68 var ( 69 _ transport.OnewayHandler = (*thriftNoWireHandler)(nil) 70 _ transport.UnaryHandler = (*thriftNoWireHandler)(nil) 71 ) 72 73 func (t thriftNoWireHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) (err error) { 74 ctx, call := encodingapi.NewInboundCall(ctx) 75 defer func() { 76 err = multierr.Append(err, closeReader(treq.Body)) 77 }() 78 79 res, err := t.decodeAndHandle(ctx, call, treq, rw, wire.Call) 80 if err != nil { 81 return err 82 } 83 84 if resType := res.Body.EnvelopeType(); resType != wire.Reply { 85 return errors.ResponseBodyEncodeError( 86 treq, errUnexpectedEnvelopeType(resType)) 87 } 88 89 if res.IsApplicationError { 90 rw.SetApplicationError() 91 if applicationErrorMetaSetter, ok := rw.(transport.ApplicationErrorMetaSetter); ok { 92 applicationErrorMetaSetter.SetApplicationErrorMeta(&transport.ApplicationErrorMeta{ 93 Details: res.ApplicationErrorDetails, 94 Name: res.ApplicationErrorName, 95 Code: res.ApplicationErrorCode, 96 }) 97 } 98 } 99 100 if err := call.WriteToResponse(rw); err != nil { 101 // not reachable 102 return err 103 } 104 105 if err := res.ResponseWriter.WriteResponse(wire.Reply, rw, res.Body); err != nil { 106 return errors.ResponseBodyEncodeError(treq, err) 107 } 108 109 return nil 110 } 111 112 func (t thriftNoWireHandler) HandleOneway(ctx context.Context, treq *transport.Request) (err error) { 113 ctx, call := encodingapi.NewInboundCall(ctx) 114 defer func() { 115 err = multierr.Append(err, closeReader(treq.Body)) 116 }() 117 118 _, err = t.decodeAndHandle(ctx, call, treq, nil, wire.OneWay) 119 return err 120 } 121 122 // decodeAndHandle is a shared utility between the implementations of 123 // transport.UnaryHandler and transport.OnewayHandler, to decode and execute 124 // the request regardless of enveloping, via the "nowire" implementation in 125 // ThriftRW. 126 func (t thriftNoWireHandler) decodeAndHandle( 127 ctx context.Context, 128 call *encodingapi.InboundCall, 129 treq *transport.Request, 130 rw transport.ResponseWriter, 131 reqEnvelopeType wire.EnvelopeType, 132 ) (NoWireResponse, error) { 133 if err := errors.ExpectEncodings(treq, Encoding); err != nil { 134 return _emptyResponse, err 135 } 136 137 if err := call.ReadFromRequest(treq); err != nil { 138 return _emptyResponse, err 139 } 140 141 nwc := NoWireCall{ 142 Reader: treq.Body, 143 EnvelopeType: reqEnvelopeType, 144 RequestReader: t.RequestReader, 145 } 146 147 return t.Handler.HandleNoWire(ctx, &nwc) 148 }