@@ -6,6 +6,7 @@ package pubsub
6
6
import (
7
7
"errors"
8
8
"fmt"
9
+ "runtime"
9
10
"sync"
10
11
"time"
11
12
)
@@ -33,7 +34,10 @@ func (a *Message[T]) Nack(err error) {
33
34
// Control messages for the topic.
34
35
type control [T any ] interface { control () }
35
36
36
- type subscribe [T any ] chan Message [T ]
37
+ type subscribe [T any ] struct {
38
+ subscriber string
39
+ msg chan Message [T ]
40
+ }
37
41
38
42
func (subscribe [T ]) control () {}
39
43
@@ -93,6 +97,11 @@ func (s *Topic[T]) PublishSync(t T) error {
93
97
}
94
98
}
95
99
100
+ func getSubscriber () string {
101
+ pc , file , line , _ := runtime .Caller (2 )
102
+ return fmt .Sprintf ("%s:%d: %s" , file , line , runtime .FuncForPC (pc ).Name ())
103
+ }
104
+
96
105
// Subscribe a channel to the topic.
97
106
//
98
107
// The channel will be closed when the topic is closed.
@@ -111,7 +120,7 @@ func (s *Topic[T]) Subscribe(c chan T) chan T {
111
120
close (c )
112
121
}()
113
122
s .rawChannelMap .Store (c , forward )
114
- s .control <- subscribe [T ]( forward )
123
+ s .control <- subscribe [T ]{ msg : forward , subscriber : getSubscriber ()}
115
124
return c
116
125
}
117
126
@@ -128,7 +137,7 @@ func (s *Topic[T]) SubscribeSync(c chan Message[T]) chan Message[T] {
128
137
if c == nil {
129
138
c = make (chan Message [T ], 16 )
130
139
}
131
- s .control <- subscribe [T ]( c )
140
+ s .control <- subscribe [T ]{ msg : c , subscriber : getSubscriber ()}
132
141
return c
133
142
}
134
143
@@ -155,13 +164,13 @@ func (s *Topic[T]) Close() error {
155
164
}
156
165
157
166
func (s * Topic [T ]) run () {
158
- subscriptions := map [chan Message [T ]]struct {} {}
167
+ subscriptions := map [chan Message [T ]]subscribe [ T ] {}
159
168
for {
160
169
select {
161
170
case msg := <- s .control :
162
171
switch msg := msg .(type ) {
163
172
case subscribe [T ]:
164
- subscriptions [msg ] = struct {}{}
173
+ subscriptions [msg . msg ] = msg
165
174
166
175
case unsubscribe [T ]:
167
176
delete (subscriptions , msg )
@@ -173,7 +182,7 @@ func (s *Topic[T]) run() {
173
182
}
174
183
close (s .control )
175
184
close (s .publish )
176
- s .rawChannelMap .Range (func (k , v interface {} ) bool {
185
+ s .rawChannelMap .Range (func (k , v any ) bool {
177
186
s .rawChannelMap .Delete (k )
178
187
return true
179
188
})
@@ -186,15 +195,15 @@ func (s *Topic[T]) run() {
186
195
187
196
case msg := <- s .publish :
188
197
errs := []error {}
189
- for ch := range subscriptions {
198
+ for ch , sub := range subscriptions {
190
199
smsg := Message [T ]{Msg : msg .Msg , ack : make (chan error , 1 )}
191
200
ch <- smsg
192
201
timer := time .NewTimer (AckTimeout )
193
202
select {
194
203
case err := <- smsg .ack :
195
204
errs = append (errs , err )
196
205
case <- timer .C :
197
- panic ("ack timeout" )
206
+ panic ("ack timeout for " + sub . subscriber )
198
207
}
199
208
timer .Stop ()
200
209
}
0 commit comments