11package services
22
33import (
4+ "context"
45 "errors"
56 "fmt"
67 "maps"
@@ -88,10 +89,10 @@ type HealthCheckerConfig struct {
8889 // Optionally override debug.BuildInfo
8990 Ver , Sha string
9091 // Optional hooks for reporting.
91- IncVersion func (ver string , sha string )
92- AddUptime func (duration time.Duration )
93- SetStatus func (name string , status int )
94- Delete func (name string )
92+ IncVersion func (ctx context. Context , ver string , sha string )
93+ AddUptime func (ctx context. Context , duration time.Duration )
94+ SetStatus func (ctx context. Context , name string , status int )
95+ Delete func (ctx context. Context , name string )
9596}
9697
9798func (cfg HealthCheckerConfig ) initVerSha () {
@@ -112,16 +113,16 @@ func (cfg HealthCheckerConfig) initVerSha() {
112113
113114func (cfg HealthCheckerConfig ) setNoopHooks () {
114115 if cfg .IncVersion == nil {
115- cfg .IncVersion = func (ver , sha string ) {}
116+ cfg .IncVersion = func (ctx context. Context , ver , sha string ) {}
116117 }
117118 if cfg .AddUptime == nil {
118- cfg .AddUptime = func (d time.Duration ) {}
119+ cfg .AddUptime = func (ctx context. Context , d time.Duration ) {}
119120 }
120121 if cfg .SetStatus == nil {
121- cfg .SetStatus = func (name string , status int ) {}
122+ cfg .SetStatus = func (ctx context. Context , name string , status int ) {}
122123 }
123124 if cfg .Delete == nil {
124- cfg .Delete = func (name string ) {}
125+ cfg .Delete = func (ctx context. Context , name string ) {}
125126 }
126127}
127128
@@ -140,10 +141,11 @@ func (cfg HealthCheckerConfig) New() *HealthChecker {
140141
141142func (c * HealthChecker ) Start () error {
142143 return c .StartOnce ("HealthCheck" , func () error {
143- c .cfg .IncVersion (c .cfg .Ver , c .cfg .Sha )
144+ ctx := context .Background ()
145+ c .cfg .IncVersion (ctx , c .cfg .Ver , c .cfg .Sha )
144146
145147 // update immediately
146- c .update ()
148+ c .update (ctx )
147149
148150 go c .run ()
149151
@@ -162,19 +164,20 @@ func (c *HealthChecker) Close() error {
162164func (c * HealthChecker ) run () {
163165 defer close (c .chDone )
164166
167+ ctx := context .Background ()
165168 ticker := time .NewTicker (interval )
166169
167170 for {
168171 select {
169172 case <- ticker .C :
170- c .update ()
173+ c .update (ctx )
171174 case <- c .chStop :
172175 return
173176 }
174177 }
175178}
176179
177- func (c * HealthChecker ) update () {
180+ func (c * HealthChecker ) update (ctx context. Context ) {
178181 // copy services into a new map to avoid lock contention while doing checks
179182 c .servicesMu .RLock ()
180183 l := len (c .services )
@@ -196,10 +199,10 @@ func (c *HealthChecker) update() {
196199 }
197200
198201 // report metrics to prometheus
199- c .cfg .SetStatus (name , value )
202+ c .cfg .SetStatus (ctx , name , value )
200203 }
201204 }
202- c .cfg .AddUptime (interval )
205+ c .cfg .AddUptime (ctx , interval )
203206
204207 // save state
205208 c .stateMu .Lock ()
@@ -231,11 +234,12 @@ func (c *HealthChecker) Unregister(name string) error {
231234 if name == "" {
232235 return fmt .Errorf ("name cannot be empty" )
233236 }
237+ ctx := context .Background ()
234238
235239 c .servicesMu .Lock ()
236240 defer c .servicesMu .Unlock ()
237241 delete (c .services , name )
238- c .cfg .Delete (name )
242+ c .cfg .Delete (ctx , name )
239243 return nil
240244}
241245
0 commit comments