github.com/m3db/m3@v1.5.0/src/x/watch/source.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 watch
    22  
    23  import (
    24  	"errors"
    25  	"sync"
    26  
    27  	xresource "github.com/m3db/m3/src/x/resource"
    28  
    29  	"go.uber.org/zap"
    30  )
    31  
    32  // ErrSourceClosed indicates that the Source should be closed.
    33  var ErrSourceClosed = errors.New("source closed")
    34  
    35  // SourceInput provides data for Source,
    36  type SourceInput interface {
    37  	// Poll will be called by Source for data. Any backoff/jitter logic should
    38  	// be handled here.
    39  	Poll() (interface{}, error)
    40  }
    41  
    42  // Source polls data by calling SourcePollFn and notifies its watches on updates.
    43  type Source interface {
    44  	xresource.SimpleCloser
    45  
    46  	// Get returns the latest value.
    47  	Get() interface{}
    48  
    49  	// Watch returns the value and a Watch.
    50  	Watch() (interface{}, Watch, error)
    51  }
    52  
    53  // NewSource returns a new Source.
    54  func NewSource(input SourceInput, logger *zap.Logger) Source {
    55  	s := &source{
    56  		input:  input,
    57  		w:      NewWatchable(),
    58  		logger: logger,
    59  	}
    60  
    61  	go s.run()
    62  	return s
    63  }
    64  
    65  type source struct {
    66  	sync.RWMutex
    67  
    68  	input  SourceInput
    69  	w      Watchable
    70  	closed bool
    71  	logger *zap.Logger
    72  }
    73  
    74  func (s *source) run() {
    75  	for !s.isClosed() {
    76  		data, err := s.input.Poll()
    77  		if err == ErrSourceClosed {
    78  			s.logger.Error("watch source upstream is closed")
    79  			s.Close()
    80  			return
    81  		}
    82  		if err != nil {
    83  			s.logger.Error("watch source poll error", zap.Error(err))
    84  			continue
    85  		}
    86  
    87  		if err = s.w.Update(data); err != nil {
    88  			s.logger.Error("watch source update error", zap.Error(err))
    89  		}
    90  	}
    91  }
    92  
    93  func (s *source) isClosed() bool {
    94  	s.RLock()
    95  	defer s.RUnlock()
    96  	return s.closed
    97  }
    98  
    99  func (s *source) Close() {
   100  	s.Lock()
   101  	defer s.Unlock()
   102  	if s.closed {
   103  		return
   104  	}
   105  	s.closed = true
   106  	s.w.Close()
   107  }
   108  
   109  func (s *source) Get() interface{} {
   110  	return s.w.Get()
   111  }
   112  
   113  func (s *source) Watch() (interface{}, Watch, error) {
   114  	return s.w.Watch()
   115  }