What's an idiomatic way to reconnect to Redis subscription in Go?

huangapple go评论88阅读模式
英文:

What's an idiomatic way to reconnect to Redis subscription in Go?

问题

我正在使用redigo库尝试订阅Redis频道并处理发布的消息。
当出现错误时,我该如何处理?
以下是我想到的方法。这是一个好的方式吗?有更好的方法吗?

注意:这个问题是针对redigo的,但我认为它适用于其他需要重新连接的地方。

package main

import (
    "fmt"
    "time"

    "github.com/garyburd/redigo/redis"
)

func main() {
    for {
        fmt.Println("connecting...")
        c, err := redis.Dial("tcp", "localhost:6379")
        if err != nil {
            fmt.Println("error connecting to redis")
            time.Sleep(5 * time.Second)
            continue
        }
        psc := redis.PubSubConn{c}
        psc.Subscribe("example")
    ReceiveLoop:
        for {
            switch v := psc.Receive().(type) {
            case redis.Message:
                fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
            case redis.Subscription:
                fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
            case error:
                fmt.Println("there was an error")
                fmt.Println(v)
                time.Sleep(5 * time.Second)
                break ReceiveLoop
            }
        }
    }
}

我只是将它放在了main()函数中作为示例。实际上,它应该在某个goroutine中运行。

英文:

I am using the redigo library to try to subscribe to a Redis channel and then handle a published message.
How do I handle the case where it errors out?
Here's what I came up with. Is this a good way to do it? Is there a better way?

Note: this question is specific to redigo, but I think it applies to other places where reconnections need to happen.

package main

import (
    "fmt"
    "time"

    "github.com/garyburd/redigo/redis"
)

func main() {
    for {
        fmt.Println("connecting...")
        c, err := redis.Dial("tcp", "localhost:6379")
        if err != nil {
            fmt.Println("error connecting to redis")
            time.Sleep(5 * time.Second)
            continue
        }
        psc := redis.PubSubConn{c}
        psc.Subscribe("example")
    ReceiveLoop:
        for {
            switch v := psc.Receive().(type) {
            case redis.Message:
                fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
            case redis.Subscription:
                fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
            case error:
                fmt.Println("there was an error")
                fmt.Println(v)
                time.Sleep(5 * time.Second)
                break ReceiveLoop
            }
        }
    }
}

I just put it in the main() function for the example. It would really run in some goroutine.

答案1

得分: 5

是的,使用标签和循环是重新连接的标准做法。

你唯一遗漏的是关闭连接。

            psc.Close()
            break ReceiveLoop

为了更加健壮,你可能想要使用redis.DialTimeout,这样Dial调用就不会无限期地挂起。

英文:

Yes, using a label and a loop is standard practice for reconnecting.

The only thing you're missing is closing the connection.

            psc.Close()
            break ReceiveLoop

For a little more resilience, you may want redis.DialTimeout so that a Dial call can't hang indefinitely.

huangapple
  • 本文由 发表于 2015年5月1日 21:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/29987793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定