Make publishing asynchronous
parent
cc752cf797
commit
c0ee174b13
|
@ -28,6 +28,7 @@ func newTopic(id string, last time.Time) *topic {
|
|||
}
|
||||
}
|
||||
|
||||
// Subscribe subscribes to this topic
|
||||
func (t *topic) Subscribe(s subscriber) int {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
@ -37,13 +38,16 @@ func (t *topic) Subscribe(s subscriber) int {
|
|||
return subscriberID
|
||||
}
|
||||
|
||||
// Unsubscribe removes the subscription from the list of subscribers
|
||||
func (t *topic) Unsubscribe(id int) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
delete(t.subscribers, id)
|
||||
}
|
||||
|
||||
// Publish asynchronously publishes to all subscribers
|
||||
func (t *topic) Publish(m *message) error {
|
||||
go func() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.last = time.Now()
|
||||
|
@ -52,9 +56,11 @@ func (t *topic) Publish(m *message) error {
|
|||
log.Printf("error publishing message to subscriber")
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Subscribers returns the number of subscribers to this topic
|
||||
func (t *topic) Subscribers() int {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
|
Loading…
Reference in New Issue