2021-10-25 13:27:59 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package uploadselection
|
|
|
|
|
2021-10-29 13:29:14 +01:00
|
|
|
import (
|
|
|
|
"storj.io/common/storj"
|
2022-02-25 16:53:24 +00:00
|
|
|
"storj.io/common/storj/location"
|
2021-10-29 13:29:14 +01:00
|
|
|
)
|
2021-10-25 13:27:59 +01:00
|
|
|
|
|
|
|
// Criteria to filter nodes.
|
|
|
|
type Criteria struct {
|
2022-02-25 16:53:24 +00:00
|
|
|
ExcludeNodeIDs []storj.NodeID
|
|
|
|
AutoExcludeSubnets map[string]struct{} // initialize it with empty map to keep only one node per subnet.
|
|
|
|
Placement storj.PlacementConstraint
|
|
|
|
ExcludedCountryCodes []location.CountryCode
|
2021-10-25 13:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MatchInclude returns with true if node is selected.
|
|
|
|
func (c *Criteria) MatchInclude(node *Node) bool {
|
|
|
|
if ContainsID(c.ExcludeNodeIDs, node.ID) {
|
|
|
|
return false
|
|
|
|
}
|
2021-10-29 13:29:14 +01:00
|
|
|
|
|
|
|
if !c.Placement.AllowedCountry(node.CountryCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:27:59 +01:00
|
|
|
if c.AutoExcludeSubnets != nil {
|
|
|
|
if _, excluded := c.AutoExcludeSubnets[node.LastNet]; excluded {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.AutoExcludeSubnets[node.LastNet] = struct{}{}
|
|
|
|
}
|
2022-02-25 16:53:24 +00:00
|
|
|
|
|
|
|
for _, code := range c.ExcludedCountryCodes {
|
|
|
|
if node.CountryCode == code {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:27:59 +01:00
|
|
|
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
|
|
|
|
}
|