<commit_before>package golib

import (
	"errors"
	"net"
	"sync"
)

// FirstIpAddress tries to get the main public IP of the local host.
// It iterates all available, enabled network interfaces and looks for the first
// non-local IP address.
func FirstIpAddress() (net.IP, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}
	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
			// Loopback and disabled interfaces are not interesting
			continue
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return nil, err
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPNet:
				return v.IP, nil
			case *net.IPAddr:
				return v.IP, nil
			}
		}
	}
	return nil, errors.New("No valid network interfaces found")
}

// ==================== TCP listener task ====================

// TCPConnectionHandler is a callback function for TCPListenerTask, which is
// invoked whenever a new TCP connection is successfully accepted.
type TCPConnectionHandler func(wg *sync.WaitGroup, conn *net.TCPConn)

// TCPListenerTask is an implementation of the Task interface that listens
// for incoming TCP connections on a given TCP endpoint. A handler function
// is invoked for every accepted TCP connection, and an optional hook can be
// executed when the TCP socket is closed and the task stops.
type TCPListenerTask struct {
	*LoopTask

	// ListenEndpoint is the TCP endpoint to open a TCP listening socket on.
	ListenEndpoint string

	// Handler is a required callback-function that will be called for every
	// successfully established TCP connection. It is not called in a separate
	// goroutine, so it should fork a new routine for long-running connections.
	// The handler is always executed while the StopChan in the underlying
	// LoopTask is locked.
	Handler TCPConnectionHandler

	// StopHook is an optional callback that is invoked after the task stops and
	// the listening TCP socket is closed. When StopHook is executed, the underlying
	// LoopTask/StopChan is NOT locked, so helpers methods like Execute() must be used
	// if synchronization is required.
	StopHook func()

	listener *net.TCPListener
}

// String implements the Task interface by returning a descriptive string.
func (task *TCPListenerTask) String() string {
	return "TCP listener " + task.ListenEndpoint
}

// Start implements the Task interface. It opens the TCP listen socket and
// starts accepting incoming connections.
func (task *TCPListenerTask) Start(wg *sync.WaitGroup) StopChan {
	return task.ExtendedStart(nil, wg)
}

// ExtendedStart creates the TCP listen socket and starts accepting incoming connections.
// In addition, a hook function can be defined that will be called once after the
// socket has been opened successfully and is passed the resolved address of the TCP endpoint.
func (task *TCPListenerTask) ExtendedStart(start func(addr net.Addr), wg *sync.WaitGroup) StopChan {
	hook := task.StopHook
	defer func() {
		if hook != nil {
			hook()
		}
	}()

	endpoint, err := net.ResolveTCPAddr("tcp", task.ListenEndpoint)
	if err != nil {
		return NewStoppedChan(err)
	}
	task.listener, err = net.ListenTCP("tcp", endpoint)
	if err != nil {
		return NewStoppedChan(err)
	}
	if start != nil {
		start(task.listener.Addr())
	}
	hook = nil
	task.LoopTask = task.listen(wg)
	return task.LoopTask.Start(wg)
}

func (task *TCPListenerTask) listen(wg *sync.WaitGroup) *LoopTask {<commit_msg>Fixed bug caused by failed initialization.<commit_after>package golib

import (
	"errors"
	"net"
	"sync"
)

// FirstIpAddress tries to get the main public IP of the local host.
// It iterates all available, enabled network interfaces and looks for the first
// non-local IP address.
func FirstIpAddress() (net.IP, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}
	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
			// Loopback and disabled interfaces are not interesting
			continue
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return nil, err
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPNet:
				return v.IP, nil
			case *net.IPAddr:
				return v.IP, nil
			}
		}
	}
	return nil, errors.New("No valid network interfaces found")
}

// ==================== TCP listener task ====================

// TCPConnectionHandler is a callback function for TCPListenerTask, which is
// invoked whenever a new TCP connection is successfully accepted.
type TCPConnectionHandler func(wg *sync.WaitGroup, conn *net.TCPConn)

// TCPListenerTask is an implementation of the Task interface that listens
// for incoming TCP connections on a given TCP endpoint. A handler function
// is invoked for every accepted TCP connection, and an optional hook can be
// executed when the TCP socket is closed and the task stops.
type TCPListenerTask struct {
	*LoopTask

	// ListenEndpoint is the TCP endpoint to open a TCP listening socket on.
	ListenEndpoint string

	// Handler is a required callback-function that will be called for every
	// successfully established TCP connection. It is not called in a separate
	// goroutine, so it should fork a new routine for long-running connections.
	// The handler is always executed while the StopChan in the underlying
	// LoopTask is locked.
	Handler TCPConnectionHandler

	// StopHook is an optional callback that is invoked after the task stops and
	// the listening TCP socket is closed. When StopHook is executed, the underlying
	// LoopTask/StopChan is NOT locked, so helpers methods like Execute() must be used
	// if synchronization is required.
	StopHook func()

	listener *net.TCPListener
}

// String implements the Task interface by returning a descriptive string.
func (task *TCPListenerTask) String() string {
	return "TCP listener " + task.ListenEndpoint
}

// Start implements the Task interface. It opens the TCP listen socket and
// starts accepting incoming connections.
func (task *TCPListenerTask) Start(wg *sync.WaitGroup) StopChan {
	return task.ExtendedStart(nil, wg)
}

// ExtendedStart creates the TCP listen socket and starts accepting incoming connections.
// In addition, a hook function can be defined that will be called once after the
// socket has been opened successfully and is passed the resolved address of the TCP endpoint.
func (task *TCPListenerTask) ExtendedStart(start func(addr net.Addr), wg *sync.WaitGroup) StopChan {
	hook := task.StopHook
	defer func() {
		if hook != nil {
			hook()
		}
	}()
	task.LoopTask = task.listen(wg)

	endpoint, err := net.ResolveTCPAddr("tcp", task.ListenEndpoint)
	if err != nil {
		return NewStoppedChan(err)
	}
	task.listener, err = net.ListenTCP("tcp", endpoint)
	if err != nil {
		return NewStoppedChan(err)
	}
	if start != nil {
		start(task.listener.Addr())
	}
	hook = nil
	return task.LoopTask.Start(wg)
}

func (task *TCPListenerTask) listen(wg *sync.WaitGroup) *LoopTask {
{'fraction_non_alphanumeric': 0.09660798626019751, 'fraction_numerical': 0.0005724917704308, 'mean_word_length': 3.8159889731219847, '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': '1123579', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1935, 'n_words': 839}