简体   繁体   中英

Golang Redis close vs unsubscribe from channel

pubsub := rdb.Subscribe(ctx, "mychannel1")

// Close the subscription when we are done.
defer pubsub.Close()
// vs unsubscribe from a channel
defer pubsub.Unsubscribe(ctx, "mychannel1")

ch := pubsub.Channel()

for msg := range ch {
    fmt.Println(msg.Channel, msg.Payload)
}

If I don't wan't redis pub-sub channel anymore. Which is the recommended way to unsubscribe a receiver/subscription from a channel and why? Do I also need to delete the redis pub-sub channel at the end too?

Close and Unsubscribe are two behaviors.

we should close when service stop, just like close.net conn.

Unsubscribe means we no longer subscribe to something even if the service start.

you don't need to delete the channel at the end, GC will ecycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM