github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/encoding/protobuf/unaggregated_options.go (about)

     1  // Copyright (c) 2018 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 "github.com/m3db/m3/src/x/pool"
    24  
    25  const (
    26  	defaultInitBufferSize = 2880
    27  
    28  	// The maximum supported unaggregated message size is 50MB by default.
    29  	// This is to protect the encoder and the decoder from incurring signicant
    30  	// memory overhead or even OOMing due to excessively large payload or
    31  	// network packet corruption.
    32  	defaultMaxUnaggregatedMessageSize = 50 * 1024 * 1024
    33  )
    34  
    35  // UnaggregatedOptions provide a set of options for the unaggregated encoder and iterator.
    36  type UnaggregatedOptions interface {
    37  	// SetBytesPool sets the bytes pool.
    38  	SetBytesPool(value pool.BytesPool) UnaggregatedOptions
    39  
    40  	// BytesPool returns the bytes pool.
    41  	BytesPool() pool.BytesPool
    42  
    43  	// SetInitBufferSize sets the initial buffer size.
    44  	SetInitBufferSize(value int) UnaggregatedOptions
    45  
    46  	// InitBufferSize returns the initial buffer size.
    47  	InitBufferSize() int
    48  
    49  	// SetMaxMessageSize sets the maximum message size.
    50  	SetMaxMessageSize(value int) UnaggregatedOptions
    51  
    52  	// MaxMessageSize returns the maximum message size.
    53  	MaxMessageSize() int
    54  }
    55  
    56  type unaggregatedOptions struct {
    57  	bytesPool      pool.BytesPool
    58  	initBufferSize int
    59  	maxMessageSize int
    60  }
    61  
    62  // NewUnaggregatedOptions create a new set of unaggregated options.
    63  func NewUnaggregatedOptions() UnaggregatedOptions {
    64  	p := pool.NewBytesPool(nil, nil)
    65  	p.Init()
    66  	return &unaggregatedOptions{
    67  		bytesPool:      p,
    68  		initBufferSize: defaultInitBufferSize,
    69  		maxMessageSize: defaultMaxUnaggregatedMessageSize,
    70  	}
    71  }
    72  
    73  func (o *unaggregatedOptions) SetBytesPool(value pool.BytesPool) UnaggregatedOptions {
    74  	opts := *o
    75  	opts.bytesPool = value
    76  	return &opts
    77  }
    78  
    79  func (o *unaggregatedOptions) BytesPool() pool.BytesPool {
    80  	return o.bytesPool
    81  }
    82  
    83  func (o *unaggregatedOptions) SetInitBufferSize(value int) UnaggregatedOptions {
    84  	opts := *o
    85  	opts.initBufferSize = value
    86  	return &opts
    87  }
    88  
    89  func (o *unaggregatedOptions) InitBufferSize() int {
    90  	return o.initBufferSize
    91  }
    92  
    93  func (o *unaggregatedOptions) SetMaxMessageSize(value int) UnaggregatedOptions {
    94  	opts := *o
    95  	opts.maxMessageSize = value
    96  	return &opts
    97  }
    98  
    99  func (o *unaggregatedOptions) MaxMessageSize() int {
   100  	return o.maxMessageSize
   101  }