Make Conn Close idempotent
* die (which is called by Close) now closes underlying connection
This commit is contained in:
@@ -232,7 +232,13 @@ func Connect(config ConnConfig) (c *Conn, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes a connection. It is safe to call Close on a already closed
|
||||||
|
// connection.
|
||||||
func (c *Conn) Close() (err error) {
|
func (c *Conn) Close() (err error) {
|
||||||
|
if !c.IsAlive() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
err = c.txMsg('X', c.getBuf(), true)
|
err = c.txMsg('X', c.getBuf(), true)
|
||||||
c.die(errors.New("Closed"))
|
c.die(errors.New("Closed"))
|
||||||
c.logger.Info("Closed connection")
|
c.logger.Info("Closed connection")
|
||||||
@@ -1190,4 +1196,6 @@ func (c *Conn) getBuf() *bytes.Buffer {
|
|||||||
func (c *Conn) die(err error) {
|
func (c *Conn) die(err error) {
|
||||||
c.alive = false
|
c.alive = false
|
||||||
c.causeOfDeath = err
|
c.causeOfDeath = err
|
||||||
|
c.writer.Flush()
|
||||||
|
c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user