MCPcopy
hub / github.com/grafana/dskit / GenerateTokens

Method GenerateTokens

ring/token_generator.go:41–71  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

39// the given allTakenTokens, representing the set of all tokens currently present in the ring.
40// Generated tokens are sorted.
41func (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
73func (t *RandomTokenGenerator) CanJoin(_ map[string]InstanceDesc) error {
74 return nil

Calls

no outgoing calls