<commit_before>package main

import (
	"bytes"
	"github.com/Cistern/sflow"
	"net"
)

func sFlowParser(buffer []byte) {
	reader := bytes.NewReader(buffer)
	d := sflow.NewDecoder(reader)
	dgram, err := d.Decode()
	if err != nil {
		println(err)
		return
	}
	for _, sample := range dgram.Samples {
		println(sample)
	}
}

func sFlowListener() (err error) {
	// Start listening UDP socket, check if it started properly
	UDPAddr, err := net.ResolveUDPAddr("udp", ":6343")
	conn, err := net.ListenUDP("udp", UDPAddr)
	if err != nil {
		return err
	}

	var buffer []byte
	for {
		conn.ReadFromUDP(buffer)
		sFlowParser(buffer)
	}

	return nil
}<commit_msg>Break listener operation for graceful exit<commit_after>package main

import (
	"bytes"
	"fmt"
	"github.com/Cistern/sflow"
	"net"
	"time"
)

func sFlowParser(buffer []byte) {
	reader := bytes.NewReader(buffer)
	d := sflow.NewDecoder(reader)
	dgram, err := d.Decode()
	if err != nil {
		println(err)
		return
	}
	for _, sample := range dgram.Samples {
		println(sample)
	}
}

func sFlowListener(AppConfig app_config) (err error) {
	defer wait.Done()

	var udp_addr = fmt.Sprintf("[%s]:%d", AppConfig.SFlowConfig.Address, AppConfig.SFlowConfig.Port)

	DebugLogger.Println("Binding sFlow listener to", udp_addr)

	UDPAddr, err := net.ResolveUDPAddr("udp", udp_addr)
	if err != nil {
		ErrorLogger.Println(err)
		return err
	}
	conn, err := net.ListenUDP("udp", UDPAddr)
	if err != nil {
		ErrorLogger.Println(err)
		return err
	}

	var buffer []byte
	for running {
		/*
		  Normally read would block, but we want to be able to break this
		  loop gracefuly. So add read timeout and every 0.1s check if it is
		  time to finish
		*/
		conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		var read, _, err = conn.ReadFromUDP(buffer)
		if read > 0 && err != nil {
			sFlowParser(buffer)
		}

	}

	conn.Close()

	return nil
}
{'fraction_non_alphanumeric': 0.13372402770378264, 'fraction_numerical': 0.005327650506126798, 'mean_word_length': 3.7908163265306123, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, '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': '575605', 'n_tokens_mistral': 742, 'n_tokens_neox': 695, 'n_words': 179}