Skip to content

Commit

Permalink
remove invalid and inactive networks
Browse files Browse the repository at this point in the history
if networks have no workers delete them from the database, similarly,
if invalid.

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
  • Loading branch information
mudler committed Aug 8, 2024
1 parent 5ee59a8 commit 6650294
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions core/explorer/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (s *DiscoveryServer) NetworkState() *NetworkState {
return s.networkState
}

// NewDiscoveryServer creates a new DiscoveryServer with the given Database.
// it keeps the db state in sync with the network state
func NewDiscoveryServer(db *Database) *DiscoveryServer {
return &DiscoveryServer{
database: db,
Expand All @@ -41,6 +43,11 @@ type Network struct {
}

func (s *DiscoveryServer) runBackground() {
if len(s.database.TokenList()) == 0 {
time.Sleep(5 * time.Second) // avoid busy loop
return
}

for _, token := range s.database.TokenList() {
c, cancel := context.WithTimeout(context.Background(), 50*time.Second)
defer cancel()
Expand All @@ -52,18 +59,21 @@ func (s *DiscoveryServer) runBackground() {
n, err := p2p.NewNode(token)
if err != nil {
fmt.Println(err)
s.database.Delete(token)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
continue
}

err = n.Start(c)
if err != nil {
fmt.Println(err)
s.database.Delete(token)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
continue
}

ledger, err := n.Ledger()
if err != nil {
fmt.Println(err)
s.database.Delete(token)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
continue
}

Expand All @@ -74,16 +84,25 @@ func (s *DiscoveryServer) runBackground() {
// and few attempts would have to be made before bailing out
go s.retrieveNetworkData(c, ledger, networkData)

hasWorkers := false
ledgerK := []ClusterData{}
for key := range networkData {
ledgerK = append(ledgerK, key)
if len(key.Workers) > 0 {
hasWorkers = true
}
}

s.Lock()
s.networkState.Networks[token] = Network{
Clusters: ledgerK,
if hasWorkers {
s.Lock()
s.networkState.Networks[token] = Network{
Clusters: ledgerK,
}
s.Unlock()
} else {
fmt.Println("No workers found in the network, removing token", token)
s.database.Delete(token)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}
s.Unlock()
}
}

Expand Down

0 comments on commit 6650294

Please sign in to comment.