<commit_before>package main

import (
	"encoding/json"
	"log"
	"net"
	"strings"
)

func udpStreamer(route *Route, logstream chan *Log) {
	var types map[string]struct{}
	if route.Source != nil {
		types = make(map[string]struct{})
		for _, t := range route.Source.Types {
			types[t] = struct{}{}
		}
	}
	for logline := range logstream {
		if types != nil {
			if _, ok := types[logline.Type]; !ok {
				continue
			}
		}
		appinfo := strings.SplitN(logline.Name, "_", 2)
		logline.Appname = appinfo[0]
		logline.Port = appinfo[1]
		logline.Tag = route.Target.AppendTag

		addr, err := route.backends.Get(logline.Appname)
		debug(logline.Appname, addr)
		if err != nil {
			debug("Get backend failed", err)
			log.Println(logline.Appname, logline.Data)
			continue
		}

		udpAddr, err := net.ResolveUDPAddr("udp", addr)
		if err != nil {
			debug("Resolve udp failed", err)
			continue
		}

		var conn *net.UDPConn
		conn, err = net.DialUDP("udp", nil, udpAddr)
		if err != nil {
			debug("Connect backend failed", err)
			continue
		}
		defer conn.Close()
		encoder := json.NewEncoder(conn)
		encoder.Encode(logline)
	}
}
<commit_msg>support backend failover<commit_after>package main

import (
	"encoding/json"
	"log"
	"net"
	"strings"
)

func udpStreamer(route *Route, logstream chan *Log) {
	var types map[string]struct{}
	if route.Source != nil {
		types = make(map[string]struct{})
		for _, t := range route.Source.Types {
			types[t] = struct{}{}
		}
	}
	for logline := range logstream {
		if types != nil {
			if _, ok := types[logline.Type]; !ok {
				continue
			}
		}
		appinfo := strings.SplitN(logline.Name, "_", 2)
		logline.Appname = appinfo[0]
		logline.Port = appinfo[1]
		logline.Tag = route.Target.AppendTag

		for offset := 0; offset < route.backends.Len(); offset++ {
			addr, err := route.backends.Get(logline.Appname, offset)
			if err != nil {
				debug("Get backend failed", err)
				log.Println(logline.Appname, logline.Data)
				break
			}

			debug(logline.Appname, addr)
			udpAddr, err := net.ResolveUDPAddr("udp", addr)
			if err != nil {
				debug("Resolve udp failed", err)
				continue
			}

			conn, err := net.DialUDP("udp", nil, udpAddr)
			if err != nil {
				debug("Connect backend failed", err)
				continue
			}
			defer conn.Close()
			encoder := json.NewEncoder(conn)
			encoder.Encode(logline)
			break
		}
	}
}

{'fraction_non_alphanumeric': 0.1473460721868365, 'fraction_numerical': 0.0029723991507431, 'mean_word_length': 3.2836363636363637, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4755316', 'n_tokens_mistral': 987, 'n_tokens_neox': 864, 'n_words': 199}