<commit_before>package luddite

import (
	"crypto/tls"
	"net"
	"os"
	"os/signal"
	"syscall"
	"time"
)

// Based on http://www.hydrogen18.com/blog/stop-listening-http-server-go.html,
// but stops on SIGINT instead of explicit Stop() call

type ListenerStoppedError struct {
}

func (e *ListenerStoppedError) Error() string {
	return "Listener stopped"
}

type StoppableTCPListener struct {
	*net.TCPListener
	stop       chan os.Signal
	keepalives bool
}

func (sl *StoppableTCPListener) Accept() (net.Conn, error) {

	for {
		//Wait up to one second for a new connection
		sl.TCPListener.SetDeadline(time.Now().Add(time.Second))
		newConn, err := sl.TCPListener.AcceptTCP()

		//Check for the channel being closed
		select {
		case <-sl.stop:
			return nil, &ListenerStoppedError{}
		default:
			//If nothing came in on the channel, continue as normal
		}

		if err != nil {
			netErr, ok := err.(net.Error)

			//If this is a timeout, then continue to wait for
			//new connections
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			}
		}

		if sl.keepalives {
			newConn.SetKeepAlive(true)
			newConn.SetKeepAlivePeriod(3 * time.Minute)
		}

		return newConn, err
	}
}

func NewStoppableTCPListener(addr string, keepalives bool) (net.Listener, error) {
	l, err := net.Listen("tcp", addr)
	if err != nil {
		return nil, err
	}
	sl := &StoppableTCPListener{
		l.(*net.TCPListener),
		make(chan os.Signal, 1),
		keepalives,
	}
	signal.Notify(sl.stop, syscall.SIGINT)
	return sl, nil
}

func NewStoppableTLSListener(addr string, keepalives bool, certFile string, keyFile string) (net.Listener, error) {
	var err error
	tlsConfig := &tls.Config{}<commit_msg>Fixes as per code review<commit_after>package luddite

import (
	"crypto/tls"
	"net"
	"os"
	"os/signal"
	"syscall"
	"time"
)

// Based on http://www.hydrogen18.com/blog/stop-listening-http-server-go.html,
// but stops on SIGINT instead of explicit Stop() call

type ListenerStoppedError struct {
}

func (e *ListenerStoppedError) Error() string {
	return "Listener stopped"
}

type StoppableTCPListener struct {
	*net.TCPListener
	stop       chan os.Signal
	keepalives bool
}

func (sl *StoppableTCPListener) Accept() (net.Conn, error) {

	for {
		//Wait up to one second for a new connection
		sl.TCPListener.SetDeadline(time.Now().Add(time.Second))
		newConn, err := sl.TCPListener.AcceptTCP()

		//Check for the channel being closed
		select {
		case <-sl.stop:
			return nil, &ListenerStoppedError{}
		default:
			//If nothing came in on the channel, continue as normal
		}

		if err != nil {
			netErr, ok := err.(net.Error)

			//If this is a timeout, then continue to wait for
			//new connections
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			}

			return nil, err
		}

		if sl.keepalives {
			newConn.SetKeepAlive(true)
			newConn.SetKeepAlivePeriod(3 * time.Minute)
		}

		return newConn, err
	}
}

func NewStoppableTCPListener(addr string, keepalives bool) (net.Listener, error) {
	l, err := net.Listen("tcp", addr)
	if err != nil {
		return nil, err
	}
	sl := &StoppableTCPListener{
		l.(*net.TCPListener),
		make(chan os.Signal, 1),
		keepalives,
	}
	signal.Notify(sl.stop, syscall.SIGINT)
	return sl, nil
}

func NewStoppableTLSListener(addr string, keepalives bool, certFile string, keyFile string) (net.Listener, error) {
	var err error
	tlsConfig := &tls.Config{}
{'fraction_non_alphanumeric': 0.12237037037037037, 'fraction_numerical': 0.0023703703703703703, 'mean_word_length': 3.612021857923497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '45338', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1169, 'n_words': 326}