Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #50 from safing/fix/hub-deletion
Browse files Browse the repository at this point in the history
Add support for discontinued Hubs, fix Hub deletion
  • Loading branch information
dhaavi authored Jan 25, 2022
2 parents 5fa1fb1 + e70ec27 commit da13887
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
13 changes: 7 additions & 6 deletions hub/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hub

import (
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -114,18 +115,18 @@ func (hub *Hub) Save() error {
// RemoveHubAndMsgs deletes a Hub and it's saved messages from the database.
func RemoveHubAndMsgs(mapName string, hubID string) (err error) {
err = db.Delete(MakeHubDBKey(mapName, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub: %w", err)
if err != nil && !errors.Is(err, database.ErrNotFound) {
return fmt.Errorf("failed to delete main hub entry: %w", err)
}

err = db.Delete(MakeHubMsgDBKey(mapName, MsgTypeAnnouncement, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub announcement: %w", err)
if err != nil && !errors.Is(err, database.ErrNotFound) {
return fmt.Errorf("failed to delete hub announcement data: %w", err)
}

err = db.Delete(MakeHubMsgDBKey(mapName, MsgTypeStatus, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub status: %w", err)
if err != nil && !errors.Is(err, database.ErrNotFound) {
return fmt.Errorf("failed to delete hub status data: %w", err)
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions hub/intel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Intel struct {
BootstrapHubs []string
// TrustedHubs is a list of Hub IDs that are specially designated for more sensitive tasls, such as handling unencrypted traffic.
TrustedHubs []string
// DiscontinuedHubs is a list of Hub IDs that have been discontinued and should be marked as offline and removed.
DiscontinuedHubs []string

// AdviseOnlyTrustedHubs advises to only use trusted Hubs regardless of intended purpose.
AdviseOnlyTrustedHubs bool
Expand Down
9 changes: 9 additions & 0 deletions navigator/intel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (m *Map) updateIntelStatuses(pin *Pin) {
return
}

// Check if Hub is discontinued.
for _, hubID := range m.intel.DiscontinuedHubs {
if pin.Hub.ID == hubID {
pin.State = StateNone
pin.addStates(StateOffline)
return
}
}

// Check if Hub is trusted.
for _, hubID := range m.intel.TrustedHubs {
if pin.Hub.ID == hubID {
Expand Down
41 changes: 32 additions & 9 deletions navigator/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,23 +379,46 @@ func (m *Map) updateHubLane(pin *Pin, lane *hub.Lane, peer *Pin) {
}

func (m *Map) updateStates(ctx context.Context, task *modules.Task) error {
m.RLock()
defer m.RUnlock()

var toDelete []string
now := time.Now()

m.Lock()
defer m.Unlock()

pinLoop:
for _, pin := range m.all {
// Update StateFailing.
if pin.State.has(StateFailing) && now.After(pin.FailingUntil) {
pin.removeStates(StateFailing)
}

// Delete obsolete Hubs.
if pin.State.hasNoneOf(StateActive) && pin.Hub.Obsolete() {
err := hub.RemoveHubAndMsgs(m.Name, pin.Hub.ID)
if err != nil {
log.Warningf("navigator: failed to delete obsolete %s", pin.Hub)
// Check for discontinued Hubs.
if m.intel != nil {
for _, hubID := range m.intel.DiscontinuedHubs {
if pin.Hub.ID == hubID {
toDelete = append(toDelete, pin.Hub.ID)
log.Infof("navigator: deleting discontinued %s", pin.Hub)
continue pinLoop
}
}
m.RemoveHub(pin.Hub.ID)
}
// Check for obsoleted Hubs.
if pin.State.hasNoneOf(StateActive) && pin.Hub.Obsolete() {
toDelete = append(toDelete, pin.Hub.ID)
log.Infof("navigator: deleting obsolete %s", pin.Hub)
}

// Delete hubs async, as deleting triggers a couple hooks that lock the map.
if len(toDelete) > 0 {
module.StartWorker("delete hubs", func(_ context.Context) error {
for _, idToDelete := range toDelete {
err := hub.RemoveHubAndMsgs(m.Name, idToDelete)
if err != nil {
log.Warningf("navigator: failed to delete Hub %s: %s", idToDelete, err)
}
}
return nil
})
}
}

Expand Down

0 comments on commit da13887

Please sign in to comment.