github.com/uber-go/tally/v4@v4.1.17/m3/customtransports/m3_calc_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  // TCalcTransport is a thrift TTransport that is used to calculate how many
    24  // bytes are used when writing a thrift element.
    25  type TCalcTransport struct {
    26  	count int32
    27  }
    28  
    29  // GetCount returns the number of bytes that would be written
    30  // Required to maintain thrift.TTransport interface
    31  func (p *TCalcTransport) GetCount() int32 {
    32  	return p.count
    33  }
    34  
    35  // ResetCount resets the number of bytes written to 0
    36  func (p *TCalcTransport) ResetCount() {
    37  	p.count = 0
    38  }
    39  
    40  // Write adds the number of bytes written to the count
    41  // Required to maintain thrift.TTransport interface
    42  func (p *TCalcTransport) Write(buf []byte) (int, error) {
    43  	p.count += int32(len(buf))
    44  	return len(buf), nil
    45  }
    46  
    47  // WriteByte adds 1 to the count
    48  // Required to maintain thrift.TRichTransport interface
    49  func (p *TCalcTransport) WriteByte(byte) error {
    50  	p.count++
    51  	return nil
    52  }
    53  
    54  // WriteString adds the length of the string to the count
    55  // Required to maintain thrift.TRichTransport interface
    56  func (p *TCalcTransport) WriteString(s string) (int, error) {
    57  	p.count += int32(len(s))
    58  	return len(s), nil
    59  }
    60  
    61  // IsOpen does nothing as transport is not maintaining a connection
    62  // Required to maintain thrift.TTransport interface
    63  func (p *TCalcTransport) IsOpen() bool {
    64  	return true
    65  }
    66  
    67  // Open does nothing as transport is not maintaining a connection
    68  // Required to maintain thrift.TTransport interface
    69  func (p *TCalcTransport) Open() error {
    70  	return nil
    71  }
    72  
    73  // Close does nothing as transport is not maintaining a connection
    74  // Required to maintain thrift.TTransport interface
    75  func (p *TCalcTransport) Close() error {
    76  	return nil
    77  }
    78  
    79  // Read does nothing as it's not required for calculations
    80  // Required to maintain thrift.TTransport interface
    81  func (p *TCalcTransport) Read(buf []byte) (int, error) {
    82  	return 0, nil
    83  }
    84  
    85  // ReadByte does nothing as it's not required for calculations
    86  // Required to maintain thrift.TRichTransport interface
    87  func (p *TCalcTransport) ReadByte() (byte, error) {
    88  	return 0, nil
    89  }
    90  
    91  // RemainingBytes returns the max number of bytes (same as Thrift's StreamTransport) as we
    92  // do not know how many bytes we have left.
    93  func (p *TCalcTransport) RemainingBytes() uint64 {
    94  	const maxSize = ^uint64(0)
    95  	return maxSize
    96  }
    97  
    98  // Flush does nothing as it's not required for calculations
    99  // Required to maintain thrift.TTransport interface
   100  func (p *TCalcTransport) Flush() error {
   101  	return nil
   102  }