func (l *SlowListener) Close() error {
	return l.listener.Close()
}
Close closes the listener.
package slowdown
import (
	"io"
	"net"
	"time"
	"github.com/juju/ratelimit"
)
var MaxRate uint = (1024 * 1024) * 1000
var blockSize = int64(1024)
var capacity = int64(blockSize * 4)
type slowReader struct {
	reader io.Reader
	bucket *ratelimit.Bucket
}
func (sr *slowReader) Read(b []byte) (n int, err error) {
	read := 0
	for read < len(b) {
		sr.bucket.Wait(blockSize)
		upper := int64(read) + blockSize
		if upper > int64(len(b)) {
			upper = int64(len(b))
		}
		slice := b[read:upper]
		n, err := sr.reader.Read(slice)
		read += n
		if err != nil || n < len(slice) {
			return read, err
		}
	}
	return read, nil
}
type slowWriter struct {
	writer io.Writer
	bucket *ratelimit.Bucket
}
func (w *slowWriter) Write(b []byte) (n int, err error) {
	written := 0
	for written < len(b) {
		w.bucket.Wait(blockSize)
		upper := int64(written) + blockSize
		if upper > int64(len(b)) {
			upper = int64(len(b))
		}
		n, err := w.writer.Write(b[written:upper])
		written += n
		if err != nil {
			return written, err
		}
	}
	return written, nil
}
type SlowConn struct {
	conn     net.Conn
	listener *SlowListener
	reader   *slowReader
	writer   *slowWriter
}
func newSlowConn(conn net.Conn, listener *SlowListener) *SlowConn {
	return &SlowConn{
		conn,
		listener,
		&slowReader{conn, listener.readbucket},
		&slowWriter{conn, listener.writebucket},
	}
}
func (sc *SlowConn) Read(b []byte) (n int, err error) {
	return sc.reader.Read(b)
}
func (sc *SlowConn) Write(b []byte) (n int, err error) {
	return sc.writer.Write(b)
}
func (sc *SlowConn) Close() error {
	return sc.conn.Close()
}
func (sc *SlowConn) LocalAddr() net.Addr {
	return sc.conn.LocalAddr()
}
func (sc *SlowConn) RemoteAddr() net.Addr {
	return sc.conn.RemoteAddr()
}
func (sc *SlowConn) SetDeadline(t time.Time) error {
	return sc.conn.SetDeadline(t)
}
func (sc *SlowConn) SetReadDeadline(t time.Time) error {
	return sc.conn.SetReadDeadline(t)
}
func (sc *SlowConn) SetWriteDeadline(t time.Time) error {
	return sc.conn.SetWriteDeadline(t)
}
type SlowListener struct {
	listener    net.Listener
	readbucket  *ratelimit.Bucket
	writebucket *ratelimit.Bucket
}
func NewSlowListener(listener net.Listener, readrate uint, writerate uint) net.Listener {
	if readrate == 0 {
		readrate = MaxRate
	}
	if writerate == 0 {
		writerate = MaxRate
	}
	return &SlowListener{
		listener:    listener,
		readbucket:  ratelimit.NewBucketWithRate(float64(readrate), capacity),
		writebucket: ratelimit.NewBucketWithRate(float64(writerate), capacity),
	}
}
func (l *SlowListener) Accept() (net.Conn, error) {
	conn, err := l.listener.Accept()
	if err != nil {
		return nil, err
	}
	return newSlowConn(conn, l), nil
}