github.com/m3db/m3@v1.5.0/src/cluster/kv/util/runtime/options.go (about)

     1  // Copyright (c) 2017 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 runtime
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/cluster/kv"
    27  	"github.com/m3db/m3/src/x/instrument"
    28  )
    29  
    30  const (
    31  	defaultInitWatchTimeout = 10 * time.Second
    32  )
    33  
    34  // Options provide a set of value options.
    35  type Options interface {
    36  	// SetInstrumentOptions sets the instrument options.
    37  	SetInstrumentOptions(value instrument.Options) Options
    38  
    39  	// InstrumentOptions returns the instrument options.
    40  	InstrumentOptions() instrument.Options
    41  
    42  	// SetInitWatchTimeout sets the initial watch timeout.
    43  	SetInitWatchTimeout(value time.Duration) Options
    44  
    45  	// InitWatchTimeout returns the initial watch timeout.
    46  	InitWatchTimeout() time.Duration
    47  
    48  	// SetKVStore sets the kv store.
    49  	SetKVStore(value kv.Store) Options
    50  
    51  	// KVStore returns the kv store.
    52  	KVStore() kv.Store
    53  
    54  	// SetUnmarshalFn sets the unmarshal function.
    55  	SetUnmarshalFn(value UnmarshalFn) Options
    56  
    57  	// UnmarshalFn returns the unmarshal function.
    58  	UnmarshalFn() UnmarshalFn
    59  
    60  	// SetProcessFn sets the process function.
    61  	SetProcessFn(value ProcessFn) Options
    62  
    63  	// ProcessFn returns the process function.
    64  	ProcessFn() ProcessFn
    65  
    66  	// InterruptedCh returns the interrupted channel.
    67  	InterruptedCh() <-chan struct{}
    68  
    69  	// SetInterruptedCh sets the interrupted channel.
    70  	SetInterruptedCh(value <-chan struct{}) Options
    71  }
    72  
    73  type options struct {
    74  	instrumentOpts   instrument.Options
    75  	initWatchTimeout time.Duration
    76  	kvStore          kv.Store
    77  	unmarshalFn      UnmarshalFn
    78  	processFn        ProcessFn
    79  	interruptedCh    <-chan struct{}
    80  }
    81  
    82  // NewOptions creates a new set of options.
    83  func NewOptions() Options {
    84  	return &options{
    85  		instrumentOpts:   instrument.NewOptions(),
    86  		initWatchTimeout: defaultInitWatchTimeout,
    87  	}
    88  }
    89  
    90  func (o *options) SetInstrumentOptions(value instrument.Options) Options {
    91  	opts := *o
    92  	opts.instrumentOpts = value
    93  	return &opts
    94  }
    95  
    96  func (o *options) InstrumentOptions() instrument.Options {
    97  	return o.instrumentOpts
    98  }
    99  
   100  func (o *options) SetInitWatchTimeout(value time.Duration) Options {
   101  	opts := *o
   102  	opts.initWatchTimeout = value
   103  	return &opts
   104  }
   105  
   106  func (o *options) InitWatchTimeout() time.Duration {
   107  	return o.initWatchTimeout
   108  }
   109  
   110  func (o *options) SetKVStore(value kv.Store) Options {
   111  	opts := *o
   112  	opts.kvStore = value
   113  	return &opts
   114  }
   115  
   116  func (o *options) KVStore() kv.Store {
   117  	return o.kvStore
   118  }
   119  
   120  func (o *options) SetUnmarshalFn(value UnmarshalFn) Options {
   121  	opts := *o
   122  	opts.unmarshalFn = value
   123  	return &opts
   124  }
   125  
   126  func (o *options) UnmarshalFn() UnmarshalFn {
   127  	return o.unmarshalFn
   128  }
   129  
   130  func (o *options) SetProcessFn(value ProcessFn) Options {
   131  	opts := *o
   132  	opts.processFn = value
   133  	return &opts
   134  }
   135  
   136  func (o *options) ProcessFn() ProcessFn {
   137  	return o.processFn
   138  }
   139  
   140  func (o *options) SetInterruptedCh(ch <-chan struct{}) Options {
   141  	o.interruptedCh = ch
   142  	return o
   143  }
   144  
   145  func (o *options) InterruptedCh() <-chan struct{} {
   146  	return o.interruptedCh
   147  }