added failsafe

This commit is contained in:
ston1th 2020-06-02 00:29:10 +02:00
commit 6d4e4c31f3

16
node.go
View file

@ -8,7 +8,11 @@ import (
"fmt" "fmt"
) )
var ErrServerNotFound = errors.New("server not found") var (
ErrServerNotFound = errors.New("server not found")
ErrNodesOffline = errors.New("one or more nodes are offline")
ErrNodesNotSearched = errors.New("one or more nodes could not be searched")
)
type NodeStatus string type NodeStatus string
@ -59,12 +63,16 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
if err != nil { if err != nil {
return return
} }
nodeOffline := false
nodeNotSearched := false
for _, node := range nl { for _, node := range nl {
if node.Status != NodeStatusOnline { if node.Status != NodeStatusOnline {
nodeOffline = true
continue continue
} }
sl, err := c.ListServers(ctx, node) sl, err := c.ListServers(ctx, node)
if err != nil { if err != nil {
nodeNotSearched = true
continue continue
} }
for _, s := range sl { for _, s := range sl {
@ -76,6 +84,12 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
} }
} }
} }
if nodeOffline {
return nil, ErrNodesOffline
}
if nodeNotSearched {
return nil, ErrNodesNotSearched
}
return nil, ErrServerNotFound return nil, ErrServerNotFound
} }