为什么不使用RabbitMQ时连接不会保持打开状态?

I have used http://github.com/streadway/amqp package in my application in order to handle connections to a remote RabbitMQ server. Everything is ok and works fine but when a connection is idle for a long period of time f.g 6 hours it gets closed. I check NotifyClose(make(chan *amqp.Error)) all time in my go routine and it returns :

Exception (501) Reason: "write tcp 192.168.133.53:55424->192.168.134.34:5672: write: broken pipe"

  1. Why this error happens? (is there any problem in my code?)

  2. How long a connection can be idle?

  3. How to prevent this problem?

As Cosmic Ossifrage says, the error is saying your RabbitMQ client has disconnected.

There are so many things that could sit between your client and server that can/will drop dormant connections that it's not worth focusing on how long your connection can be dormant for. You want to set the requested heartbeat interval in your connection manager.

https://www.rabbitmq.com/heartbeats.html

I'm not familiar with the framework you're using but I see it has a defaultHeartbeat field in connection.go. You might need to experiment with the value to find the best balance is to stop the connection being killed but not hit the server too often with keep-alive traffic.