Removes unnecessary returned func from WaitReady
[govpp.git] / core / core.go
index 8977902..ab0c6bf 100644 (file)
@@ -36,9 +36,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.
@@ -105,6 +106,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) {
@@ -164,10 +187,13 @@ func newConnection(vppAdapter adapter.VppAdapter) (*Connection, error) {
                return nil, errors.New("only one connection per process is supported")
        }
 
-       conn = &Connection{vpp: vppAdapter, codec: &MsgCodec{}}
-       conn.channels = make(map[uint32]*api.Channel)
-       conn.msgIDs = make(map[string]uint16)
-       conn.notifSubscriptions = make(map[uint16][]*api.NotifSubscription)
+       conn = &Connection{
+               vpp:                vppAdapter,
+               codec:              &MsgCodec{},
+               channels:           make(map[uint32]*api.Channel),
+               msgIDs:             make(map[string]uint16),
+               notifSubscriptions: make(map[uint16][]*api.NotifSubscription),
+       }
 
        conn.vpp.SetMsgCallback(msgCallback)
        return conn, nil
@@ -207,8 +233,8 @@ func (c *Connection) disconnectVPP() {
 func (c *Connection) connectLoop(connChan chan ConnectionEvent) {
        // loop until connected
        for {
-               err := c.connectVPP()
-               if err == nil {
+               c.vpp.WaitReady()
+               if err := c.connectVPP(); err == nil {
                        // signal connected event
                        connChan <- ConnectionEvent{Timestamp: time.Now(), State: Connected}
                        break
@@ -229,6 +255,7 @@ func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) {
                return
        }
 
+       failedChecks := 0
        // send health check probes until an error occurs
        for {
                // wait for healthCheckProbeInterval
@@ -251,8 +278,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}