20d03bebdb
This commit doesn't change any behavior, just organize the code in different way to make it easier to implement different Criterias to include nodes. Today we use NodeID and Subnet based selection but later Criteria can be extended with different kind of placement rules (like geofencing). The change nodeselection is used by segment allocaton (upload) and repair and excludes nodes from an in-memory selection. Resolves https://github.com/storj/storj/issues/4240 Change-Id: I0c1955fe16a045e3b76d7e50b2e1f4575a7ff095
37 lines
874 B
Go
37 lines
874 B
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information
|
|
|
|
package uploadselection
|
|
|
|
import "storj.io/common/storj"
|
|
|
|
// Criteria to filter nodes.
|
|
type Criteria struct {
|
|
ExcludeNodeIDs []storj.NodeID
|
|
AutoExcludeSubnets map[string]struct{} // initialize it with empty map to keep only one node per subnet.
|
|
}
|
|
|
|
// MatchInclude returns with true if node is selected.
|
|
func (c *Criteria) MatchInclude(node *Node) bool {
|
|
if ContainsID(c.ExcludeNodeIDs, node.ID) {
|
|
return false
|
|
}
|
|
if c.AutoExcludeSubnets != nil {
|
|
if _, excluded := c.AutoExcludeSubnets[node.LastNet]; excluded {
|
|
return false
|
|
}
|
|
c.AutoExcludeSubnets[node.LastNet] = struct{}{}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ContainsID returns whether ids contain id.
|
|
func ContainsID(ids []storj.NodeID, id storj.NodeID) bool {
|
|
for _, k := range ids {
|
|
if k == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|