Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PubSub documentation example unclear on concurrent use #1271

Open
mgale opened this issue Mar 6, 2020 · 0 comments
Open

PubSub documentation example unclear on concurrent use #1271

mgale opened this issue Mar 6, 2020 · 0 comments

Comments

@mgale
Copy link

@mgale mgale commented Mar 6, 2020

The following example exist in the docs:
https://godoc.org/github.com/go-redis/redis#ex-PubSub

  1. pubsub := rdb.Subscribe("mychannel1")
  2. // Wait for confirmation that subscription is created before publishing anything.
  3. _, err := pubsub.Receive()
  4. if err != nil {
  5. panic(err)
    
  6. }
  7. // Go channel which receives messages.
  8. ch := pubsub.Channel()
  9. // Publish a message.
  10. err = rdb.Publish("mychannel1", "hello").Err()
  11. if err != nil {
  12. panic(err)
    
  13. }
  14. time.AfterFunc(time.Second, func() {
  15. // When pubsub is closed channel is closed too.
    
  16. _ = pubsub.Close()
    
  17. })
  18. // Consume messages.
  19. for msg := range ch {
  20. fmt.Println(msg.Channel, msg.Payload)
    
  21. }

Based on the documentation it seems that:

  • code between lines 1-8 are not safe for concurrent use.
  • code between line 10 is safe for concurrent use.

I am struggling to determine the correct setup for an app that has long lived connections, where each connection could be subscribed to the same topic, for example topicfoobar or each connection has it's own topic.

In my example I can have multiple consumers that all need a copy of the message from the producer.

It is possible to update the documentation or provide an example how to handle multiple consumers for dynamic topics.

@mgale mgale changed the title PubSub document and example unclear on concurrent use PubSub documentation example unclear on concurrent use Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.