github.com/uber-go/tally/v4@v4.1.17/m3/customtransports/buffered_read_transport.go (about)

     1  // Copyright (c) 2021 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 customtransport
    22  
    23  import (
    24  	"bytes"
    25  
    26  	"github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift"
    27  )
    28  
    29  // TBufferedReadTransport is a thrift.TTransport that reads from a buffer
    30  type TBufferedReadTransport struct {
    31  	readBuf *bytes.Buffer
    32  }
    33  
    34  // NewTBufferedReadTransport creates a buffer backed TTransport
    35  func NewTBufferedReadTransport(readBuf *bytes.Buffer) (*TBufferedReadTransport, error) {
    36  	return &TBufferedReadTransport{readBuf: readBuf}, nil
    37  }
    38  
    39  // IsOpen does nothing as transport is not maintaining the connection
    40  // Required to maintain thrift.TTransport interface
    41  func (p *TBufferedReadTransport) IsOpen() bool {
    42  	return true
    43  }
    44  
    45  // Open does nothing as transport is not maintaining the connection
    46  // Required to maintain thrift.TTransport interface
    47  func (p *TBufferedReadTransport) Open() error {
    48  	return nil
    49  }
    50  
    51  // Close does nothing as transport is not maintaining the connection
    52  // Required to maintain thrift.TTransport interface
    53  func (p *TBufferedReadTransport) Close() error {
    54  	return nil
    55  }
    56  
    57  // Read reads bytes from the local buffer and puts them in the specified buf
    58  func (p *TBufferedReadTransport) Read(buf []byte) (int, error) {
    59  	in, err := p.readBuf.Read(buf)
    60  	return in, thrift.NewTTransportExceptionFromError(err)
    61  }
    62  
    63  // RemainingBytes returns the number of bytes left to be read from the readBuf
    64  func (p *TBufferedReadTransport) RemainingBytes() uint64 {
    65  	return uint64(p.readBuf.Len())
    66  }
    67  
    68  // Write writes bytes into the read buffer
    69  // Required to maintain thrift.TTransport interface
    70  func (p *TBufferedReadTransport) Write(buf []byte) (int, error) {
    71  	p.readBuf = bytes.NewBuffer(buf)
    72  	return len(buf), nil
    73  }
    74  
    75  // Flush does nothing as udp server does not write responses back
    76  // Required to maintain thrift.TTransport interface
    77  func (p *TBufferedReadTransport) Flush() error {
    78  	return nil
    79  }