github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/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 xresource "github.com/m3db/m3/src/x/resource" 25 "github.com/m3db/m3/src/x/watch" 26 ) 27 28 // OptionsManager manages runtime options. 29 type OptionsManager interface { 30 // SetRuntimeOptions sets the runtime options. 31 SetRuntimeOptions(value Options) 32 33 // RuntimeOptions returns the current runtime options. 34 RuntimeOptions() Options 35 36 // RegisterWatcher registers a watcher that watches updates to runtime options. 37 // When an update arrives, the manager will deliver the update to all registered 38 // watchers. 39 RegisterWatcher(l OptionsWatcher) xresource.SimpleCloser 40 41 // Close closes the watcher and all descendent watches 42 Close() 43 } 44 45 // OptionsWatcher watches for updates to runtime options. 46 type OptionsWatcher interface { 47 // SetRuntimeOptions is called for registerer watchers when the runtime options 48 // are updated, passing in the updated options as the argument. 49 SetRuntimeOptions(value Options) 50 } 51 52 type optionsManager struct { 53 watchable watch.Watchable 54 } 55 56 // NewOptionsManager creates a new runtime options manager. 57 func NewOptionsManager(initialValue Options) OptionsManager { 58 watchable := watch.NewWatchable() 59 watchable.Update(initialValue) 60 return &optionsManager{ 61 watchable: watchable, 62 } 63 } 64 65 func (w *optionsManager) SetRuntimeOptions(value Options) { 66 w.watchable.Update(value) 67 } 68 69 func (w *optionsManager) RuntimeOptions() Options { 70 return w.watchable.Get().(Options) 71 } 72 73 func (w *optionsManager) RegisterWatcher( 74 watcher OptionsWatcher, 75 ) xresource.SimpleCloser { 76 _, watch, _ := w.watchable.Watch() 77 78 // The watchable is always initialized so it's okay to do a blocking read. 79 <-watch.C() 80 81 // Deliver the current runtime options. 82 watcher.SetRuntimeOptions(watch.Get().(Options)) 83 84 // Spawn a new goroutine that will terminate when the 85 // watchable terminates on the close of the runtime options manager. 86 go func() { 87 for range watch.C() { 88 watcher.SetRuntimeOptions(watch.Get().(Options)) 89 } 90 }() 91 92 return watch 93 } 94 95 func (w *optionsManager) Close() { 96 w.watchable.Close() 97 }