Skip to content

Documentation for Ringcandidateselector Module

RINGCandidateSelector

Bases: CandidateSelector

Source code in nebula/core/situationalawareness/discovery/candidateselection/ringcandidateselector.py
 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
44
45
46
47
48
49
50
51
52
class RINGCandidateSelector(CandidateSelector):
    def __init__(self):
        self._candidates = []
        self._rejected_candidates = []
        self.candidates_lock = Locker(name="candidates_lock")

    async def set_config(self, config):
        pass

    async def add_candidate(self, candidate):
        """
        To avoid topology problems select 1st candidate found
        """
        self.candidates_lock.acquire()
        self._candidates.append(candidate)
        self.candidates_lock.release()

    async def select_candidates(self):
        self.candidates_lock.acquire()
        cdts = []

        if self._candidates:
            min_neighbors = min(self._candidates, key=lambda x: x[1])[1]
            tied_candidates = [c for c in self._candidates if c[1] == min_neighbors]

            selected = random.choice(tied_candidates)
            cdts.append(selected)

        for cdt in self._candidates:
            if cdt not in cdts:
                self._rejected_candidates.append(cdt)

        not_cdts = self._rejected_candidates.copy()
        self.candidates_lock.release()
        return (cdts, not_cdts)

    async def remove_candidates(self):
        self.candidates_lock.acquire()
        self._candidates = []
        self.candidates_lock.release()

    async def any_candidate(self):
        self.candidates_lock.acquire()
        any = True if len(self._candidates) > 0 else False
        self.candidates_lock.release()
        return any

add_candidate(candidate) async

To avoid topology problems select 1st candidate found

Source code in nebula/core/situationalawareness/discovery/candidateselection/ringcandidateselector.py
16
17
18
19
20
21
22
async def add_candidate(self, candidate):
    """
    To avoid topology problems select 1st candidate found
    """
    self.candidates_lock.acquire()
    self._candidates.append(candidate)
    self.candidates_lock.release()