<commit_before>package util

import (
	"io"
	"net"
	"os"
	"sync"
)

func GetEnvDefault(key string, defaultVal string) string {
	val := os.Getenv(key)
	if val == "" {
		return defaultVal
	}

	return val
}

type ReconnectMutex struct {
	mutex        sync.RWMutex
	reconnecting bool
}

func (r *ReconnectMutex) Check() bool {
	r.mutex.RLock()
	defer r.mutex.RUnlock()

	return r.reconnecting
}

func (r *ReconnectMutex) Update(state bool) {
	r.mutex.Lock()
	defer r.mutex.Unlock()

	r.reconnecting = state
}

// ProxyTCP is a basic TCP proxy which can terminate connections. It can be
// used to test reconnect behaviour.
type ProxyTCP struct {
	sync.Mutex
	listener net.Listener
	remote   string
	conns    []net.Conn
	wg       sync.WaitGroup
}

func NewProxyTCP(lAddr, rAddr string) (*ProxyTCP, error) {
	ln, err := net.Listen("tcp", lAddr)
	if err != nil {
		return nil, err
	}

	proxy := &ProxyTCP{
		listener: ln,
		remote:   rAddr,
	}
	go proxy.AcceptLoop()

	return proxy, nil
}

func (p *ProxyTCP) Addr() string {
	return p.listener.Addr().String()
}

func (p *ProxyTCP) AcceptLoop() {
	for {
		p.wg.Add(1)
		defer p.wg.Done()

		lConn, err := p.listener.Accept()
		if err != nil {
			return
		}

		p.Lock()
		p.conns = append(p.conns, lConn)
		p.Unlock()

		rConn, err := net.Dial("tcp", p.remote)
		if err != nil {
			return
		}

		go io.Copy(lConn, rConn)
		go io.Copy(rConn, lConn)
	}
}

func (p *ProxyTCP) Close() {
	p.listener.Close()
	p.wg.Wait()
	p.KillConnected()
}

func (p *ProxyTCP) KillConnected() {
	p.Lock()
	for _, conn := range p.conns {
		conn.Close()
	}
	p.Unlock()
}<commit_msg>Prefer defer as more idiomatic style<commit_after>package util

import (
	"io"
	"net"
	"os"
	"sync"
)

func GetEnvDefault(key string, defaultVal string) string {
	val := os.Getenv(key)
	if val == "" {
		return defaultVal
	}

	return val
}

type ReconnectMutex struct {
	mutex        sync.RWMutex
	reconnecting bool
}

func (r *ReconnectMutex) Check() bool {
	r.mutex.RLock()
	defer r.mutex.RUnlock()

	return r.reconnecting
}

func (r *ReconnectMutex) Update(state bool) {
	r.mutex.Lock()
	defer r.mutex.Unlock()

	r.reconnecting = state
}

// ProxyTCP is a basic TCP proxy which can terminate connections. It can be
// used to test reconnect behaviour.
type ProxyTCP struct {
	sync.Mutex
	listener net.Listener
	remote   string
	conns    []net.Conn
	wg       sync.WaitGroup
}

func NewProxyTCP(lAddr, rAddr string) (*ProxyTCP, error) {
	ln, err := net.Listen("tcp", lAddr)
	if err != nil {
		return nil, err
	}

	proxy := &ProxyTCP{
		listener: ln,
		remote:   rAddr,
	}
	go proxy.AcceptLoop()

	return proxy, nil
}

func (p *ProxyTCP) Addr() string {
	return p.listener.Addr().String()
}

func (p *ProxyTCP) AcceptLoop() {
	for {
		p.wg.Add(1)
		defer p.wg.Done()

		lConn, err := p.listener.Accept()
		if err != nil {
			return
		}

		p.Lock()
		p.conns = append(p.conns, lConn)
		p.Unlock()

		rConn, err := net.Dial("tcp", p.remote)
		if err != nil {
			return
		}

		go io.Copy(lConn, rConn)
		go io.Copy(rConn, lConn)
	}
}

func (p *ProxyTCP) Close() {
	p.listener.Close()
	p.wg.Wait()
	p.KillConnected()
}

func (p *ProxyTCP) KillConnected() {
	p.Lock()
	defer p.Unlock()
	for _, conn := range p.conns {
		conn.Close()
	}
}
{'fraction_non_alphanumeric': 0.14290133003402414, 'fraction_numerical': 0.0006186204763377668, 'mean_word_length': 3.1729032258064516, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4248670', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1269, 'n_words': 334}