go.uber.org/yarpc@v1.72.1/encoding/protobuf/inbound.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 protobuf
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/gogo/protobuf/proto"
    27  	apiencoding "go.uber.org/yarpc/api/encoding"
    28  	"go.uber.org/yarpc/api/transport"
    29  	"go.uber.org/yarpc/pkg/errors"
    30  )
    31  
    32  type unaryHandler struct {
    33  	handle     func(context.Context, proto.Message) (proto.Message, error)
    34  	newRequest func() proto.Message
    35  	codec      *codec
    36  }
    37  
    38  func newUnaryHandler(
    39  	handle func(context.Context, proto.Message) (proto.Message, error),
    40  	newRequest func() proto.Message,
    41  	codec *codec,
    42  ) *unaryHandler {
    43  	return &unaryHandler{
    44  		handle:     handle,
    45  		newRequest: newRequest,
    46  		codec:      codec,
    47  	}
    48  }
    49  
    50  func (u *unaryHandler) Handle(ctx context.Context, transportRequest *transport.Request, responseWriter transport.ResponseWriter) error {
    51  	ctx, call, request, err := getProtoRequest(ctx, transportRequest, u.newRequest, u.codec)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	response, appErr := u.handle(ctx, request)
    57  
    58  	if err := call.WriteToResponse(responseWriter); err != nil {
    59  		return err
    60  	}
    61  	var responseData []byte
    62  	var responseCleanup func()
    63  	if response != nil {
    64  		responseData, responseCleanup, err = marshal(transportRequest.Encoding, response, u.codec)
    65  		if responseCleanup != nil {
    66  			defer responseCleanup()
    67  		}
    68  		if err != nil {
    69  			return errors.ResponseBodyEncodeError(transportRequest, err)
    70  		}
    71  	}
    72  	_, err = responseWriter.Write(responseData)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	if appErr != nil {
    77  		responseWriter.SetApplicationError()
    78  	}
    79  	return convertToYARPCError(transportRequest.Encoding, appErr, u.codec, responseWriter)
    80  }
    81  
    82  type onewayHandler struct {
    83  	handleOneway func(context.Context, proto.Message) error
    84  	newRequest   func() proto.Message
    85  	codec        *codec
    86  }
    87  
    88  func newOnewayHandler(
    89  	handleOneway func(context.Context, proto.Message) error,
    90  	newRequest func() proto.Message,
    91  	codec *codec,
    92  ) *onewayHandler {
    93  	return &onewayHandler{
    94  		handleOneway: handleOneway,
    95  		newRequest:   newRequest,
    96  		codec:        codec,
    97  	}
    98  }
    99  
   100  func (o *onewayHandler) HandleOneway(ctx context.Context, transportRequest *transport.Request) error {
   101  	ctx, _, request, err := getProtoRequest(ctx, transportRequest, o.newRequest, o.codec)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	return convertToYARPCError(transportRequest.Encoding, o.handleOneway(ctx, request), o.codec, nil /*responseWriter*/)
   106  }
   107  
   108  type streamHandler struct {
   109  	handle func(*ServerStream) error
   110  	codec  *codec
   111  }
   112  
   113  func newStreamHandler(handle func(*ServerStream) error) *streamHandler {
   114  	return &streamHandler{handle, newCodec(nil /*AnyResolver*/)}
   115  }
   116  
   117  func (s *streamHandler) HandleStream(stream *transport.ServerStream) error {
   118  	ctx, call := apiencoding.NewInboundCallWithOptions(stream.Context(), apiencoding.DisableResponseHeaders())
   119  	transportRequest := stream.Request()
   120  	if err := call.ReadFromRequestMeta(transportRequest.Meta); err != nil {
   121  		return err
   122  	}
   123  	protoStream := &ServerStream{
   124  		ctx:    ctx,
   125  		stream: stream,
   126  		codec:  s.codec,
   127  	}
   128  	return convertToYARPCError(transportRequest.Meta.Encoding, s.handle(protoStream), s.codec, nil /*responseWriter*/)
   129  }
   130  
   131  func getProtoRequest(ctx context.Context, transportRequest *transport.Request, newRequest func() proto.Message, codec *codec) (context.Context, *apiencoding.InboundCall, proto.Message, error) {
   132  	if err := errors.ExpectEncodings(transportRequest, Encoding, JSONEncoding); err != nil {
   133  		return nil, nil, nil, err
   134  	}
   135  	ctx, call := apiencoding.NewInboundCall(ctx)
   136  	if err := call.ReadFromRequest(transportRequest); err != nil {
   137  		return nil, nil, nil, err
   138  	}
   139  	request := newRequest()
   140  	if err := unmarshal(transportRequest.Encoding, transportRequest.Body, request, codec); err != nil {
   141  		return nil, nil, nil, errors.RequestBodyDecodeError(transportRequest, err)
   142  	}
   143  	return ctx, call, request, nil
   144  }