From 03bc1c23595e998e8fa75a65e23bedfbec115643 Mon Sep 17 00:00:00 2001 From: Lukas Macko Date: Tue, 12 Sep 2017 10:28:40 +0200 Subject: Make healthCheck parameters configurable Change-Id: Idfb6945e13522867ced96a1ed7db85e725f42d1e Signed-off-by: Lukas Macko --- core/core.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/core/core.go b/core/core.go index 3eea332..a045e60 100644 --- a/core/core.go +++ b/core/core.go @@ -37,9 +37,10 @@ const ( notificationChannelBufSize = 100 // default size of the notification channel buffers ) -const ( +var ( healthCheckProbeInterval = time.Second * 1 // default health check probe interval healthCheckReplyTimeout = time.Millisecond * 100 // timeout for reply to a health check probe + healthCheckThreshold = 1 // number of failed healthProbe until the error is reported ) // ConnectionState holds the current state of the connection to VPP. @@ -115,6 +116,28 @@ func SetLogger(l *logger.Logger) { log = l } +// SetHealthCheckProbeInterval sets health check probe interval. +// Beware: Function is not thread-safe. It is recommended to setup this parameter +// before connecting to vpp. +func SetHealthCheckProbeInterval(interval time.Duration) { + healthCheckProbeInterval = interval +} + +// SetHealthCheckReplyTimeout sets timeout for reply to a health check probe. +// If reply arrives after the timeout, check is considered as failed. +// Beware: Function is not thread-safe. It is recommended to setup this parameter +// before connecting to vpp. +func SetHealthCheckReplyTimeout(timeout time.Duration) { + healthCheckReplyTimeout = timeout +} + +// SetHealthCheckThreshold sets the number of failed healthProbe checks until the error is reported. +// Beware: Function is not thread-safe. It is recommended to setup this parameter +// before connecting to vpp. +func SetHealthCheckThreshold(threshold int) { + healthCheckThreshold = threshold +} + // Connect connects to VPP using specified VPP adapter and returns the connection handle. // This call blocks until VPP is connected, or an error occurs. Only one connection attempt will be performed. func Connect(vppAdapter adapter.VppAdapter) (*Connection, error) { @@ -276,6 +299,7 @@ func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) { return } + failedChecks := 0 // send health check probes until an error occurs for { // wait for healthCheckProbeInterval @@ -298,8 +322,14 @@ func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) { err = errors.New("probe reply not received within the timeout period") } - // in case of error, break & disconnect if err != nil { + failedChecks++ + } else { + failedChecks = 0 + } + + if failedChecks >= healthCheckThreshold { + // in case of error, break & disconnect log.Errorf("VPP health check failed: %v", err) // signal disconnected event via channel connChan <- ConnectionEvent{Timestamp: time.Now(), State: Disconnected} -- cgit 1.2.3-korg