summaryrefslogtreecommitdiff
path: root/service/health
diff options
context:
space:
mode:
Diffstat (limited to 'service/health')
-rw-r--r--service/health/service.go21
-rw-r--r--service/health/service_test.go54
2 files changed, 64 insertions, 11 deletions
diff --git a/service/health/service.go b/service/health/service.go
index c0be68e0..c82f43b5 100644
--- a/service/health/service.go
+++ b/service/health/service.go
@@ -2,6 +2,8 @@ package health
import (
"context"
+ "fmt"
+ "github.com/sirupsen/logrus"
"net/http"
"sync"
@@ -14,19 +16,21 @@ const ID = "health"
// Service to serve an endpoint for checking the health of the worker pool
type Service struct {
cfg *Config
+ log *logrus.Logger
mu sync.Mutex
http *http.Server
httpService *rrhttp.Service
}
// Init health service
-func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) {
+func (s *Service) Init(cfg *Config, r *rrhttp.Service, log *logrus.Logger) (bool, error) {
// Ensure the httpService is set
if r == nil {
return false, nil
}
s.cfg = cfg
+ s.log = log
s.httpService = r
return true, nil
}
@@ -37,7 +41,13 @@ func (s *Service) Serve() error {
s.mu.Lock()
s.http = &http.Server{Addr: s.cfg.Address, Handler: s}
s.mu.Unlock()
- return s.http.ListenAndServe()
+
+ err := s.http.ListenAndServe()
+ if err == nil || err == http.ErrServerClosed {
+ return nil
+ }
+
+ return err
}
// Stop the health endpoint
@@ -47,7 +57,12 @@ func (s *Service) Stop() {
if s.http != nil {
// gracefully stop the server
- go s.http.Shutdown(context.Background())
+ go func() {
+ err := s.http.Shutdown(context.Background())
+ if err != nil && err != http.ErrServerClosed {
+ s.log.Error(fmt.Errorf("error shutting down the metrics server: error %v", err))
+ }
+ }()
}
}
diff --git a/service/health/service_test.go b/service/health/service_test.go
index 76462df9..d346d92b 100644
--- a/service/health/service_test.go
+++ b/service/health/service_test.go
@@ -66,7 +66,12 @@ func TestService_Serve(t *testing.T) {
assert.NotNil(t, hS)
assert.Equal(t, service.StatusOK, httpStatus)
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 500)
defer c.Stop()
@@ -104,13 +109,21 @@ func TestService_Serve_DeadWorker(t *testing.T) {
assert.NotNil(t, hS)
assert.Equal(t, service.StatusOK, httpStatus)
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("server error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 500)
defer c.Stop()
// Kill the worker
httpSvc := hS.(*rrhttp.Service)
- httpSvc.Server().Workers()[0].Kill()
+ err := httpSvc.Server().Workers()[0].Kill()
+ if err != nil {
+ t.Errorf("error killing the worker: error %v", err)
+ }
// Check health check
_, res, err := get("http://localhost:2116/")
@@ -147,13 +160,21 @@ func TestService_Serve_DeadWorkerStillHealthy(t *testing.T) {
assert.NotNil(t, hS)
assert.Equal(t, service.StatusOK, httpStatus)
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Second * 1)
defer c.Stop()
// Kill one of the workers
httpSvc := hS.(*rrhttp.Service)
- httpSvc.Server().Workers()[0].Kill()
+ err := httpSvc.Server().Workers()[0].Kill()
+ if err != nil {
+ t.Errorf("error killing the worker: error %v", err)
+ }
// Check health check
_, res, err := get("http://localhost:2116/")
@@ -210,7 +231,12 @@ func TestService_Serve_NoServer(t *testing.T) {
assert.NotNil(t, hS)
assert.Equal(t, service.StatusOK, httpStatus)
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 500)
defer c.Stop()
@@ -253,7 +279,12 @@ func TestService_Serve_NoPool(t *testing.T) {
assert.NotNil(t, hS)
assert.Equal(t, service.StatusOK, httpStatus)
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 500)
defer c.Stop()
@@ -271,8 +302,15 @@ func get(url string) (string, *http.Response, error) {
if err != nil {
return "", nil, err
}
- defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return "", nil, err
+ }
+
+ err = r.Body.Close()
+ if err != nil {
+ return "", nil, err
+ }
return string(b), r, err
}