From f29024ce37b7d40eb5f7d29c75512b573a3fee8d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 11 Feb 2013 20:33:37 -0800 Subject: [PATCH] throttle: fix crash I saw hitting reload Change-Id: I2df79e7a2f37c44c2a8bf4f8a0968caf058ec624 --- pkg/throttle/throttle.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/throttle/throttle.go b/pkg/throttle/throttle.go index b7e0b2949..6c28cb2ec 100644 --- a/pkg/throttle/throttle.go +++ b/pkg/throttle/throttle.go @@ -19,6 +19,7 @@ package throttle import ( "fmt" "net" + "sync" "time" ) @@ -34,7 +35,7 @@ func (r Rate) byteTime(n int) time.Duration { if r.KBps == 0 { return 0 } - return time.Duration(float64(n) / 1024 / float64(r.KBps)) * time.Second + return time.Duration(float64(n)/1024/float64(r.KBps)) * time.Second } type Listener struct { @@ -69,7 +70,9 @@ type conn struct { net.Conn Down, Up Rate - wchan chan writeReq + wchan chan writeReq + closeOnce sync.Once + closeErr error } func (c *conn) start() { @@ -97,9 +100,12 @@ func (c *conn) writeLoop() { } func (c *conn) Close() error { - err := c.Conn.Close() - close(c.wchan) - return err + c.closeOnce.Do(func() { + err := c.Conn.Close() + close(c.wchan) + c.closeErr = err + }) + return c.closeErr } func (c *conn) Write(p []byte) (n int, err error) {