go.uber.org/yarpc@v1.72.1/transport/grpc/response_writer.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 grpc
    22  
    23  import (
    24  	"bytes"
    25  
    26  	"go.uber.org/multierr"
    27  	"go.uber.org/yarpc/api/transport"
    28  	"google.golang.org/grpc/metadata"
    29  )
    30  
    31  var (
    32  	_ transport.ResponseWriter             = (*responseWriter)(nil)
    33  	_ transport.ApplicationErrorMetaSetter = (*responseWriter)(nil)
    34  )
    35  
    36  type responseWriter struct {
    37  	buffer    *bytes.Buffer
    38  	md        metadata.MD
    39  	headerErr error
    40  }
    41  
    42  func newResponseWriter() *responseWriter {
    43  	return &responseWriter{}
    44  }
    45  
    46  func (r *responseWriter) Write(p []byte) (int, error) {
    47  	if r.buffer == nil {
    48  		// Response writer bytes must not be pooled since calls to SendMsg hold on
    49  		// to the bytes after the the function returns.
    50  		//
    51  		// See https://github.com/yarpc/yarpc-go/pull/1738 for details.
    52  		r.buffer = bytes.NewBuffer(make([]byte, 0, len(p)))
    53  	}
    54  	return r.buffer.Write(p)
    55  }
    56  
    57  func (r *responseWriter) AddHeaders(headers transport.Headers) {
    58  	if r.md == nil {
    59  		r.md = metadata.New(nil)
    60  	}
    61  	r.headerErr = multierr.Combine(r.headerErr, addApplicationHeaders(r.md, headers))
    62  }
    63  
    64  func (r *responseWriter) SetApplicationError() {
    65  	r.AddSystemHeader(ApplicationErrorHeader, ApplicationErrorHeaderValue)
    66  }
    67  func (r *responseWriter) SetApplicationErrorMeta(meta *transport.ApplicationErrorMeta) {
    68  	if meta == nil {
    69  		return
    70  	}
    71  
    72  	if meta.Name != "" {
    73  		r.AddSystemHeader(_applicationErrorNameHeader, meta.Name)
    74  	}
    75  	if meta.Details != "" {
    76  		r.AddSystemHeader(_applicationErrorDetailsHeader, meta.Details)
    77  	}
    78  }
    79  
    80  func (r *responseWriter) AddSystemHeader(key string, value string) {
    81  	if r.md == nil {
    82  		r.md = metadata.New(nil)
    83  	}
    84  	r.headerErr = multierr.Combine(r.headerErr, addToMetadata(r.md, key, value))
    85  }
    86  
    87  func (r *responseWriter) Bytes() []byte {
    88  	if r.buffer == nil {
    89  		return nil
    90  	}
    91  	return r.buffer.Bytes()
    92  }
    93  
    94  func (r *responseWriter) Close() {
    95  	r.buffer = nil
    96  }