Skip to content

Instantly share code, notes, and snippets.

@cmelbye
Created April 10, 2015 02:15
Show Gist options
  • Save cmelbye/9d3c6659e37596e6f675 to your computer and use it in GitHub Desktop.
Save cmelbye/9d3c6659e37596e6f675 to your computer and use it in GitHub Desktop.
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