File tree 1 file changed +13
-7
lines changed
1 file changed +13
-7
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ func newTopic(id string, last time.Time) *topic {
28
28
}
29
29
}
30
30
31
+ // Subscribe subscribes to this topic
31
32
func (t * topic ) Subscribe (s subscriber ) int {
32
33
t .mu .Lock ()
33
34
defer t .mu .Unlock ()
@@ -37,24 +38,29 @@ func (t *topic) Subscribe(s subscriber) int {
37
38
return subscriberID
38
39
}
39
40
41
+ // Unsubscribe removes the subscription from the list of subscribers
40
42
func (t * topic ) Unsubscribe (id int ) {
41
43
t .mu .Lock ()
42
44
defer t .mu .Unlock ()
43
45
delete (t .subscribers , id )
44
46
}
45
47
48
+ // Publish asynchronously publishes to all subscribers
46
49
func (t * topic ) Publish (m * message ) error {
47
- t .mu .Lock ()
48
- defer t .mu .Unlock ()
49
- t .last = time .Now ()
50
- for _ , s := range t .subscribers {
51
- if err := s (m ); err != nil {
52
- log .Printf ("error publishing message to subscriber" )
50
+ go func () {
51
+ t .mu .Lock ()
52
+ defer t .mu .Unlock ()
53
+ t .last = time .Now ()
54
+ for _ , s := range t .subscribers {
55
+ if err := s (m ); err != nil {
56
+ log .Printf ("error publishing message to subscriber" )
57
+ }
53
58
}
54
- }
59
+ }()
55
60
return nil
56
61
}
57
62
63
+ // Subscribers returns the number of subscribers to this topic
58
64
func (t * topic ) Subscribers () int {
59
65
t .mu .Lock ()
60
66
defer t .mu .Unlock ()
You can’t perform that action at this time.
0 commit comments