@@ -22,7 +22,6 @@ type ConsumerImpl struct {
22
22
23
23
mu sync.Mutex
24
24
sf singleflight.Group
25
- consumerMap map [string ][]chan mq.Message
26
25
consumerChan map [string ]chan mq.Message
27
26
pool routinepool.Pool
28
27
}
@@ -33,7 +32,6 @@ func NewConsumer(c *config.Config, logger log.Logger) (mq.Consumer, error) {
33
32
c : c ,
34
33
logger : logger ,
35
34
consumerChan : make (map [string ]chan mq.Message ),
36
- consumerMap : make (map [string ][]chan mq.Message ),
37
35
pool : routinepool .NewPool ("[diskq][Consumer]" , 4 , routinepool .NewConfig ()),
38
36
}, nil
39
37
}
@@ -42,40 +40,33 @@ func (x *ConsumerImpl) Subscribe(ctx context.Context, topic string, channel stri
42
40
x .mu .Lock ()
43
41
defer x .mu .Unlock ()
44
42
45
- uniKey := x .generateKey (topic , channel )
46
- if ch , ok := x .consumerChan [uniKey ]; ok {
43
+ if ch , ok := x .consumerChan [topic ]; ok {
47
44
return ch , nil
48
45
}
49
46
50
- _ , _ , _ = x .sf .Do (topic , func () (interface {}, error ) {
51
- if consumer , ok := x .consumerMap [topic ]; ok {
52
- return consumer , nil
47
+ ch , _ , _ : = x .sf .Do (topic , func () (interface {}, error ) {
48
+ if ch , ok := x .consumerChan [topic ]; ok {
49
+ return ch , nil
53
50
}
54
51
queue := gDiskQueueManager .NewDiskQueue (topic , x .c .DataPath , x .c .MaxBytesPerFile , x .c .MinMsgSize , x .c .MaxMsgSize , x .c .SyncEvery , time .Duration (x .c .SyncTimeout )* time .Millisecond , x .logger )
52
+ ch := make (chan mq.Message , 1 )
55
53
x .pool .Go (func (ctx context.Context ) {
56
54
for {
57
55
select {
58
56
case body := <- queue .ReadChan ():
59
57
msg , err := mq .NewMessageFromByte (body )
60
58
if err != nil {
61
- x .logger .Log (log .LevelError , "module" , "NewMessageFromByte" , "err" , err , "body" , string (body ))
62
- }
63
- for _ , cch := range x .consumerMap [topic ] {
64
- select {
65
- case cch <- msg :
66
- default :
67
- }
59
+ _ = x .logger .Log (log .LevelError , "module" , "NewMessageFromByte" , "err" , err , "body" , string (body ))
68
60
}
61
+ ch <- msg
69
62
}
70
63
}
71
64
})
72
- return queue , nil
65
+ return ch , nil
73
66
})
74
67
75
- ch := make (chan mq.Message , 1024 )
76
- x .consumerMap [topic ] = append (x .consumerMap [topic ], ch )
77
- x .consumerChan [uniKey ] = ch
78
- return ch , nil
68
+ x .consumerChan [topic ] = ch .(chan mq.Message )
69
+ return x .consumerChan [topic ], nil
79
70
}
80
71
81
72
func (x * ConsumerImpl ) Close (ctx context.Context ) error {
0 commit comments