Created
April 10, 2015 02:15
-
-
Save cmelbye/9d3c6659e37596e6f675 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type limitConn struct { | |
mu sync.Mutex // only for closing the net.Conn | |
net.Conn | |
} | |
func (lc *limitConn) Read(b []byte) (int, error) { | |
lc.mu.Lock() | |
defer lc.mu.Unlock() | |
if lc.Conn == nil { | |
return 0, nil // Silently ignore closed connection. | |
} | |
return lc.Conn.Read(b) | |
} | |
func (lc *limitConn) Close() error { | |
lc.mu.Lock() | |
defer lc.mu.Unlock() | |
if lc.Conn == nil { | |
// Silently ignore double close. | |
return nil | |
} | |
err := lc.Conn.Close() | |
lc.Conn = nil | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment