|
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,124 @@ 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(ProducerMap)) |
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(ProducerMap)) |
| 71 | + producers := val.(ProducerMap) |
80 | 72 | _, found := producers[p.peerInfo.id] |
81 | 73 | if found == false { |
82 | 74 | producers[p.peerInfo.id] = p |
83 | 75 | } |
| 76 | + |
84 | 77 | return !found |
85 | 78 | } |
86 | 79 |
|
87 | 80 | // remove a producer from a registration |
88 | 81 | func (r *RegistrationDB) RemoveProducer(k Registration, id string) (bool, int) { |
89 | 82 | r.Lock() |
90 | 83 | defer r.Unlock() |
91 | | - producers, ok := r.registrationMap[k] |
| 84 | + value, ok := r.registrationMap.Load(k) |
92 | 85 | if !ok { |
93 | 86 | return false, 0 |
94 | 87 | } |
| 88 | + producers := value.(ProducerMap) |
95 | 89 | removed := false |
96 | 90 | if _, exists := producers[id]; exists { |
97 | 91 | removed = true |
98 | 92 | } |
99 | 93 |
|
100 | 94 | // Note: this leaves keys in the DB even if they have empty lists |
101 | 95 | delete(producers, id) |
| 96 | + |
102 | 97 | return removed, len(producers) |
103 | 98 | } |
104 | 99 |
|
105 | 100 | // remove a Registration and all it's producers |
106 | 101 | func (r *RegistrationDB) RemoveRegistration(k Registration) { |
107 | | - r.Lock() |
108 | | - defer r.Unlock() |
109 | | - delete(r.registrationMap, k) |
| 102 | + r.registrationMap.Delete(k) |
110 | 103 | } |
111 | 104 |
|
112 | 105 | func (r *RegistrationDB) needFilter(key string, subkey string) bool { |
113 | 106 | return key == "*" || subkey == "*" |
114 | 107 | } |
115 | 108 |
|
116 | 109 | func (r *RegistrationDB) FindRegistrations(category string, key string, subkey string) Registrations { |
117 | | - r.RLock() |
118 | | - defer r.RUnlock() |
119 | 110 | if !r.needFilter(key, subkey) { |
120 | 111 | k := Registration{category, key, subkey} |
121 | | - if _, ok := r.registrationMap[k]; ok { |
| 112 | + if _, ok := r.registrationMap.Load(k); ok { |
122 | 113 | return Registrations{k} |
123 | 114 | } |
124 | 115 | return Registrations{} |
125 | 116 | } |
126 | 117 | results := Registrations{} |
127 | | - for k := range r.registrationMap { |
128 | | - if !k.IsMatch(category, key, subkey) { |
129 | | - continue |
| 118 | + r.registrationMap.Range(func(k, _ interface{}) bool { |
| 119 | + if k.(Registration).IsMatch(category, key, subkey) { |
| 120 | + results = append(results, k.(Registration)) |
130 | 121 | } |
131 | | - results = append(results, k) |
132 | | - } |
| 122 | + return true |
| 123 | + }) |
| 124 | + |
133 | 125 | return results |
134 | 126 | } |
135 | 127 |
|
136 | 128 | func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers { |
137 | | - r.RLock() |
138 | | - defer r.RUnlock() |
139 | 129 | if !r.needFilter(key, subkey) { |
140 | 130 | k := Registration{category, key, subkey} |
141 | | - return ProducerMap2Slice(r.registrationMap[k]) |
| 131 | + val, _ := r.registrationMap.Load(k) |
| 132 | + |
| 133 | + r.RLock() |
| 134 | + defer r.RUnlock() |
| 135 | + return ProducerMap2Slice(val.(ProducerMap)) |
142 | 136 | } |
143 | 137 |
|
| 138 | + r.RLock() |
144 | 139 | results := make(map[string]struct{}) |
145 | 140 | 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) |
| 141 | + r.registrationMap.Range(func(k, v interface{}) bool { |
| 142 | + if k.(Registration).IsMatch(category, key, subkey) { |
| 143 | + producers := v.(ProducerMap) |
| 144 | + for _, producer := range producers { |
| 145 | + _, found := results[producer.peerInfo.id] |
| 146 | + if found == false { |
| 147 | + results[producer.peerInfo.id] = struct{}{} |
| 148 | + retProducers = append(retProducers, producer) |
| 149 | + } |
155 | 150 | } |
156 | 151 | } |
157 | | - } |
| 152 | + return true |
| 153 | + }) |
| 154 | + |
| 155 | + r.RUnlock() |
| 156 | + |
158 | 157 | return retProducers |
159 | 158 | } |
160 | 159 |
|
161 | 160 | func (r *RegistrationDB) LookupRegistrations(id string) Registrations { |
162 | 161 | r.RLock() |
163 | | - defer r.RUnlock() |
| 162 | + |
164 | 163 | results := Registrations{} |
165 | | - for k, producers := range r.registrationMap { |
| 164 | + r.registrationMap.Range(func(k, v interface{}) bool { |
| 165 | + producers := v.(ProducerMap) |
166 | 166 | if _, exists := producers[id]; exists { |
167 | | - results = append(results, k) |
| 167 | + results = append(results, k.(Registration)) |
168 | 168 | } |
169 | | - } |
| 169 | + |
| 170 | + return true |
| 171 | + }) |
| 172 | + |
| 173 | + r.RUnlock() |
| 174 | + |
170 | 175 | return results |
171 | 176 | } |
172 | 177 |
|
|
0 commit comments