<commit_before>// package balancer provides weighted round-robin load balancing of network
// connections with the ability to specify quality of service (QOS) levels.
package balancer

import (
	"fmt"
	"math/rand"
	"net"
	"sort"

	"github.com/getlantern/golog"
)

var (
	log = golog.LoggerFor("balancer")
)

var (
	emptyDialers = []*dialer{}
)

// Balancer balances connections established by one or more Dialers.
type Balancer struct {
	dialers []*dialer
	trusted []*dialer
}

// New creates a new Balancer using the supplied Dialers.
func New(dialers ...*Dialer) *Balancer {
	trustedDialersCount := 0

	bal := new(Balancer)

	bal.dialers = make([]*dialer, 0, len(dialers))

	for _, d := range dialers {
		dl := &dialer{Dialer: d}
		dl.start()
		bal.dialers = append(bal.dialers, dl)

		if dl.Trusted {
			trustedDialersCount++
		}
	}

	// Sort dialers by QOS (ascending) for later selection
	sort.Sort(byQOSAscending(bal.dialers))

	bal.trusted = make([]*dialer, 0, trustedDialersCount)

	for _, d := range bal.dialers {
		if d.Trusted {
			bal.trusted = append(bal.trusted, d)
		}
	}

	return bal
}

// DialQOS dials network, addr using one of the currently active configured
// Dialers. It attempts to use a Dialer whose QOS is higher than targetQOS, but
// will use the highest QOS Dialer(s) if none meet targetQOS. When multiple
// Dialers meet the targetQOS, load is distributed amongst them randomly based
// on their relative Weights.
//
// If a Dialer fails to connect, Dial will keep falling back through the
// remaining Dialers until it either manages to connect, or runs out of dialers
// in which case it returns an error.
func (b *Balancer) DialQOS(network, addr string, targetQOS int) (net.Conn, error) {
	var dialers []*dialer

	_, port, _ := net.SplitHostPort(addr)

	if port == "" || port == "80" || port == "8080" {
		dialers = b.trusted
	} else {
		dialers = b.dialers
	}

	for {
		if len(dialers) == 0 {
			return nil, fmt.Errorf("No dialers left to try")
		}
		var d *dialer<commit_msg>just adding dummy logging and a comment<commit_after>// package balancer provides weighted round-robin load balancing of network
// connections with the ability to specify quality of service (QOS) levels.
package balancer

import (
	"fmt"
	"math/rand"
	"net"
	"sort"

	"github.com/getlantern/golog"
)

var (
	log = golog.LoggerFor("balancer")
)

var (
	emptyDialers = []*dialer{}
)

// Balancer balances connections established by one or more Dialers.
type Balancer struct {
	dialers []*dialer
	trusted []*dialer
}

// New creates a new Balancer using the supplied Dialers.
func New(dialers ...*Dialer) *Balancer {
	trustedDialersCount := 0

	bal := new(Balancer)

	bal.dialers = make([]*dialer, 0, len(dialers))

	for _, d := range dialers {
		dl := &dialer{Dialer: d}
		dl.start()
		bal.dialers = append(bal.dialers, dl)

		if dl.Trusted {
			trustedDialersCount++
		}
	}

	// Sort dialers by QOS (ascending) for later selection
	sort.Sort(byQOSAscending(bal.dialers))

	bal.trusted = make([]*dialer, 0, trustedDialersCount)

	for _, d := range bal.dialers {
		if d.Trusted {
			bal.trusted = append(bal.trusted, d)
		}
	}

	return bal
}

// DialQOS dials network, addr using one of the currently active configured
// Dialers. It attempts to use a Dialer whose QOS is higher than targetQOS, but
// will use the highest QOS Dialer(s) if none meet targetQOS. When multiple
// Dialers meet the targetQOS, load is distributed amongst them randomly based
// on their relative Weights.
//
// If a Dialer fails to connect, Dial will keep falling back through the
// remaining Dialers until it either manages to connect, or runs out of dialers
// in which case it returns an error.
func (b *Balancer) DialQOS(network, addr string, targetQOS int) (net.Conn, error) {
	var dialers []*dialer

	_, port, _ := net.SplitHostPort(addr)

	if len(b.trusted) == 0 {
		log.Error("No trusted dialers!")
	}

	// We try to identify HTTP traffic (as opposed to HTTPS) by port and only
	// send HTTP traffic to dialers marked as trusted.
	if port == "" || port == "80" || port == "8080" {
		dialers = b.trusted
	} else {
		dialers = b.dialers
	}

	for {
		if len(dialers) == 0 {
			return nil, fmt.Errorf("No dialers left to try")
		}
		var d *dialer
{'fraction_non_alphanumeric': 0.11373289287399717, 'fraction_numerical': 0.004955167531854649, 'mean_word_length': 3.577753779697624, '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': '6711638', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1443, 'n_words': 503}