github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/producer/buffer/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 buffer
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/x/instrument"
    28  	"github.com/m3db/m3/src/x/retry"
    29  )
    30  
    31  const (
    32  	defaultMaxBufferSize         = 100 * 1024 * 1024 // 100MB.
    33  	defaultMaxMessageSize        = 1 * 1024 * 1024   // 1MB.
    34  	defaultCloseCheckInterval    = time.Second
    35  	defaultDropOldestInterval    = time.Second
    36  	defaultScanBatchSize         = 16
    37  	defaultCleanupInitialBackoff = 10 * time.Second
    38  	defaultAllowedSpilloverRatio = 0.2
    39  	defaultCleanupMaxBackoff     = time.Minute
    40  )
    41  
    42  var (
    43  	errInvalidScanBatchSize   = errors.New("invalid scan batch size")
    44  	errInvalidMaxMessageSize  = errors.New("invalid max message size")
    45  	errNegativeMaxBufferSize  = errors.New("negative max buffer size")
    46  	errNegativeMaxMessageSize = errors.New("negative max message size")
    47  )
    48  
    49  type bufferOptions struct {
    50  	strategy              OnFullStrategy
    51  	maxBufferSize         int
    52  	maxMessageSize        int
    53  	closeCheckInterval    time.Duration
    54  	dropOldestInterval    time.Duration
    55  	scanBatchSize         int
    56  	allowedSpilloverRatio float64
    57  	rOpts                 retry.Options
    58  	iOpts                 instrument.Options
    59  }
    60  
    61  // NewOptions creates Options.
    62  func NewOptions() Options {
    63  	return &bufferOptions{
    64  		strategy:              DropOldest,
    65  		maxBufferSize:         defaultMaxBufferSize,
    66  		maxMessageSize:        defaultMaxMessageSize,
    67  		closeCheckInterval:    defaultCloseCheckInterval,
    68  		dropOldestInterval:    defaultDropOldestInterval,
    69  		scanBatchSize:         defaultScanBatchSize,
    70  		allowedSpilloverRatio: defaultAllowedSpilloverRatio,
    71  		rOpts: retry.NewOptions().
    72  			SetInitialBackoff(defaultCleanupInitialBackoff).
    73  			SetMaxBackoff(defaultCleanupMaxBackoff).
    74  			SetForever(true),
    75  		iOpts: instrument.NewOptions(),
    76  	}
    77  }
    78  
    79  func (opts *bufferOptions) OnFullStrategy() OnFullStrategy {
    80  	return opts.strategy
    81  }
    82  
    83  func (opts *bufferOptions) SetOnFullStrategy(value OnFullStrategy) Options {
    84  	o := *opts
    85  	o.strategy = value
    86  	return &o
    87  }
    88  
    89  func (opts *bufferOptions) MaxMessageSize() int {
    90  	return opts.maxMessageSize
    91  }
    92  
    93  func (opts *bufferOptions) SetMaxMessageSize(value int) Options {
    94  	o := *opts
    95  	o.maxMessageSize = value
    96  	return &o
    97  }
    98  
    99  func (opts *bufferOptions) MaxBufferSize() int {
   100  	return opts.maxBufferSize
   101  }
   102  
   103  func (opts *bufferOptions) SetMaxBufferSize(value int) Options {
   104  	o := *opts
   105  	o.maxBufferSize = value
   106  	return &o
   107  }
   108  
   109  func (opts *bufferOptions) CloseCheckInterval() time.Duration {
   110  	return opts.closeCheckInterval
   111  }
   112  
   113  func (opts *bufferOptions) SetCloseCheckInterval(value time.Duration) Options {
   114  	o := *opts
   115  	o.closeCheckInterval = value
   116  	return &o
   117  }
   118  
   119  func (opts *bufferOptions) DropOldestInterval() time.Duration {
   120  	return opts.dropOldestInterval
   121  }
   122  
   123  func (opts *bufferOptions) SetDropOldestInterval(value time.Duration) Options {
   124  	o := *opts
   125  	o.dropOldestInterval = value
   126  	return &o
   127  }
   128  
   129  func (opts *bufferOptions) ScanBatchSize() int {
   130  	return opts.scanBatchSize
   131  }
   132  
   133  func (opts *bufferOptions) SetScanBatchSize(value int) Options {
   134  	o := *opts
   135  	o.scanBatchSize = value
   136  	return &o
   137  }
   138  
   139  func (opts *bufferOptions) AllowedSpilloverRatio() float64 {
   140  	return opts.allowedSpilloverRatio
   141  }
   142  
   143  func (opts *bufferOptions) SetAllowedSpilloverRatio(value float64) Options {
   144  	o := *opts
   145  	o.allowedSpilloverRatio = value
   146  	return &o
   147  }
   148  
   149  func (opts *bufferOptions) CleanupRetryOptions() retry.Options {
   150  	return opts.rOpts
   151  }
   152  
   153  func (opts *bufferOptions) SetCleanupRetryOptions(value retry.Options) Options {
   154  	o := *opts
   155  	o.rOpts = value
   156  	return &o
   157  }
   158  
   159  func (opts *bufferOptions) InstrumentOptions() instrument.Options {
   160  	return opts.iOpts
   161  }
   162  
   163  func (opts *bufferOptions) SetInstrumentOptions(value instrument.Options) Options {
   164  	o := *opts
   165  	o.iOpts = value
   166  	return &o
   167  }
   168  
   169  func (opts *bufferOptions) Validate() error {
   170  	if opts.ScanBatchSize() <= 0 {
   171  		return errInvalidScanBatchSize
   172  	}
   173  	if opts.MaxBufferSize() <= 0 {
   174  		return errNegativeMaxBufferSize
   175  	}
   176  	if opts.MaxMessageSize() <= 0 {
   177  		return errNegativeMaxMessageSize
   178  	}
   179  	if opts.MaxMessageSize() > opts.MaxBufferSize() {
   180  		// Max message size can only be as large as max buffer size.
   181  		return errInvalidMaxMessageSize
   182  	}
   183  	return nil
   184  }