github.com/m3db/m3@v1.5.0/src/cluster/etcd/watchmanager/types.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  	"time"
    25  
    26  	"github.com/m3db/m3/src/x/instrument"
    27  
    28  	clientv3 "go.etcd.io/etcd/client/v3"
    29  )
    30  
    31  // WatchManager manages etcd watch on a key
    32  type WatchManager interface {
    33  	// Watch watches the key forever until the CheckAndStopFn returns true
    34  	Watch(key string)
    35  }
    36  
    37  // UpdateFn is called when an event on the watch channel happens
    38  type UpdateFn func(key string, events []*clientv3.Event) error
    39  
    40  // TickAndStopFn is called every once a while
    41  // to check and stop the watch if needed
    42  type TickAndStopFn func(key string) bool
    43  
    44  // Options are options for the etcd watch helper
    45  type Options interface {
    46  	// Client sets the etcd client
    47  	Client() *clientv3.Client
    48  	// SetClient sets the etcd client
    49  	SetClient(cli *clientv3.Client) Options
    50  
    51  	// UpdateFn is the function called when a notification on a key is received
    52  	UpdateFn() UpdateFn
    53  	// SetUpdateFn sets the UpdateFn
    54  	SetUpdateFn(f UpdateFn) Options
    55  
    56  	// TickAndStopFn is the function called periodically to check if a watch should be stopped
    57  	TickAndStopFn() TickAndStopFn
    58  	// SetTickAndStopFn sets the TickAndStopFn
    59  	SetTickAndStopFn(f TickAndStopFn) Options
    60  
    61  	// WatchOptions is a set of options for the etcd watch
    62  	WatchOptions() []clientv3.OpOption
    63  	// SetWatchOptions sets the WatchOptions
    64  	SetWatchOptions(opts []clientv3.OpOption) Options
    65  
    66  	// WatchChanCheckInterval will be used to periodically check if a watch chan
    67  	// is no longer being subscribed and should be closed
    68  	WatchChanCheckInterval() time.Duration
    69  	// SetWatchChanCheckInterval sets the WatchChanCheckInterval
    70  	SetWatchChanCheckInterval(t time.Duration) Options
    71  
    72  	// WatchChanResetInterval is the delay before resetting the etcd watch chan
    73  	WatchChanResetInterval() time.Duration
    74  	// SetWatchChanResetInterval sets the WatchChanResetInterval
    75  	SetWatchChanResetInterval(t time.Duration) Options
    76  
    77  	// WatchChanInitTimeout is the timeout for a watchChan initialization
    78  	WatchChanInitTimeout() time.Duration
    79  	// SetWatchChanInitTimeout sets the WatchChanInitTimeout
    80  	SetWatchChanInitTimeout(t time.Duration) Options
    81  
    82  	// InstrumentsOptions is the instrument options
    83  	InstrumentsOptions() instrument.Options
    84  	// SetInstrumentsOptions sets the InstrumentsOptions
    85  	SetInstrumentsOptions(iopts instrument.Options) Options
    86  
    87  	// Validate validates the options
    88  	Validate() error
    89  }