|
9 | 9 |
|
10 | 10 | type RegistrationDB struct { |
11 | 11 | sync.RWMutex |
12 | | - registrationMap map[Registration]ProducerMap |
| 12 | + registrationMap *sync.Map |
13 | 13 | } |
14 | 14 |
|
15 | 15 | type Registration struct { |
@@ -54,119 +54,116 @@ func (p *Producer) IsTombstoned(lifetime time.Duration) bool { |
54 | 54 |
|
55 | 55 | func NewRegistrationDB() *RegistrationDB { |
56 | 56 | return &RegistrationDB{ |
57 | | - registrationMap: make(map[Registration]ProducerMap), |
| 57 | + registrationMap: &sync.Map{}, |
58 | 58 | } |
59 | 59 | } |
60 | 60 |
|
61 | 61 | // add a registration key |
62 | 62 | func (r *RegistrationDB) AddRegistration(k Registration) { |
63 | | - r.Lock() |
64 | | - defer r.Unlock() |
65 | | - _, ok := r.registrationMap[k] |
66 | | - if !ok { |
67 | | - r.registrationMap[k] = make(map[string]*Producer) |
68 | | - } |
| 63 | + r.registrationMap.LoadOrStore(k, make(map[string]*Producer)) |
69 | 64 | } |
70 | 65 |
|
71 | 66 | // add a producer to a registration |
72 | 67 | func (r *RegistrationDB) AddProducer(k Registration, p *Producer) bool { |
73 | 68 | r.Lock() |
74 | 69 | defer r.Unlock() |
75 | | - _, ok := r.registrationMap[k] |
76 | | - if !ok { |
77 | | - r.registrationMap[k] = make(map[string]*Producer) |
78 | | - } |
79 | | - producers := r.registrationMap[k] |
| 70 | + val, _ := r.registrationMap.LoadOrStore(k, make(map[string]*Producer)) |
| 71 | + producers := val.(map[string]*Producer) |
80 | 72 | _, found := producers[p.peerInfo.id] |
81 | 73 | if found == false { |
82 | 74 | producers[p.peerInfo.id] = p |
83 | 75 | } |
| 76 | + |
| 77 | + r.registrationMap.Store(k, producers) |
84 | 78 | return !found |
85 | 79 | } |
86 | 80 |
|
87 | 81 | // remove a producer from a registration |
88 | 82 | func (r *RegistrationDB) RemoveProducer(k Registration, id string) (bool, int) { |
89 | 83 | r.Lock() |
90 | 84 | defer r.Unlock() |
91 | | - producers, ok := r.registrationMap[k] |
| 85 | + value, ok := r.registrationMap.Load(k) |
92 | 86 | if !ok { |
93 | 87 | return false, 0 |
94 | 88 | } |
| 89 | + producers := value.(map[string]*Producer) |
95 | 90 | removed := false |
96 | 91 | if _, exists := producers[id]; exists { |
97 | 92 | removed = true |
98 | 93 | } |
99 | 94 |
|
100 | 95 | // Note: this leaves keys in the DB even if they have empty lists |
101 | 96 | delete(producers, id) |
| 97 | + |
| 98 | + r.registrationMap.Store(k, producers) |
| 99 | + |
102 | 100 | return removed, len(producers) |
103 | 101 | } |
104 | 102 |
|
105 | 103 | // remove a Registration and all it's producers |
106 | 104 | func (r *RegistrationDB) RemoveRegistration(k Registration) { |
107 | | - r.Lock() |
108 | | - defer r.Unlock() |
109 | | - delete(r.registrationMap, k) |
| 105 | + r.registrationMap.Delete(k) |
110 | 106 | } |
111 | 107 |
|
112 | 108 | func (r *RegistrationDB) needFilter(key string, subkey string) bool { |
113 | 109 | return key == "*" || subkey == "*" |
114 | 110 | } |
115 | 111 |
|
116 | 112 | func (r *RegistrationDB) FindRegistrations(category string, key string, subkey string) Registrations { |
117 | | - r.RLock() |
118 | | - defer r.RUnlock() |
119 | 113 | if !r.needFilter(key, subkey) { |
120 | 114 | k := Registration{category, key, subkey} |
121 | | - if _, ok := r.registrationMap[k]; ok { |
| 115 | + if _, ok := r.registrationMap.Load(k); ok { |
122 | 116 | return Registrations{k} |
123 | 117 | } |
124 | 118 | return Registrations{} |
125 | 119 | } |
126 | 120 | results := Registrations{} |
127 | | - for k := range r.registrationMap { |
128 | | - if !k.IsMatch(category, key, subkey) { |
129 | | - continue |
| 121 | + r.registrationMap.Range(func(k, _ interface{}) bool { |
| 122 | + if k.(Registration).IsMatch(category, key, subkey) { |
| 123 | + results = append(results, k.(Registration)) |
130 | 124 | } |
131 | | - results = append(results, k) |
132 | | - } |
| 125 | + return true |
| 126 | + }) |
| 127 | + |
133 | 128 | return results |
134 | 129 | } |
135 | 130 |
|
136 | 131 | func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers { |
137 | | - r.RLock() |
138 | | - defer r.RUnlock() |
139 | 132 | if !r.needFilter(key, subkey) { |
140 | 133 | k := Registration{category, key, subkey} |
141 | | - return ProducerMap2Slice(r.registrationMap[k]) |
| 134 | + val, _ := r.registrationMap.Load(k) |
| 135 | + return ProducerMap2Slice(val.(map[string]*Producer)) |
142 | 136 | } |
143 | 137 |
|
144 | 138 | results := make(map[string]struct{}) |
145 | 139 | var retProducers Producers |
146 | | - for k, producers := range r.registrationMap { |
147 | | - if !k.IsMatch(category, key, subkey) { |
148 | | - continue |
149 | | - } |
150 | | - for _, producer := range producers { |
151 | | - _, found := results[producer.peerInfo.id] |
152 | | - if found == false { |
153 | | - results[producer.peerInfo.id] = struct{}{} |
154 | | - retProducers = append(retProducers, producer) |
| 140 | + r.registrationMap.Range(func(k, v interface{}) bool { |
| 141 | + if k.(Registration).IsMatch(category, key, subkey) { |
| 142 | + producers := v.(map[string]*Producer) |
| 143 | + for _, producer := range producers { |
| 144 | + _, found := results[producer.peerInfo.id] |
| 145 | + if found == false { |
| 146 | + results[producer.peerInfo.id] = struct{}{} |
| 147 | + retProducers = append(retProducers, producer) |
| 148 | + } |
155 | 149 | } |
156 | 150 | } |
157 | | - } |
| 151 | + return true |
| 152 | + }) |
| 153 | + |
158 | 154 | return retProducers |
159 | 155 | } |
160 | 156 |
|
161 | 157 | func (r *RegistrationDB) LookupRegistrations(id string) Registrations { |
162 | | - r.RLock() |
163 | | - defer r.RUnlock() |
164 | 158 | results := Registrations{} |
165 | | - for k, producers := range r.registrationMap { |
| 159 | + r.registrationMap.Range(func(k, v interface{}) bool { |
| 160 | + producers := v.(map[string]*Producer) |
166 | 161 | if _, exists := producers[id]; exists { |
167 | | - results = append(results, k) |
| 162 | + results = append(results, k.(Registration)) |
168 | 163 | } |
169 | | - } |
| 164 | + |
| 165 | + return true |
| 166 | + }) |
170 | 167 | return results |
171 | 168 | } |
172 | 169 |
|
|
0 commit comments