<commit_before>package sos

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/funkygao/gafka/sos"
	"github.com/funkygao/go-metrics"
	log "github.com/funkygao/log4go"
)

var (
	sosMetrics  = metrics.NewRegisteredCounter("sos", nil)
	lastSos     time.Time
	IdleTimeout = 5 * time.Minute
)

func init() {
	http.HandleFunc("/", handleSOS)
	go http.ListenAndServe(fmt.Sprintf(":%d", sos.SOSPort), nil)
	go maintainSosCounter()

	log.Info("SOS receiver started")
}

func handleSOS(w http.ResponseWriter, r *http.Request) {
	sosMsg, _ := ioutil.ReadAll(r.Body)
	r.Body.Close()

	sosMetrics.Inc(1)
	lastSos = time.Now()
	log.Critical("SOS[%s] from %s %s", r.Header.Get(sos.IdentHeader), r.RemoteAddr, string(sosMsg))

	w.WriteHeader(http.StatusAccepted)
}

func maintainSosCounter() {
	for {
		time.Sleep(time.Minute)

		if time.Since(lastSos) >= IdleTimeout {
			if sosMetrics.Count() > 0 {
				log.Info("SOS[#%d] idle over %s, metrics reset", sosMetrics.Count(), IdleTimeout)
			}
			sosMetrics.Clear()
		}
	}
}<commit_msg>bug fix: 'kguard -version' will even show log: SOS receiver started<commit_after>package sos

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/funkygao/gafka/sos"
	"github.com/funkygao/go-metrics"
	log "github.com/funkygao/log4go"
)

var (
	sosMetrics  = metrics.NewRegisteredCounter("sos", nil)
	lastSos     time.Time
	IdleTimeout = 5 * time.Minute
)

func init() {
	http.HandleFunc("/", handleSOS)
	go http.ListenAndServe(fmt.Sprintf(":%d", sos.SOSPort), nil)
	go maintainSosCounter()
}

func handleSOS(w http.ResponseWriter, r *http.Request) {
	sosMsg, _ := ioutil.ReadAll(r.Body)
	r.Body.Close()

	sosMetrics.Inc(1)
	lastSos = time.Now()
	log.Critical("SOS[%s] from %s %s", r.Header.Get(sos.IdentHeader), r.RemoteAddr, string(sosMsg))

	w.WriteHeader(http.StatusAccepted)
}

func maintainSosCounter() {
	for {
		time.Sleep(time.Minute)

		if time.Since(lastSos) >= IdleTimeout {
			if sosMetrics.Count() > 0 {
				log.Info("SOS[#%d] idle over %s, metrics reset", sosMetrics.Count(), IdleTimeout)
			}
			sosMetrics.Clear()
		}
	}
}
{'fraction_non_alphanumeric': 0.16257816257816257, 'fraction_numerical': 0.003848003848003848, 'mean_word_length': 4.279187817258883, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, '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': '7209239', 'n_tokens_mistral': 912, 'n_tokens_neox': 867, 'n_words': 149}