func (l *Listener) Start() error {
	var err error
	l.listener, err = net.Listen("tcp", l.Addr)
	if err != nil {
		return err
	}
	l.isRunning.Set(true)
	for {
		if l.isRunning.Get() {
			l.wg.Add(1)
			conn, err := l.listener.Accept()
			if err != nil {
				l.wg.Done()
				glog.Error("error accepting connection ", err)
				continue
			}
			l.metrics.acceptCounter.Mark(1)
			go l.readLoop(conn)
		}
	}
}
Start your listener, not thread safe, blocking.
package mongoproxy
import (
	"net"
	"sync"
	"time"
	"github.com/golang/glog"
)
type Listener struct {
	Addr string
	Timeout time.Duration
	listener   net.Listener
	dispatcher *Dispatcher
	wg         sync.WaitGroup
	isRunning  atomicBool
	metrics    *listenerMetrics
}
type atomicBool struct {
	value bool
	mutex sync.RWMutex
}
func (a *atomicBool) Get() bool {
	a.mutex.RLock()
	defer a.mutex.RUnlock()
	return a.value
}
func (a *atomicBool) Set(b bool) {
	a.mutex.Lock()
	defer a.mutex.Unlock()
	a.value = b
}
func (l *Listener) Stop() {
	l.isRunning.Set(false)
	if err := l.listener.Close(); err != nil {
		glog.Error("error closing listener", err)
	}
	block := func() (interface{}, error) {
		l.wg.Wait()
		return nil, nil
	}
	TimeoutIn(block, l.Timeout)
}