go.uber.org/yarpc@v1.72.1/encoding/raw/outbound.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 raw
    22  
    23  import (
    24  	"bytes"
    25  	"context"
    26  	"io/ioutil"
    27  
    28  	"go.uber.org/yarpc"
    29  	encodingapi "go.uber.org/yarpc/api/encoding"
    30  	"go.uber.org/yarpc/api/transport"
    31  	"go.uber.org/yarpc/pkg/encoding"
    32  )
    33  
    34  // Client makes Raw requests to a single service.
    35  type Client interface {
    36  	// Call performs a unary outbound Raw request.
    37  	Call(ctx context.Context, procedure string, body []byte, opts ...yarpc.CallOption) ([]byte, error)
    38  
    39  	// CallOneway performs a oneway outbound Raw request.
    40  	CallOneway(ctx context.Context, procedure string, body []byte, opts ...yarpc.CallOption) (transport.Ack, error)
    41  }
    42  
    43  // New builds a new Raw client.
    44  func New(c transport.ClientConfig) Client {
    45  	return rawClient{cc: c}
    46  }
    47  
    48  func init() {
    49  	yarpc.RegisterClientBuilder(New)
    50  }
    51  
    52  type rawClient struct {
    53  	cc transport.ClientConfig
    54  }
    55  
    56  func (c rawClient) Call(ctx context.Context, procedure string, body []byte, opts ...yarpc.CallOption) ([]byte, error) {
    57  	call := encodingapi.NewOutboundCall(encoding.FromOptions(opts)...)
    58  	treq := transport.Request{
    59  		Caller:    c.cc.Caller(),
    60  		Service:   c.cc.Service(),
    61  		Procedure: procedure,
    62  		Encoding:  Encoding,
    63  		Body:      bytes.NewReader(body),
    64  	}
    65  
    66  	ctx, err := call.WriteToRequest(ctx, &treq)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	tres, appErr := c.cc.GetUnaryOutbound().Call(ctx, &treq)
    72  	if tres == nil {
    73  		return nil, appErr
    74  	}
    75  	if tres.Body != nil {
    76  		defer tres.Body.Close()
    77  	}
    78  
    79  	if _, err = call.ReadFromResponse(ctx, tres); err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	// we want to return the appErr if it exists as this is what
    84  	// the previous behavior was so we deprioritize this error
    85  	var resBody []byte
    86  	var readErr error
    87  	if tres.Body != nil {
    88  		resBody, readErr = ioutil.ReadAll(tres.Body)
    89  	}
    90  	if appErr != nil {
    91  		return resBody, appErr
    92  	}
    93  	return resBody, readErr
    94  }
    95  
    96  func (c rawClient) CallOneway(ctx context.Context, procedure string, body []byte, opts ...yarpc.CallOption) (transport.Ack, error) {
    97  	call := encodingapi.NewOutboundCall(encoding.FromOptions(opts)...)
    98  	treq := transport.Request{
    99  		Caller:    c.cc.Caller(),
   100  		Service:   c.cc.Service(),
   101  		Procedure: procedure,
   102  		Encoding:  Encoding,
   103  		Body:      bytes.NewReader(body),
   104  	}
   105  
   106  	ctx, err := call.WriteToRequest(ctx, &treq)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	return c.cc.GetOnewayOutbound().CallOneway(ctx, &treq)
   112  }