github.com/m3db/m3@v1.5.0/src/aggregator/client/conn_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 client
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/x/clock"
    27  	"github.com/m3db/m3/src/x/instrument"
    28  	xio "github.com/m3db/m3/src/x/io"
    29  	"github.com/m3db/m3/src/x/retry"
    30  )
    31  
    32  const (
    33  	defaultConnectionTimeout            = 1 * time.Second
    34  	defaultConnectionKeepAlive          = true
    35  	defaultWriteTimeout                 = 15 * time.Second
    36  	defaultInitReconnectThreshold       = 1
    37  	defaultMaxReconnectThreshold        = 4
    38  	defaultReconnectThresholdMultiplier = 2
    39  	defaultMaxReconnectDuration         = 20 * time.Second
    40  	defaultWriteRetryInitialBackoff     = 0
    41  	defaultWriteRetryBackoffFactor      = 2
    42  	defaultWriteRetryMaxBackoff         = time.Second
    43  	defaultWriteRetryMaxRetries         = 1
    44  	defaultWriteRetryJitterEnabled      = true
    45  )
    46  
    47  // ConnectionOptions provides a set of options for tcp connections.
    48  type ConnectionOptions interface {
    49  	// SetInstrumentOptions sets the instrument options.
    50  	SetClockOptions(value clock.Options) ConnectionOptions
    51  
    52  	// ClockOptions returns the clock options.
    53  	ClockOptions() clock.Options
    54  
    55  	// SetInstrumentOptions sets the instrument options.
    56  	SetInstrumentOptions(value instrument.Options) ConnectionOptions
    57  
    58  	// InstrumentOptions returns the instrument options.
    59  	InstrumentOptions() instrument.Options
    60  
    61  	// SetConnectionTimeout sets the timeout for establishing connections.
    62  	SetConnectionTimeout(value time.Duration) ConnectionOptions
    63  
    64  	// ConnectionTimeout returns the timeout for establishing connections.
    65  	ConnectionTimeout() time.Duration
    66  
    67  	// SetConnectionKeepAlive sets the keepAlive for the connection.
    68  	SetConnectionKeepAlive(value bool) ConnectionOptions
    69  
    70  	// ConnectionKeepAlive returns the keepAlive for the connection.
    71  	ConnectionKeepAlive() bool
    72  
    73  	// SetWriteTimeout sets the timeout for writing data.
    74  	SetWriteTimeout(value time.Duration) ConnectionOptions
    75  
    76  	// WriteTimeout returns the timeout for writing data.
    77  	WriteTimeout() time.Duration
    78  
    79  	// SetInitReconnectThreshold sets the initial threshold for re-establshing connections.
    80  	SetInitReconnectThreshold(value int) ConnectionOptions
    81  
    82  	// InitReconnectThreshold returns the initial threshold for re-establishing connections.
    83  	InitReconnectThreshold() int
    84  
    85  	// SetMaxReconnectThreshold sets the max threshold for re-establishing connections.
    86  	SetMaxReconnectThreshold(value int) ConnectionOptions
    87  
    88  	// MaxReconnectThreshold returns the max threshold for re-establishing connections.
    89  	MaxReconnectThreshold() int
    90  
    91  	// SetReconnectThresholdMultiplier sets the threshold multiplier.
    92  	SetReconnectThresholdMultiplier(value int) ConnectionOptions
    93  
    94  	// ReconnectThresholdMultiplier returns the threshold multiplier.
    95  	ReconnectThresholdMultiplier() int
    96  
    97  	// SetMaxReconnectDuration sets the max duration between attempts to re-establish connections.
    98  	SetMaxReconnectDuration(value time.Duration) ConnectionOptions
    99  
   100  	// MaxReconnectDuration returns the max duration between attempts to re-establish connections.
   101  	MaxReconnectDuration() time.Duration
   102  
   103  	// SetWriteRetryOptions sets the retry options for retrying failed writes.
   104  	SetWriteRetryOptions(value retry.Options) ConnectionOptions
   105  
   106  	// WriteRetryOptions returns the retry options for retrying failed writes.
   107  	WriteRetryOptions() retry.Options
   108  
   109  	// SetRWOptions sets RW options.
   110  	SetRWOptions(value xio.Options) ConnectionOptions
   111  
   112  	// RWOptions returns the RW options.
   113  	RWOptions() xio.Options
   114  }
   115  
   116  type connectionOptions struct {
   117  	clockOpts      clock.Options
   118  	instrumentOpts instrument.Options
   119  	writeRetryOpts retry.Options
   120  	rwOpts         xio.Options
   121  	connTimeout    time.Duration
   122  	writeTimeout   time.Duration
   123  	maxDuration    time.Duration
   124  	initThreshold  int
   125  	maxThreshold   int
   126  	multiplier     int
   127  	connKeepAlive  bool
   128  }
   129  
   130  // NewConnectionOptions create a new set of connection options.
   131  func NewConnectionOptions() ConnectionOptions {
   132  	defaultWriteRetryOpts := retry.NewOptions().
   133  		SetInitialBackoff(defaultWriteRetryInitialBackoff).
   134  		SetBackoffFactor(defaultWriteRetryBackoffFactor).
   135  		SetMaxBackoff(defaultWriteRetryMaxBackoff).
   136  		SetMaxRetries(defaultWriteRetryMaxRetries).
   137  		SetJitter(defaultWriteRetryJitterEnabled)
   138  	return &connectionOptions{
   139  		clockOpts:      clock.NewOptions(),
   140  		instrumentOpts: instrument.NewOptions(),
   141  		connTimeout:    defaultConnectionTimeout,
   142  		connKeepAlive:  defaultConnectionKeepAlive,
   143  		writeTimeout:   defaultWriteTimeout,
   144  		initThreshold:  defaultInitReconnectThreshold,
   145  		maxThreshold:   defaultMaxReconnectThreshold,
   146  		multiplier:     defaultReconnectThresholdMultiplier,
   147  		maxDuration:    defaultMaxReconnectDuration,
   148  		writeRetryOpts: defaultWriteRetryOpts,
   149  		rwOpts:         xio.NewOptions(),
   150  	}
   151  }
   152  
   153  func (o *connectionOptions) SetClockOptions(value clock.Options) ConnectionOptions {
   154  	opts := *o
   155  	opts.clockOpts = value
   156  	return &opts
   157  }
   158  
   159  func (o *connectionOptions) ClockOptions() clock.Options {
   160  	return o.clockOpts
   161  }
   162  
   163  func (o *connectionOptions) SetInstrumentOptions(value instrument.Options) ConnectionOptions {
   164  	opts := *o
   165  	opts.instrumentOpts = value
   166  	return &opts
   167  }
   168  
   169  func (o *connectionOptions) InstrumentOptions() instrument.Options {
   170  	return o.instrumentOpts
   171  }
   172  
   173  func (o *connectionOptions) SetConnectionTimeout(value time.Duration) ConnectionOptions {
   174  	opts := *o
   175  	opts.connTimeout = value
   176  	return &opts
   177  }
   178  
   179  func (o *connectionOptions) ConnectionTimeout() time.Duration {
   180  	return o.connTimeout
   181  }
   182  
   183  func (o *connectionOptions) SetConnectionKeepAlive(value bool) ConnectionOptions {
   184  	opts := *o
   185  	opts.connKeepAlive = value
   186  	return &opts
   187  }
   188  
   189  func (o *connectionOptions) ConnectionKeepAlive() bool {
   190  	return o.connKeepAlive
   191  }
   192  
   193  func (o *connectionOptions) SetWriteTimeout(value time.Duration) ConnectionOptions {
   194  	opts := *o
   195  	opts.writeTimeout = value
   196  	return &opts
   197  }
   198  
   199  func (o *connectionOptions) WriteTimeout() time.Duration {
   200  	return o.writeTimeout
   201  }
   202  
   203  func (o *connectionOptions) SetInitReconnectThreshold(value int) ConnectionOptions {
   204  	opts := *o
   205  	opts.initThreshold = value
   206  	return &opts
   207  }
   208  
   209  func (o *connectionOptions) InitReconnectThreshold() int {
   210  	return o.initThreshold
   211  }
   212  
   213  func (o *connectionOptions) SetMaxReconnectThreshold(value int) ConnectionOptions {
   214  	opts := *o
   215  	opts.maxThreshold = value
   216  	return &opts
   217  }
   218  
   219  func (o *connectionOptions) MaxReconnectThreshold() int {
   220  	return o.maxThreshold
   221  }
   222  
   223  func (o *connectionOptions) SetReconnectThresholdMultiplier(value int) ConnectionOptions {
   224  	opts := *o
   225  	opts.multiplier = value
   226  	return &opts
   227  }
   228  
   229  func (o *connectionOptions) ReconnectThresholdMultiplier() int {
   230  	return o.multiplier
   231  }
   232  
   233  func (o *connectionOptions) SetMaxReconnectDuration(value time.Duration) ConnectionOptions {
   234  	opts := *o
   235  	opts.maxDuration = value
   236  	return &opts
   237  }
   238  
   239  func (o *connectionOptions) MaxReconnectDuration() time.Duration {
   240  	return o.maxDuration
   241  }
   242  
   243  func (o *connectionOptions) SetWriteRetryOptions(value retry.Options) ConnectionOptions {
   244  	opts := *o
   245  	opts.writeRetryOpts = value
   246  	return &opts
   247  }
   248  
   249  func (o *connectionOptions) WriteRetryOptions() retry.Options {
   250  	return o.writeRetryOpts
   251  }
   252  
   253  func (o *connectionOptions) SetRWOptions(value xio.Options) ConnectionOptions {
   254  	opts := *o
   255  	opts.rwOpts = value
   256  	return &opts
   257  }
   258  
   259  func (o *connectionOptions) RWOptions() xio.Options {
   260  	return o.rwOpts
   261  }