github.com/m3db/m3@v1.5.0/src/cluster/kv/util/atomic.go (about) 1 // Copyright (c) 2018 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 util 22 23 import ( 24 "github.com/m3db/m3/src/cluster/kv" 25 26 "go.uber.org/atomic" 27 ) 28 29 // WatchAndUpdateAtomicBool sets up a watch with validation for an atomic bool 30 // property. Any malformed or invalid updates are not applied. The default value 31 // is applied when the key does not exist in KV. The watch on the value is returned. 32 func WatchAndUpdateAtomicBool( 33 store kv.Store, 34 key string, 35 property *atomic.Bool, 36 defaultValue bool, 37 opts Options, 38 ) (kv.ValueWatch, error) { 39 if opts == nil { 40 opts = NewOptions() 41 } 42 updateFn := func(i interface{}) { property.Store(i.(bool)) } 43 44 return watchAndUpdate( 45 store, key, getBool, updateFn, opts.ValidateFn(), defaultValue, opts.Logger(), 46 ) 47 } 48 49 // WatchAndUpdateAtomicFloat64 sets up a watch with validation for an atomic float64 50 // property. Any malformed or invalid updates are not applied. The default value is 51 // applied when the key does not exist in KV. The watch on the value is returned. 52 func WatchAndUpdateAtomicFloat64( 53 store kv.Store, 54 key string, 55 property *atomic.Float64, 56 defaultValue float64, 57 opts Options, 58 ) (kv.ValueWatch, error) { 59 if opts == nil { 60 opts = NewOptions() 61 } 62 updateFn := func(i interface{}) { property.Store(i.(float64)) } 63 64 return watchAndUpdate( 65 store, key, getFloat64, updateFn, opts.ValidateFn(), defaultValue, opts.Logger(), 66 ) 67 } 68 69 // WatchAndUpdateAtomicInt64 sets up a watch with validation for an atomic int64 70 // property. Anymalformed or invalid updates are not applied. The default value 71 // is applied when the key does not exist in KV. The watch on the value is returned. 72 func WatchAndUpdateAtomicInt64( 73 store kv.Store, 74 key string, 75 property *atomic.Int64, 76 defaultValue int64, 77 opts Options, 78 ) (kv.ValueWatch, error) { 79 if opts == nil { 80 opts = NewOptions() 81 } 82 updateFn := func(i interface{}) { property.Store(i.(int64)) } 83 84 return watchAndUpdate( 85 store, key, getInt64, updateFn, opts.ValidateFn(), defaultValue, opts.Logger(), 86 ) 87 } 88 89 // WatchAndUpdateAtomicString sets up a watch with validation for an atomic string 90 // property. Any malformed or invalid updates are not applied. The default value 91 // is applied when the key does not exist in KV. The watch on the value is returned. 92 func WatchAndUpdateAtomicString( 93 store kv.Store, 94 key string, 95 property *atomic.String, 96 defaultValue string, 97 opts Options, 98 ) (kv.ValueWatch, error) { 99 if opts == nil { 100 opts = NewOptions() 101 } 102 updateFn := func(i interface{}) { property.Store(i.(string)) } 103 104 return watchAndUpdate( 105 store, key, getString, updateFn, opts.ValidateFn(), defaultValue, opts.Logger(), 106 ) 107 }