good morning!!!!

Skip to content
Snippets Groups Projects
listener.go 555 B
Newer Older
a's avatar
a committed
package jrpctest

import (
	"math/rand"
	"net"
	"time"
)

// flakeyListener kills accepted connections after a random timeout.
type FlakeyListener struct {
	net.Listener
	maxKillTimeout time.Duration
	maxAcceptDelay time.Duration
}

func (l *FlakeyListener) Accept() (net.Conn, error) {
	delay := time.Duration(rand.Int63n(int64(l.maxAcceptDelay)))
	time.Sleep(delay)

	c, err := l.Listener.Accept()
	if err == nil {
		timeout := time.Duration(rand.Int63n(int64(l.maxKillTimeout)))
		time.AfterFunc(timeout, func() {
			c.Close()
		})
	}
	return c, err
}