<commit_before>// Socket server that can be shut down -- stop serving, in a graceful manner.
package shutdown1

import (
	"io"
	"log"
	"net"
	"sync"
)

type Server struct {
	listener net.Listener
	quit     chan interface{}
	wg       sync.WaitGroup
}

// NewServer creates and launches a new server listening on addr. When the
// server is returned, it's already listening for new connections.
func NewServer(addr string) *Server {
	s := &Server{
		quit: make(chan interface{}),
	}
	l, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}
	s.listener = l
	go s.serve()
	return s
}

// Stop asks the server to stop and blocks until the server is actually stopped.
func (s *Server) Stop() {
	log.Println("asking the server to stop")
	close(s.quit)
	s.listener.Close()
	s.wg.Wait()
	log.Println("server stopped")
}

func (s *Server) serve() {
	s.wg.Add(1)

	defer func() {
		s.wg.Done()
	}()

	for {
		conn, err := s.listener.Accept()
		if err != nil {
			select {
			case <-s.quit:
				return
			default:
			}

			log.Println("accept error:", err)
		} else {
			go s.handleConection(conn)
		}
	}
}

func (s *Server) handleConection(conn net.Conn) {
	s.wg.Add(1)
	log.Printf("connection from %v", conn.RemoteAddr())

	defer func() {
		log.Printf("connection from %v done", conn.RemoteAddr())
		conn.Close()
		s.wg.Done()
	}()

	buf := make([]byte, 2048)
	for {
		n, err := conn.Read(buf)
		if err != nil && err != io.EOF {
			log.Println("read error", err)
			return
		}
		if n == 0 {
			return
		}
		log.Printf("received from %v: %s", conn.RemoteAddr(), string(buf[:n]))
	}
}

func init() {
	log.SetFlags(log.Ltime | log.Lmicroseconds)
}<commit_msg>Clean up shutdown1<commit_after>// Socket server that can be shut down -- stop serving, in a graceful manner.

// TODO: copy most of this to shutdown1, they should be identical except
// handleConnection.

package shutdown1

import (
	"io"
	"log"
	"net"
	"sync"
)

type Server struct {
	listener net.Listener
	quit     chan interface{}
	wg       sync.WaitGroup
}

func NewServer(addr string) *Server {
	s := &Server{
		quit: make(chan interface{}),
	}
	l, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}
	s.listener = l
	go s.serve()
	return s
}

func (s *Server) Stop() {
	close(s.quit)
	s.listener.Close()
	s.wg.Wait()
}

func (s *Server) serve() {
	s.wg.Add(1)
	defer s.wg.Done()

	for {
		conn, err := s.listener.Accept()
		if err != nil {
			select {
			case <-s.quit:
				return
			default:
			}
		} else {
			go func() {
				s.wg.Add(1)
				s.handleConection(conn)
				s.wg.Done()
			}()
		}
	}
}

func (s *Server) handleConection(conn net.Conn) {
	defer conn.Close()
	buf := make([]byte, 2048)
	for {
		n, err := conn.Read(buf)
		if err != nil && err != io.EOF {
			log.Println("read error", err)
			return
		}
		if n == 0 {
			return
		}
		log.Printf("received from %v: %s", conn.RemoteAddr(), string(buf[:n]))
	}
}

func init() {
	log.SetFlags(log.Ltime | log.Lmicroseconds)
}
{'fraction_non_alphanumeric': 0.1600943714189417, 'fraction_numerical': 0.006066734074823054, 'mean_word_length': 2.9732262382864794, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4537721', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1127, 'n_words': 316}