Make healthCheck parameters configurable 93/8393/1
authorLukas Macko <lmacko@cisco.com>
Tue, 12 Sep 2017 08:28:40 +0000 (10:28 +0200)
committerLukas Macko <lmacko@cisco.com>
Tue, 12 Sep 2017 08:49:30 +0000 (10:49 +0200)
Change-Id: Idfb6945e13522867ced96a1ed7db85e725f42d1e
Signed-off-by: Lukas Macko <lmacko@cisco.com>
core/core.go

index 3eea332..a045e60 100644 (file)
@@ -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}