@@ -23,7 +23,6 @@ const (
23
23
)
24
24
25
25
type info struct {
26
- mu sync.Mutex
27
26
state int32
28
27
err error
29
28
}
@@ -35,10 +34,6 @@ type Channel struct {
35
34
messageCount uint64
36
35
midPrefix string
37
36
38
- pubc chan amqp.Confirmation
39
- retc chan amqp.Return
40
- closc chan * amqp.Error
41
-
42
37
sm sync.Map
43
38
44
39
timeout time.Duration
@@ -60,43 +55,36 @@ func SyncChannel(ch *amqp.Channel, err error) (*Channel, error) {
60
55
timeout : 2 * time .Second ,
61
56
}
62
57
63
- c .pubc = ch .NotifyPublish (make (chan amqp.Confirmation ))
64
58
go func () {
65
- for pub := range c .pubc {
59
+ pubc := ch .NotifyPublish (make (chan amqp.Confirmation ))
60
+ for pub := range pubc {
66
61
smkey := "DeliveryTag-" + strconv .Itoa (int (pub .DeliveryTag ))
67
- if iinf , ok := c .sm .Load (smkey ); ok {
68
- inf := iinf .(* info )
69
- swapped := atomic .CompareAndSwapInt32 (& inf .state , int32 (StateUnknown ), int32 (StatePublished ))
70
- if swapped {
71
- inf .mu .Unlock ()
62
+ if v , ok := c .sm .Load (smkey ); ok {
63
+ v .(chan info ) <- info {
64
+ state : int32 (StatePublished ),
72
65
}
73
66
}
74
67
}
75
68
}()
76
69
77
- c .retc = ch .NotifyReturn (make (chan amqp.Return ))
78
70
go func () {
79
- for ret := range c .retc {
71
+ retc := ch .NotifyReturn (make (chan amqp.Return ))
72
+ for ret := range retc {
80
73
smkey := ret .MessageId + ret .CorrelationId
81
- if iinf , ok := c .sm .Load (smkey ); ok {
82
- inf := iinf .(* info )
83
- swapped := atomic .CompareAndSwapInt32 (& inf .state , int32 (StateUnknown ), int32 (StateReturned ))
84
- if swapped {
85
- inf .mu .Unlock ()
74
+ if v , ok := c .sm .Load (smkey ); ok {
75
+ v .(chan info ) <- info {
76
+ state : int32 (StateReturned ),
86
77
}
87
78
}
88
79
}
89
80
}()
90
81
91
- c .closc = ch .NotifyClose (make (chan * amqp.Error ))
92
82
go func () {
93
- err := <- c .closc
94
- c .sm .Range (func (key , value interface {}) bool {
95
- inf := value .(* info )
96
- swapped := atomic .CompareAndSwapInt32 (& inf .state , int32 (StateUnknown ), int32 (StateClosed ))
97
- if swapped {
98
- inf .err = err
99
- inf .mu .Unlock ()
83
+ err := <- ch .NotifyClose (make (chan * amqp.Error ))
84
+ c .sm .Range (func (k , v interface {}) bool {
85
+ v .(chan info ) <- info {
86
+ state : int32 (StateClosed ),
87
+ err : err ,
100
88
}
101
89
return true
102
90
})
@@ -125,17 +113,13 @@ func (c *Channel) PublishAndWait(exchange, key string, mandatory, immediate bool
125
113
msg .CorrelationId = strconv .Itoa (int (mid ))
126
114
}
127
115
128
- inf := & info {
129
- state : int32 (StateUnknown ),
130
- mu : sync.Mutex {},
131
- }
132
- inf .mu .Lock ()
133
-
134
116
mkey := msg .MessageId + msg .CorrelationId
135
117
tkey := "DeliveryTag-" + strconv .Itoa (int (mid ))
136
118
137
- c .sm .Store (mkey , inf )
138
- c .sm .Store (tkey , inf )
119
+ sch := make (chan info )
120
+
121
+ c .sm .Store (mkey , sch )
122
+ c .sm .Store (tkey , sch )
139
123
140
124
defer func () {
141
125
c .sm .Delete (mkey )
@@ -147,20 +131,17 @@ func (c *Channel) PublishAndWait(exchange, key string, mandatory, immediate bool
147
131
return StateUnknown , err
148
132
}
149
133
150
- go func () {
151
- timer := time .NewTimer (c .timeout )
152
- defer timer .Stop ()
153
-
154
- <- timer .C
155
- swapped := atomic .CompareAndSwapInt32 (& inf .state , int32 (StateUnknown ), int32 (StateTimeout ))
156
- if swapped {
157
- inf .err = errors .New ("message publishing timeout reached" )
158
- inf .mu .Unlock ()
159
- }
160
- }()
134
+ timer := time .NewTimer (c .timeout )
135
+ defer timer .Stop ()
161
136
162
- inf .mu .Lock ()
163
- return State (inf .state ), inf .err
137
+ for {
138
+ select {
139
+ case <- timer .C :
140
+ return StateTimeout , errors .New ("message publishing timeout reached" )
141
+ case inf := <- sch :
142
+ return State (inf .state ), inf .err
143
+ }
144
+ }
164
145
}
165
146
166
147
// ConsumeMessages returns chan of wrapped messages from queue
0 commit comments