Whether to create connection every time when amqp.Dial is threadsafe or not in go lang

前端 未结 1 1592
天命终不由人
天命终不由人 2020-12-18 07:33

As it is mentioned in the RabbitMQ docs that tcp connections are expensive to make. So, for that concept of channel was introduced. Now i came across this example. In the

相关标签:
1条回答
  • 2020-12-18 08:01

    Of course, you shouldn't create a connection for each request. Make it a global variable or better part of an application context which you initialize once at startup.

    You can handle connection errors by registering a channel using Connection.NotifyClose:

    func initialize() {
      c := make(chan *amqp.Error)
      go func() {
        err := <-c
        log.Println("reconnect: " + err.Error())
        initialize()
      }()
    
      conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
      if err != nil {
        panic("cannot connect")
      }
      conn.NotifyClose(c)
    
      // create topology
    }
    
    0 讨论(0)
提交回复
热议问题