github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3em/node/heartbeat_options.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 node 22 23 import ( 24 "fmt" 25 "time" 26 27 xclock "github.com/m3db/m3/src/x/clock" 28 ) 29 30 const ( 31 defaultEnabled = false 32 defaultHeartbeatInterval = 10 * time.Second 33 defaultCheckInterval = 2 * time.Second 34 defaultHeartbeatTimeout = 30 * time.Second 35 ) 36 37 var ( 38 defaultNowFn = time.Now 39 ) 40 41 type heartbeatOpts struct { 42 enabled bool 43 nowFn xclock.NowFn 44 interval time.Duration 45 checkInterval time.Duration 46 timeout time.Duration 47 router HeartbeatRouter 48 } 49 50 // NewHeartbeatOptions returns the default HeartbeatOptions 51 func NewHeartbeatOptions() HeartbeatOptions { 52 return &heartbeatOpts{ 53 enabled: defaultEnabled, 54 nowFn: defaultNowFn, 55 interval: defaultHeartbeatInterval, 56 checkInterval: defaultCheckInterval, 57 timeout: defaultHeartbeatTimeout, 58 } 59 } 60 61 func (ho *heartbeatOpts) Validate() error { 62 if ho.enabled && ho.router == nil { 63 return fmt.Errorf("HeartbeatRouter not set") 64 } 65 return nil 66 } 67 68 func (ho *heartbeatOpts) SetEnabled(f bool) HeartbeatOptions { 69 ho.enabled = f 70 return ho 71 } 72 73 func (ho *heartbeatOpts) Enabled() bool { 74 return ho.enabled 75 } 76 77 func (ho *heartbeatOpts) SetNowFn(fn xclock.NowFn) HeartbeatOptions { 78 ho.nowFn = fn 79 return ho 80 } 81 82 func (ho *heartbeatOpts) NowFn() xclock.NowFn { 83 return ho.nowFn 84 } 85 86 func (ho *heartbeatOpts) SetInterval(d time.Duration) HeartbeatOptions { 87 ho.interval = d 88 return ho 89 } 90 91 func (ho *heartbeatOpts) Interval() time.Duration { 92 return ho.interval 93 } 94 95 func (ho *heartbeatOpts) SetCheckInterval(td time.Duration) HeartbeatOptions { 96 ho.checkInterval = td 97 return ho 98 } 99 100 func (ho *heartbeatOpts) CheckInterval() time.Duration { 101 return ho.checkInterval 102 } 103 104 func (ho *heartbeatOpts) SetTimeout(d time.Duration) HeartbeatOptions { 105 ho.timeout = d 106 return ho 107 } 108 109 func (ho *heartbeatOpts) Timeout() time.Duration { 110 return ho.timeout 111 } 112 113 func (ho *heartbeatOpts) SetHeartbeatRouter(r HeartbeatRouter) HeartbeatOptions { 114 ho.router = r 115 return ho 116 } 117 118 func (ho *heartbeatOpts) HeartbeatRouter() HeartbeatRouter { 119 return ho.router 120 }