github.com/m3db/m3@v1.5.0/src/cluster/etcd/watchmanager/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 watchmanager
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/x/instrument"
    28  
    29  	clientv3 "go.etcd.io/etcd/client/v3"
    30  )
    31  
    32  const (
    33  	defaultWatchChanCheckInterval = 10 * time.Second
    34  	defaultWatchChanResetInterval = 10 * time.Second
    35  	defaultWatchChanInitTimeout   = 10 * time.Second
    36  )
    37  
    38  var (
    39  	errNilClient                 = errors.New("invalid options: nil client")
    40  	errNilUpdateFn               = errors.New("invalid options: nil updateFn")
    41  	errNilTickAndStopFn          = errors.New("invalid options: nil tickAndStopFn")
    42  	errNilInstrumentOptions      = errors.New("invalid options: nil instrument options")
    43  	errInvalidWatchCheckInterval = errors.New("invalid watch channel check interval")
    44  )
    45  
    46  // NewOptions creates sane options
    47  func NewOptions() Options {
    48  	return &options{
    49  		watchChanCheckInterval: defaultWatchChanCheckInterval,
    50  		watchChanResetInterval: defaultWatchChanResetInterval,
    51  		watchChanInitTimeout:   defaultWatchChanInitTimeout,
    52  		iopts:                  instrument.NewOptions(),
    53  	}
    54  }
    55  
    56  type options struct {
    57  	client        *clientv3.Client
    58  	updateFn      UpdateFn
    59  	tickAndStopFn TickAndStopFn
    60  
    61  	wopts                  []clientv3.OpOption
    62  	watchChanCheckInterval time.Duration
    63  	watchChanResetInterval time.Duration
    64  	watchChanInitTimeout   time.Duration
    65  	iopts                  instrument.Options
    66  }
    67  
    68  func (o *options) Client() *clientv3.Client {
    69  	return o.client
    70  }
    71  
    72  func (o *options) SetClient(cli *clientv3.Client) Options {
    73  	opts := *o
    74  	opts.client = cli
    75  	return &opts
    76  }
    77  
    78  func (o *options) WatchChanCheckInterval() time.Duration {
    79  	return o.watchChanCheckInterval
    80  }
    81  
    82  func (o *options) SetWatchChanCheckInterval(t time.Duration) Options {
    83  	opts := *o
    84  	opts.watchChanCheckInterval = t
    85  	return &opts
    86  }
    87  
    88  func (o *options) WatchChanResetInterval() time.Duration {
    89  	return o.watchChanResetInterval
    90  }
    91  
    92  func (o *options) SetWatchChanResetInterval(t time.Duration) Options {
    93  	opts := *o
    94  	opts.watchChanResetInterval = t
    95  	return &opts
    96  }
    97  
    98  func (o *options) WatchChanInitTimeout() time.Duration {
    99  	return o.watchChanInitTimeout
   100  }
   101  
   102  func (o *options) SetWatchChanInitTimeout(t time.Duration) Options {
   103  	opts := *o
   104  	opts.watchChanInitTimeout = t
   105  	return &opts
   106  }
   107  
   108  func (o *options) UpdateFn() UpdateFn {
   109  	return o.updateFn
   110  }
   111  
   112  func (o *options) SetUpdateFn(f UpdateFn) Options {
   113  	opts := *o
   114  	opts.updateFn = f
   115  	return &opts
   116  }
   117  
   118  func (o *options) TickAndStopFn() TickAndStopFn {
   119  	return o.tickAndStopFn
   120  }
   121  
   122  func (o *options) SetTickAndStopFn(f TickAndStopFn) Options {
   123  	opts := *o
   124  	opts.tickAndStopFn = f
   125  	return &opts
   126  }
   127  
   128  func (o *options) WatchOptions() []clientv3.OpOption {
   129  	return o.wopts
   130  }
   131  
   132  func (o *options) SetWatchOptions(options []clientv3.OpOption) Options {
   133  	opts := *o
   134  	opts.wopts = options
   135  	return &opts
   136  }
   137  
   138  func (o *options) InstrumentsOptions() instrument.Options {
   139  	return o.iopts
   140  }
   141  
   142  func (o *options) SetInstrumentsOptions(iopts instrument.Options) Options {
   143  	opts := *o
   144  	opts.iopts = iopts
   145  	return &opts
   146  }
   147  
   148  func (o *options) Validate() error {
   149  	if o.client == nil {
   150  		return errNilClient
   151  	}
   152  
   153  	if o.updateFn == nil {
   154  		return errNilUpdateFn
   155  	}
   156  
   157  	if o.tickAndStopFn == nil {
   158  		return errNilTickAndStopFn
   159  	}
   160  
   161  	if o.iopts == nil {
   162  		return errNilInstrumentOptions
   163  	}
   164  
   165  	if o.watchChanCheckInterval <= 0 {
   166  		return errInvalidWatchCheckInterval
   167  	}
   168  
   169  	return nil
   170  }