From 7a8e80ac0daf4fc39f818c46226df6ed54415cd9 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 20 Jun 2014 14:50:36 -0500 Subject: [PATCH] Make Conn Close idempotent * die (which is called by Close) now closes underlying connection --- conn.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/conn.go b/conn.go index 33eef2f1..7821fc67 100644 --- a/conn.go +++ b/conn.go @@ -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) { + if !c.IsAlive() { + return nil + } + err = c.txMsg('X', c.getBuf(), true) c.die(errors.New("Closed")) c.logger.Info("Closed connection") @@ -1190,4 +1196,6 @@ func (c *Conn) getBuf() *bytes.Buffer { func (c *Conn) die(err error) { c.alive = false c.causeOfDeath = err + c.writer.Flush() + c.conn.Close() }