MCPcopy
hub / github.com/minio/minio-go / HealthCheck

Method HealthCheck

api.go:478–528  ·  view source on GitHub ↗

HealthCheck starts a healthcheck to see if endpoint is up. Returns a context cancellation function, to stop the health check, and an error if health check is already started.

(hcDuration time.Duration)

Source from the content-addressed store, hash-verified

476// Returns a context cancellation function, to stop the health check,
477// and an error if health check is already started.
478func (c *Client) HealthCheck(hcDuration time.Duration) (context.CancelFunc, error) {
479 if atomic.LoadInt32(&c.healthStatus) != unknown {
480 return nil, fmt.Errorf("health check is running")
481 }
482 if hcDuration < 1*time.Second {
483 return nil, fmt.Errorf("health check duration should be at least 1 second")
484 }
485 probeBucketName := randString(60, rand.NewSource(time.Now().UnixNano()), "probe-health-")
486 ctx, cancelFn := context.WithCancel(context.Background())
487 atomic.StoreInt32(&c.healthStatus, offline)
488 {
489 // Change to online, if we can connect.
490 gctx, gcancel := context.WithTimeout(ctx, 3*time.Second)
491 _, err := c.getBucketLocation(gctx, probeBucketName)
492 gcancel()
493 if !IsNetworkOrHostDown(err, false) {
494 switch ToErrorResponse(err).Code {
495 case NoSuchBucket, AccessDenied, "":
496 atomic.CompareAndSwapInt32(&c.healthStatus, offline, online)
497 }
498 }
499 }
500
501 go func(duration time.Duration) {
502 timer := time.NewTimer(duration)
503 defer timer.Stop()
504 for {
505 select {
506 case <-ctx.Done():
507 atomic.StoreInt32(&c.healthStatus, unknown)
508 return
509 case <-timer.C:
510 // Do health check the first time and ONLY if the connection is marked offline
511 if c.IsOffline() {
512 gctx, gcancel := context.WithTimeout(context.Background(), 3*time.Second)
513 _, err := c.getBucketLocation(gctx, probeBucketName)
514 gcancel()
515 if !IsNetworkOrHostDown(err, false) {
516 switch ToErrorResponse(err).Code {
517 case NoSuchBucket, AccessDenied, "":
518 atomic.CompareAndSwapInt32(&c.healthStatus, offline, online)
519 }
520 }
521 }
522
523 timer.Reset(duration)
524 }
525 }
526 }(hcDuration)
527 return cancelFn, nil
528}
529
530// requestMetadata - is container for all the values to make a request.
531type requestMetadata struct {

Callers 2

TestHealthCheckFunction · 0.80
mainFunction · 0.80

Calls 5

getBucketLocationMethod · 0.95
IsOfflineMethod · 0.95
IsNetworkOrHostDownFunction · 0.85
ToErrorResponseFunction · 0.85
randStringFunction · 0.70

Tested by 1

TestHealthCheckFunction · 0.64