[#306] private: Define and use HealthChecker interface to get the status

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
remotes/KirillovDenis/release/v0.21.1
Leonard Lyubich 2021-01-13 16:34:59 +03:00 committed by Alex Vanin
parent e8bd2eac0d
commit 44a0fb5a69
2 changed files with 23 additions and 2 deletions

View File

@ -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 {

View File

@ -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
}
}