@@ -11,8 +11,8 @@ import (
11
11
12
12
type Coordinator struct {
13
13
counter int
14
- producers map [ interface {}] interface {}
15
- consumers map [ interface {}] interface {}
14
+ producers * sync. Map
15
+ consumers * sync. Map
16
16
responses map [interface {}]interface {}
17
17
nextItemProducer uint8
18
18
nextItemConsumer uint8
@@ -43,8 +43,8 @@ type Response struct {
43
43
44
44
func NewCoordinator () * Coordinator {
45
45
return & Coordinator {mutex : & sync.Mutex {},
46
- producers : make ( map [ interface {}] interface {}) ,
47
- consumers : make ( map [ interface {}] interface {}) ,
46
+ producers : & sync. Map {} ,
47
+ consumers : & sync. Map {} ,
48
48
responses : make (map [interface {}]interface {})}
49
49
}
50
50
@@ -77,7 +77,7 @@ func (coordinator *Coordinator) NewProducer(
77
77
confirmMutex : & sync.Mutex {},
78
78
onClose : cleanUp ,
79
79
}
80
- coordinator .producers [ lastId ] = producer
80
+ coordinator .producers . Store ( lastId , producer )
81
81
return producer , err
82
82
}
83
83
@@ -89,11 +89,8 @@ func (coordinator *Coordinator) RemoveConsumerById(id interface{}, reason Event)
89
89
return consumer .close (reason )
90
90
91
91
}
92
- func (coordinator * Coordinator ) GetConsumers () map [interface {}]interface {} {
93
- coordinator .mutex .Lock ()
94
- defer coordinator .mutex .Unlock ()
92
+ func (coordinator * Coordinator ) Consumers () * sync.Map {
95
93
return coordinator .consumers
96
-
97
94
}
98
95
99
96
func (coordinator * Coordinator ) RemoveProducerById (id uint8 , reason Event ) error {
@@ -117,7 +114,7 @@ func (coordinator *Coordinator) RemoveResponseById(id interface{}) error {
117
114
}
118
115
119
116
func (coordinator * Coordinator ) ProducersCount () int {
120
- return coordinator .count (coordinator .producers )
117
+ return coordinator .countSyncMap (coordinator .producers )
121
118
}
122
119
123
120
// response
@@ -198,28 +195,25 @@ func (coordinator *Coordinator) NewConsumer(messagesHandler MessagesHandler,
198
195
onClose : cleanUp ,
199
196
}
200
197
201
- coordinator .consumers [lastId ] = item
198
+ coordinator .consumers .Store (lastId , item )
199
+
202
200
return item
203
201
}
204
202
205
203
func (coordinator * Coordinator ) GetConsumerById (id interface {}) (* Consumer , error ) {
206
- v , err := coordinator .getById (id , coordinator .consumers )
207
- if err != nil {
208
- return nil , err
204
+ if consumer , exists := coordinator .consumers .Load (id ); exists {
205
+ return consumer .(* Consumer ), nil
209
206
}
210
- return v .(* Consumer ), err
207
+
208
+ return nil , errors .New ("item #{id} not found " )
211
209
}
212
210
213
211
func (coordinator * Coordinator ) ExtractConsumerById (id interface {}) (* Consumer , error ) {
214
- coordinator .mutex .Lock ()
215
- defer coordinator .mutex .Unlock ()
216
- if coordinator .consumers [id ] == nil {
217
- return nil , errors .New ("item #{id} not found " )
212
+ if consumer , exists := coordinator .consumers .LoadAndDelete (id ); exists {
213
+ return consumer .(* Consumer ), nil
218
214
}
219
- consumer := coordinator .consumers [id ].(* Consumer )
220
- coordinator .consumers [id ] = nil
221
- delete (coordinator .consumers , id )
222
- return consumer , nil
215
+
216
+ return nil , errors .New ("item #{id} not found " )
223
217
}
224
218
225
219
func (coordinator * Coordinator ) GetResponseById (id uint32 ) (* Response , error ) {
@@ -231,31 +225,26 @@ func (coordinator *Coordinator) GetResponseById(id uint32) (*Response, error) {
231
225
}
232
226
233
227
func (coordinator * Coordinator ) ConsumersCount () int {
234
- return coordinator .count (coordinator .consumers )
228
+ return coordinator .countSyncMap (coordinator .consumers )
235
229
}
236
230
237
231
func (coordinator * Coordinator ) GetProducerById (id interface {}) (* Producer , error ) {
238
- v , err := coordinator .getById (id , coordinator .producers )
239
- if err != nil {
240
- return nil , err
232
+ if producer , exists := coordinator .producers .Load (id ); exists {
233
+ return producer .(* Producer ), nil
241
234
}
242
- return v .(* Producer ), err
235
+
236
+ return nil , errors .New ("item #{id} not found " )
243
237
}
244
238
245
239
func (coordinator * Coordinator ) ExtractProducerById (id interface {}) (* Producer , error ) {
246
- coordinator .mutex .Lock ()
247
- defer coordinator .mutex .Unlock ()
248
- if coordinator .producers [id ] == nil {
249
- return nil , errors .New ("item #{id} not found " )
240
+ if producer , exists := coordinator .producers .LoadAndDelete (id ); exists {
241
+ return producer .(* Producer ), nil
250
242
}
251
- producer := coordinator .producers [id ].(* Producer )
252
- coordinator .producers [id ] = nil
253
- delete (coordinator .producers , id )
254
- return producer , nil
243
+
244
+ return nil , errors .New ("item #{id} not found " )
255
245
}
256
246
257
247
// general functions
258
-
259
248
func (coordinator * Coordinator ) getById (id interface {}, refmap map [interface {}]interface {}) (interface {}, error ) {
260
249
coordinator .mutex .Lock ()
261
250
defer coordinator .mutex .Unlock ()
@@ -276,11 +265,16 @@ func (coordinator *Coordinator) removeById(id interface{}, refmap map[interface{
276
265
return nil
277
266
}
278
267
279
- func (coordinator * Coordinator ) count (refmap map [interface {}]interface {}) int {
280
- coordinator .mutex .Lock ()
281
- defer coordinator .mutex .Unlock ()
282
- return len (refmap )
268
+ func (coordinator * Coordinator ) countSyncMap (refmap * sync.Map ) int {
269
+ count := 0
270
+ refmap .Range (func (_ , _ interface {}) bool {
271
+ count ++
272
+ return true
273
+ })
274
+
275
+ return count
283
276
}
277
+
284
278
func (coordinator * Coordinator ) getNextProducerItem () (uint8 , error ) {
285
279
if coordinator .nextItemProducer >= ^ uint8 (0 ) {
286
280
return coordinator .reuseFreeId (coordinator .producers )
@@ -299,11 +293,11 @@ func (coordinator *Coordinator) getNextConsumerItem() (uint8, error) {
299
293
return res , nil
300
294
}
301
295
302
- func (coordinator * Coordinator ) reuseFreeId (refMap map [ interface {}] interface {} ) (byte , error ) {
296
+ func (coordinator * Coordinator ) reuseFreeId (refMap * sync. Map ) (byte , error ) {
303
297
maxValue := int (^ uint8 (0 ))
304
298
var result byte
305
299
for i := 0 ; i < maxValue ; i ++ {
306
- if refMap [ byte (i )] == nil {
300
+ if _ , exists := refMap . Load ( byte (i )); ! exists {
307
301
return byte (i ), nil
308
302
}
309
303
result ++
@@ -314,8 +308,20 @@ func (coordinator *Coordinator) reuseFreeId(refMap map[interface{}]interface{})
314
308
return result , nil
315
309
}
316
310
317
- func (coordinator * Coordinator ) Producers () map [interface {}]interface {} {
318
- coordinator .mutex .Lock ()
319
- defer coordinator .mutex .Unlock ()
311
+ func (coordinator * Coordinator ) Producers () * sync.Map {
320
312
return coordinator .producers
321
313
}
314
+
315
+ func (coordinator * Coordinator ) Close () {
316
+ coordinator .producers .Range (func (_ , producer interface {}) bool {
317
+ _ = producer .(* Producer ).Close ()
318
+
319
+ return true
320
+ })
321
+
322
+ coordinator .consumers .Range (func (_ , consumer interface {}) bool {
323
+ _ = consumer .(* Consumer ).Close ()
324
+
325
+ return true
326
+ })
327
+ }
0 commit comments