github.com/m3db/m3@v1.5.0/src/cluster/services/heartbeat/etcd/options.go (about)

     1  // Copyright (c) 2016 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 etcd
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/cluster/services"
    28  	"github.com/m3db/m3/src/x/instrument"
    29  	"github.com/m3db/m3/src/x/retry"
    30  )
    31  
    32  var (
    33  	defaultRequestTimeout         = 10 * time.Second
    34  	defaultWatchChanCheckInterval = 10 * time.Second
    35  	defaultWatchChanResetInterval = 10 * time.Second
    36  	defaultWatchChanInitTimeout   = 10 * time.Second
    37  	defaultRetryOptions           = retry.NewOptions().SetMaxRetries(3)
    38  )
    39  
    40  // Options are options for the client of the kv store
    41  type Options interface {
    42  	// RequestTimeout is the timeout for etcd requests
    43  	RequestTimeout() time.Duration
    44  	// SetRequestTimeout sets the RequestTimeout
    45  	SetRequestTimeout(t time.Duration) Options
    46  
    47  	// InstrumentsOptions is the instrument options
    48  	InstrumentsOptions() instrument.Options
    49  	// SetInstrumentsOptions sets the InstrumentsOptions
    50  	SetInstrumentsOptions(iopts instrument.Options) Options
    51  
    52  	// RetryOptions is the retry options
    53  	RetryOptions() retry.Options
    54  	// SetRetryOptions sets the RetryOptions
    55  	SetRetryOptions(ropts retry.Options) Options
    56  
    57  	// WatchChanCheckInterval will be used to periodically check if a watch chan
    58  	// is no longer being subscribed and should be closed
    59  	WatchChanCheckInterval() time.Duration
    60  	// SetWatchChanCheckInterval sets the WatchChanCheckInterval
    61  	SetWatchChanCheckInterval(t time.Duration) Options
    62  
    63  	// WatchChanResetInterval is the delay before resetting the etcd watch chan
    64  	WatchChanResetInterval() time.Duration
    65  	// SetWatchChanResetInterval sets the WatchChanResetInterval
    66  	SetWatchChanResetInterval(t time.Duration) Options
    67  
    68  	// WatchChanInitTimeout is the timeout for a watchChan initialization
    69  	WatchChanInitTimeout() time.Duration
    70  	// SetWatchChanInitTimeout sets the WatchChanInitTimeout
    71  	SetWatchChanInitTimeout(t time.Duration) Options
    72  
    73  	// ServiceID returns the service the heartbeat store is managing heartbeats for.
    74  	ServiceID() services.ServiceID
    75  
    76  	// SetServiceID sets the service the heartbeat store is managing heartbeats for.
    77  	SetServiceID(sid services.ServiceID) Options
    78  
    79  	// Validate validates the Options
    80  	Validate() error
    81  }
    82  
    83  type options struct {
    84  	requestTimeout         time.Duration
    85  	iopts                  instrument.Options
    86  	ropts                  retry.Options
    87  	watchChanCheckInterval time.Duration
    88  	watchChanResetInterval time.Duration
    89  	watchChanInitTimeout   time.Duration
    90  	sid                    services.ServiceID
    91  }
    92  
    93  // NewOptions creates a sane default Option
    94  func NewOptions() Options {
    95  	o := options{}
    96  	return o.SetRequestTimeout(defaultRequestTimeout).
    97  		SetInstrumentsOptions(instrument.NewOptions()).
    98  		SetRetryOptions(defaultRetryOptions).
    99  		SetWatchChanCheckInterval(defaultWatchChanCheckInterval).
   100  		SetWatchChanInitTimeout(defaultWatchChanInitTimeout).
   101  		SetWatchChanResetInterval(defaultWatchChanResetInterval)
   102  }
   103  
   104  func (o options) Validate() error {
   105  	if o.iopts == nil {
   106  		return errors.New("no instrument options")
   107  	}
   108  
   109  	if o.ropts == nil {
   110  		return errors.New("no retry options")
   111  	}
   112  
   113  	if o.watchChanCheckInterval <= 0 {
   114  		return errors.New("invalid watch channel check interval")
   115  	}
   116  
   117  	return nil
   118  }
   119  
   120  func (o options) RequestTimeout() time.Duration {
   121  	return o.requestTimeout
   122  }
   123  
   124  func (o options) SetRequestTimeout(t time.Duration) Options {
   125  	o.requestTimeout = t
   126  	return o
   127  }
   128  
   129  func (o options) InstrumentsOptions() instrument.Options {
   130  	return o.iopts
   131  }
   132  
   133  func (o options) SetInstrumentsOptions(iopts instrument.Options) Options {
   134  	o.iopts = iopts
   135  	return o
   136  }
   137  
   138  func (o options) RetryOptions() retry.Options {
   139  	return o.ropts
   140  }
   141  
   142  func (o options) SetRetryOptions(ropts retry.Options) Options {
   143  	o.ropts = ropts
   144  	return o
   145  }
   146  
   147  func (o options) WatchChanCheckInterval() time.Duration {
   148  	return o.watchChanCheckInterval
   149  }
   150  
   151  func (o options) SetWatchChanCheckInterval(t time.Duration) Options {
   152  	o.watchChanCheckInterval = t
   153  	return o
   154  }
   155  
   156  func (o options) WatchChanResetInterval() time.Duration {
   157  	return o.watchChanResetInterval
   158  }
   159  
   160  func (o options) SetWatchChanResetInterval(t time.Duration) Options {
   161  	o.watchChanResetInterval = t
   162  	return o
   163  }
   164  
   165  func (o options) WatchChanInitTimeout() time.Duration {
   166  	return o.watchChanInitTimeout
   167  }
   168  
   169  func (o options) SetWatchChanInitTimeout(t time.Duration) Options {
   170  	o.watchChanInitTimeout = t
   171  	return o
   172  }
   173  
   174  func (o options) ServiceID() services.ServiceID {
   175  	return o.sid
   176  }
   177  
   178  func (o options) SetServiceID(sid services.ServiceID) Options {
   179  	o.sid = sid
   180  	return o
   181  }