/
member_predicate.go
43 lines (37 loc) · 1.55 KB
/
member_predicate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package swim
// MemberPredicate is a function that tests if a Member satisfies a condition.
// It is advised to use exported functions on Member instead of its exported
// fields in case we want to extract the functionality of Member to an Interface
// in the future. This is likely to happen if we pursue plugable membership.
type MemberPredicate func(member Member) bool
// MemberMatchesPredicates can take multiple predicates and test them against a
// member returning if the member satisfies all the predicates. This means that
// if one test fails it will stop executing and return with false.
func MemberMatchesPredicates(member Member, predicates ...MemberPredicate) bool {
for _, p := range predicates {
if !p(member) {
return false
}
}
return true
}
// memberIsReachable tests if a member is deemed to be reachable. This filters
// out all members that are known to be unresponsive. Most operations will only
// ever be concerned with Members that are in a Reachable state. In SWIM terms a
// member is considered reachable when it is either in Alive status or in
// Suspect status. All other Members are considered to not be reachable.
func memberIsReachable(member Member) bool {
return member.isReachable()
}
// MemberWithLabelAndValue returns a predicate able to test if the value of a
// label on a member is equal to the provided value.
func MemberWithLabelAndValue(key, value string) MemberPredicate {
return func(member Member) bool {
v, ok := member.Labels[key]
if !ok {
return false
}
// test if the values match
return v == value
}
}