GenerateTokens generates at most requestedTokensCount unique random tokens, none of which clashes with the given allTakenTokens, representing the set of all tokens currently present in the ring. Generated tokens are sorted.
(requestedTokensCount int, allTakenTokens []uint32)
| 39 | // the given allTakenTokens, representing the set of all tokens currently present in the ring. |
| 40 | // Generated tokens are sorted. |
| 41 | func (t *RandomTokenGenerator) GenerateTokens(requestedTokensCount int, allTakenTokens []uint32) Tokens { |
| 42 | if requestedTokensCount <= 0 { |
| 43 | return []uint32{} |
| 44 | } |
| 45 | |
| 46 | used := make(map[uint32]bool, len(allTakenTokens)) |
| 47 | for _, v := range allTakenTokens { |
| 48 | used[v] = true |
| 49 | } |
| 50 | |
| 51 | tokens := make([]uint32, 0, requestedTokensCount) |
| 52 | for i := 0; i < requestedTokensCount; { |
| 53 | t.m.Lock() |
| 54 | candidate := t.r.Uint32() |
| 55 | t.m.Unlock() |
| 56 | |
| 57 | if used[candidate] { |
| 58 | continue |
| 59 | } |
| 60 | used[candidate] = true |
| 61 | tokens = append(tokens, candidate) |
| 62 | i++ |
| 63 | } |
| 64 | |
| 65 | // Ensure returned tokens are sorted. |
| 66 | sort.Slice(tokens, func(i, j int) bool { |
| 67 | return tokens[i] < tokens[j] |
| 68 | }) |
| 69 | |
| 70 | return tokens |
| 71 | } |
| 72 | |
| 73 | func (t *RandomTokenGenerator) CanJoin(_ map[string]InstanceDesc) error { |
| 74 | return nil |
no outgoing calls