<commit_before>package knuckles

import (
	"fmt"
	"net/http"
	"net/url"
	"time"
)

type Backend struct {
	Name          string
	Endpoint      string
	CheckInterval time.Duration
	CheckURL      *url.URL
	Alive         bool
	quitChan      chan bool
	notifyChan    chan BackendStatus
}

type BackendStatus struct {
	Name  string
	Alive bool
}

type BackendSettings struct {
	Endpoint      string
	CheckUrl      string
	CheckInterval time.Duration
	Updates       chan BackendStatus
}

func NewBackend(name string, settings BackendSettings) (*Backend, error) {
	var err error
	be := new(Backend)
	be.Name = name
	be.Alive = false
	be.quitChan = make(chan bool)

	if settings.CheckInterval == 0 ||
		settings.CheckUrl == "" ||
		settings.Endpoint == "" ||
		settings.Updates == nil {
		return nil, fmt.Errorf("Missing parameters")
	}
	be.Endpoint = settings.Endpoint
	be.notifyChan = settings.Updates
	be.CheckInterval = settings.CheckInterval

	be.CheckURL, err = url.Parse(settings.CheckUrl)
	return be, err
}

func (self *Backend) Start() {
	run := true

	self.CheckHealth()
	for run {
		select {
		case <-self.quitChan:
			run = false
		case <-time.After(self.CheckInterval):
			self.CheckHealth()
		}
	}
	self.quitChan <- true
}

func (self *Backend) Stop() {
	self.quitChan <- true
	<-self.quitChan
	close(self.quitChan)
}

func (self *Backend) CheckHealth() {
	current := self.Alive

	client := &http.Client{}

	req, _ := http.NewRequest("GET", self.CheckURL.String(), nil)
	req.Close = true

	resp, err := client.Do(req)
	if err == nil {
		defer resp.Body.Close()
	}

	if err != nil || (resp.StatusCode >= 400) {
		self.Alive = false
	} else {
		self.Alive = true
	}

	if current != self.Alive {
		status := BackendStatus{
			Name:  self.Name,
			Alive: self.Alive,
		}
		self.notifyChan <- status
	}
}<commit_msg>do not follow redirects on health checks<commit_after>package knuckles

import (
	"fmt"
	"net/http"
	"net/url"
	"time"
)

type Backend struct {
	Name          string
	Endpoint      string
	CheckInterval time.Duration
	CheckURL      *url.URL
	Alive         bool
	quitChan      chan bool
	notifyChan    chan BackendStatus
}

type BackendStatus struct {
	Name  string
	Alive bool
}

type BackendSettings struct {
	Endpoint      string
	CheckUrl      string
	CheckInterval time.Duration
	Updates       chan BackendStatus
}

func NewBackend(name string, settings BackendSettings) (*Backend, error) {
	var err error
	be := new(Backend)
	be.Name = name
	be.Alive = false
	be.quitChan = make(chan bool)

	if settings.CheckInterval == 0 ||
		settings.CheckUrl == "" ||
		settings.Endpoint == "" ||
		settings.Updates == nil {
		return nil, fmt.Errorf("Missing parameters")
	}
	be.Endpoint = settings.Endpoint
	be.notifyChan = settings.Updates
	be.CheckInterval = settings.CheckInterval

	be.CheckURL, err = url.Parse(settings.CheckUrl)
	return be, err
}

func (self *Backend) Start() {
	run := true

	self.CheckHealth()
	for run {
		select {
		case <-self.quitChan:
			run = false
		case <-time.After(self.CheckInterval):
			self.CheckHealth()
		}
	}
	self.quitChan <- true
}

func (self *Backend) Stop() {
	self.quitChan <- true
	<-self.quitChan
	close(self.quitChan)
}

func (self *Backend) CheckHealth() {
	current := self.Alive

	tr := &http.Transport{}

	req, _ := http.NewRequest("GET", self.CheckURL.String(), nil)
	req.Close = true

	resp, err := tr.RoundTrip(req)
	if err == nil {
		defer resp.Body.Close()
	}

	if err != nil {
		self.Alive = false
	} else {
		self.Alive = true
	}

	if current != self.Alive {
		status := BackendStatus{
			Name:  self.Name,
			Alive: self.Alive,
		}
		self.notifyChan <- status
	}
}
{'fraction_non_alphanumeric': 0.12138728323699421, 'fraction_numerical': 0.0013762730525736307, 'mean_word_length': 3.1770114942528735, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4761495', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1259, 'n_words': 318}