From 44a0fb5a697c64e0c397e82010d38be62c5e7ccb Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 13 Jan 2021 16:34:59 +0300 Subject: [PATCH] [#306] private: Define and use HealthChecker interface to get the status Signed-off-by: Leonard Lyubich --- pkg/services/private/server/healthcheck.go | 3 +-- pkg/services/private/server/server.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/services/private/server/healthcheck.go b/pkg/services/private/server/healthcheck.go index daf41aa55..9d0c266e4 100644 --- a/pkg/services/private/server/healthcheck.go +++ b/pkg/services/private/server/healthcheck.go @@ -23,8 +23,7 @@ func (s *Server) HealthCheck(_ context.Context, req *private.HealthCheckRequest) body := new(private.HealthCheckResponse_Body) resp.SetBody(body) - // FIXME: calculate the status - body.SetStatus(private.HealthStatus_ONLINE) + body.SetStatus(s.healthChecker.HealthStatus()) // sign the response if err := SignMessage(s.key, resp); err != nil { diff --git a/pkg/services/private/server/server.go b/pkg/services/private/server/server.go index 027226722..e6263d0d2 100644 --- a/pkg/services/private/server/server.go +++ b/pkg/services/private/server/server.go @@ -2,6 +2,8 @@ package private import ( "crypto/ecdsa" + + "github.com/nspcc-dev/neofs-node/pkg/services/private" ) // Server is an entity that serves @@ -10,6 +12,16 @@ type Server struct { *cfg } +// HealthChecker is component interface for calculating +// the current health status of a node. +type HealthChecker interface { + // Must calculate and return current node health status. + // + // If status can not be calculated for any reason, + // private.HealthStatus_STATUS_UNDEFINED should be returned. + HealthStatus() private.HealthStatus +} + // Option of the Server's constructor. type Option func(*cfg) @@ -17,6 +29,8 @@ type cfg struct { key *ecdsa.PrivateKey allowedKeys [][]byte + + healthChecker HealthChecker } func defaultCfg() *cfg { @@ -51,3 +65,11 @@ func WithAllowedKeys(keys [][]byte) Option { c.allowedKeys = append(c.allowedKeys, keys...) } } + +// WithHealthChecker returns option to set component +// to calculate node health status. +func WithHealthChecker(hc HealthChecker) Option { + return func(c *cfg) { + c.healthChecker = hc + } +}