github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/runtime/runtime_options_manager.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  	"fmt"
    25  
    26  	xresource "github.com/m3db/m3/src/x/resource"
    27  	xwatch "github.com/m3db/m3/src/x/watch"
    28  )
    29  
    30  type optionsManager struct {
    31  	watchable xwatch.Watchable
    32  }
    33  
    34  // NewOptionsManager creates a new runtime options manager
    35  func NewOptionsManager() OptionsManager {
    36  	watchable := xwatch.NewWatchable()
    37  	watchable.Update(NewOptions())
    38  	return &optionsManager{watchable: watchable}
    39  }
    40  
    41  func (w *optionsManager) Update(value Options) error {
    42  	if err := value.Validate(); err != nil {
    43  		return err
    44  	}
    45  	w.watchable.Update(value)
    46  	return nil
    47  }
    48  
    49  func (w *optionsManager) Get() Options {
    50  	return w.watchable.Get().(Options)
    51  }
    52  
    53  func (w *optionsManager) RegisterListener(
    54  	listener OptionsListener,
    55  ) xresource.SimpleCloser {
    56  	_, watch, _ := w.watchable.Watch()
    57  
    58  	// We always initialize the watchable so always read
    59  	// the first notification value
    60  	<-watch.C()
    61  
    62  	// Deliver the current runtime options
    63  	listener.SetRuntimeOptions(watch.Get().(Options))
    64  
    65  	// Spawn a new goroutine that will terminate when the
    66  	// watchable terminates on the close of the runtime options manager
    67  	go func() {
    68  		for range watch.C() {
    69  			listener.SetRuntimeOptions(watch.Get().(Options))
    70  		}
    71  	}()
    72  
    73  	return watch
    74  }
    75  
    76  func (w *optionsManager) Close() {
    77  	w.watchable.Close()
    78  }
    79  
    80  // NewNoOpOptionsManager returns a no-op options manager that cannot
    81  // be updated and does not spawn backround goroutines (useful for globals
    82  // in test files).
    83  func NewNoOpOptionsManager(opts Options) OptionsManager {
    84  	return noOpOptionsManager{opts: opts}
    85  }
    86  
    87  type noOpOptionsManager struct {
    88  	opts Options
    89  }
    90  
    91  func (n noOpOptionsManager) Update(value Options) error {
    92  	return fmt.Errorf("no-op options manager cannot update options")
    93  }
    94  
    95  func (n noOpOptionsManager) Get() Options {
    96  	return n.opts
    97  }
    98  
    99  func (n noOpOptionsManager) RegisterListener(
   100  	listener OptionsListener,
   101  ) xresource.SimpleCloser {
   102  	// noOpOptionsManager never changes its options, not worth
   103  	// registering listener
   104  	return noOpCloser{}
   105  }
   106  
   107  func (n noOpOptionsManager) Close() {
   108  }
   109  
   110  type noOpCloser struct{}
   111  
   112  func (n noOpCloser) Close() {
   113  
   114  }