<commit_before>package main

import (
	"fmt"
	"strings"
	"sync"

	"github.com/Sirupsen/logrus"
	"github.com/Sirupsen/tomb"

	"net"
	"time"
)

var AcceptTimeout = time.Second

// Proxy represents the proxy in its entirity with all its links. The main
// responsibility of Proxy is to accept new client and create Links between the
// client and upstream.
type Proxy struct {
	sync.Mutex

	Name     string
	Listen   string
	Upstream string

	started chan bool

	tomb  tomb.Tomb
	links []*link
}

func NewProxy() *Proxy {
	return &Proxy{
		started: make(chan bool, 1),
	}
}

func (proxy *Proxy) Start() {
	logrus.WithFields(logrus.Fields{
		"name":     proxy.Name,
		"proxy":    proxy.Listen,
		"upstream": proxy.Upstream,
	}).Info("Starting proxy")

	go proxy.server()
}

// server runs the Proxy server, accepting new clients and creating Links to
// connect them to upstreams.
func (proxy *Proxy) server() {
	ln, err := net.Listen("tcp", proxy.Listen)
	if err != nil {
		logrus.WithFields(logrus.Fields{"upstream": proxy.Upstream, "err": err}).Error("Unable to start proxy server")
		return
	}

	// This is a super hacky way to get a local address correct.
	// We want to set #Listen because if it's not supplied in the API we'll just
	// use an ephemeral port.
	tcpAddr := ln.Addr().(*net.TCPAddr)
	tcpAddrIp := strings.Trim(string(tcpAddr.IP), "\u0000")
	if tcpAddrIp == "" {
		tcpAddrIp = "localhost"
	}
	proxy.Listen = fmt.Sprintf("%s:%d", tcpAddrIp, tcpAddr.Port)

	proxy.started <- true

	for {
		// Set a deadline to not make Accept() block forever, allowing us to shut
		// down this thread.
		err = ln.(*net.TCPListener).SetDeadline(time.Now().Add(AcceptTimeout))
		if err != nil {
			logrus.WithField("name", proxy.Name).Fatal("Unable to set deadline")
		}

		// Shut down if the tomb is not empty
		select {
		case <-proxy.tomb.Dying():<commit_msg>proxy: fix localhost parsing<commit_after>package main

import (
	"fmt"
	"sync"

	"github.com/Sirupsen/logrus"
	"github.com/Sirupsen/tomb"

	"net"
	"time"
)

var AcceptTimeout = time.Second

// Proxy represents the proxy in its entirity with all its links. The main
// responsibility of Proxy is to accept new client and create Links between the
// client and upstream.
type Proxy struct {
	sync.Mutex

	Name     string
	Listen   string
	Upstream string

	started chan bool

	tomb  tomb.Tomb
	links []*link
}

func NewProxy() *Proxy {
	return &Proxy{
		started: make(chan bool, 1),
	}
}

func (proxy *Proxy) Start() {
	go proxy.server()
}

// server runs the Proxy server, accepting new clients and creating Links to
// connect them to upstreams.
func (proxy *Proxy) server() {
	ln, err := net.Listen("tcp", proxy.Listen)
	if err != nil {
		logrus.WithFields(logrus.Fields{"upstream": proxy.Upstream, "err": err}).Error("Unable to start proxy server")
		return
	}

	// This is a super hacky way to get a local address correct.
	// We want to set #Listen because if it's not supplied in the API we'll just
	// use an ephemeral port.
	tcpAddr := ln.Addr().(*net.TCPAddr)
	tcpAddrIp := string(tcpAddr.IP)
	if net.ParseIP(string(tcpAddr.IP)) == nil {
		tcpAddrIp = "127.0.0.1"
	}
	proxy.Listen = fmt.Sprintf("%s:%d", tcpAddrIp, tcpAddr.Port)

	proxy.started <- true

	logrus.WithFields(logrus.Fields{
		"name":     proxy.Name,
		"proxy":    proxy.Listen,
		"upstream": proxy.Upstream,
	}).Info("Started proxy")

	for {
		// Set a deadline to not make Accept() block forever, allowing us to shut
		// down this thread.
		err = ln.(*net.TCPListener).SetDeadline(time.Now().Add(AcceptTimeout))
		if err != nil {
			logrus.WithField("name", proxy.Name).Fatal("Unable to set deadline")
		}

		// Shut down if the tomb is not empty
		select {
		case <-proxy.tomb.Dying():
{'fraction_non_alphanumeric': 0.13091397849462366, 'fraction_numerical': 0.0032258064516129032, 'mean_word_length': 3.5157766990291264, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '3556531', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1245, 'n_words': 407}