Browse Source

:gear: Split reading alive/active nodes from API

Ettore Di Giacinto 3 years ago
parent
commit
b543fdf674
1 changed files with 21 additions and 2 deletions
  1. 21 2
      api/client/service/service.go

+ 21 - 2
api/client/service/service.go

@@ -21,6 +21,7 @@ import (
 	"time"
 
 	edgeVPNClient "github.com/mudler/edgevpn/api/client"
+	"github.com/mudler/edgevpn/pkg/protocol"
 )
 
 // Client is a wrapper of an edgeVPN client
@@ -59,8 +60,8 @@ func (c Client) Advertize(uuid string) error {
 	return c.Client.Put(c.serviceID, fmt.Sprintf("%s-uuid", uuid), advertizeMessage{Time: time.Now().UTC()})
 }
 
-// ActiveNodes returns a list of active nodes
-func (c Client) ActiveNodes() (active []string, err error) {
+// AdvertizingNodes returns a list of advertizing nodes
+func (c Client) AdvertizingNodes() (active []string, err error) {
 	uuids, err := c.ListItems(c.serviceID, "uuid")
 	if err != nil {
 		return
@@ -80,6 +81,24 @@ func (c Client) ActiveNodes() (active []string, err error) {
 	return
 }
 
+// ActiveNodes returns a list of active nodes
+func (c Client) ActiveNodes() (active []string, err error) {
+	res, err := c.Client.GetBucket(protocol.HealthCheckKey)
+	if err != nil {
+		return []string{}, err
+	}
+
+	for u, r := range res {
+		var s string
+		r.Unmarshal(&s)
+		parsed, _ := time.Parse(time.RFC3339, s)
+		if parsed.Add(15 * time.Minute).After(time.Now().UTC()) {
+			active = append(active, u)
+		}
+	}
+	return
+}
+
 // Clean cleans up the serviceID associated data
 func (c Client) Clean() error {
 	return c.Client.DeleteBucket(c.serviceID)