<commit_before>package proxy

import (
	disc "github.com/jeffjen/go-discovery"
	"github.com/jeffjen/go-libkv/libkv"
	"github.com/jeffjen/go-proxy/proxy"

	log "github.com/Sirupsen/logrus"
	etcd "github.com/coreos/etcd/client"
	ctx "golang.org/x/net/context"

	"encoding/json"
	"io/ioutil"
	"time"
)

var (
	ProxyConfigKey string

	ConfigReset ctx.CancelFunc

	retry = &proxy.Backoff{}
)

func ConfigKey() string {
	var cfgkey string
	if buf, err := ioutil.ReadFile(".proxycfg"); err == nil {
		cfgkey = string(buf)
	}
	go func() {
		for _ = range time.Tick(2 * time.Minute) {
			ioutil.WriteFile(".proxycfg", []byte(ProxyConfigKey), 0644)
		}
	}()
	return cfgkey
}

func parse(spec string) (*Info, error) {
	var i = new(Info)
	if err := json.Unmarshal([]byte(spec), i); err != nil {
		return nil, err
	}
	if i.Name == "" {
		return nil, ErrMissingName
	}
	return i, nil
}

func get(value string) (targets []*Info) {
	targets = make([]*Info, 0)
	if err := json.Unmarshal([]byte(value), &targets); err != nil {
		log.WithFields(log.Fields{"err": err}).Warning("bad proxy spec")
		targets = nil
	}
	return
}

func doReload(pxycfg []*Info) {
	it, mod := Store.IterateW()
	for elem := range it {
		mod <- &libkv.Value{R: true}
		elem.X.(*Info).Cancel()
	}
	log.WithFields(log.Fields{"count": len(Targets)}).Debug("reload from args")
	for _, spec := range Targets {
		meta, err := parse(spec)
		if err != nil {
			log.WithFields(log.Fields{"err": err}).Warning("reload")
			continue
		}
		if err = Listen(meta); err != nil {
			if err != ErrProxyExist {
				log.WithFields(log.Fields{"err": err}).Debug("reload")
			}
		}
	}
	log.WithFields(log.Fields{"count": len(pxycfg)}).Debug("reload from cfgkey")
	for _, meta := range pxycfg {
		if err := Listen(meta); err != nil {
			if err != ErrProxyExist {
				log.WithFields(log.Fields{"err": err}).Debug("reload")
			}
		}
	}
}

func reloadWorker() chan<- []*Info {
	order := make(chan []*Info)
	go func() {
		for o := range order {
			doReload(o)
		}
	}()
	return order
}

func doWatch(c ctx.Context, watcher etcd.Watcher) <-chan []*Info {
	v := make(chan []*Info)
	go func() {
		evt, err := watcher.Next(c)
		if err != nil {
			log.WithFields(log.Fields{"err": err}).Debug("config")
			retry.Delay()
			v <- nil
		} else {
			retry.Reset()
			if evt.Node.Dir {
				log.WithFields(log.Fields{"key": evt.Node.Key}).Warning("not a valid node")
				v <- nil
			} else {
				switch evt.Action {
				default:
					v <- nil<commit_msg>FIX: default config key for event avoidance<commit_after>package proxy

import (
	disc "github.com/jeffjen/go-discovery"
	"github.com/jeffjen/go-libkv/libkv"
	"github.com/jeffjen/go-proxy/proxy"

	log "github.com/Sirupsen/logrus"
	etcd "github.com/coreos/etcd/client"
	ctx "golang.org/x/net/context"

	"encoding/json"
	"io/ioutil"
	"time"
)

var (
	ProxyConfigKey string = "__nobody__"

	ConfigReset ctx.CancelFunc

	retry = &proxy.Backoff{}
)

func ConfigKey() string {
	var cfgkey string
	if buf, err := ioutil.ReadFile(".proxycfg"); err == nil {
		cfgkey = string(buf)
	}
	go func() {
		for _ = range time.Tick(2 * time.Minute) {
			ioutil.WriteFile(".proxycfg", []byte(ProxyConfigKey), 0644)
		}
	}()
	return cfgkey
}

func parse(spec string) (*Info, error) {
	var i = new(Info)
	if err := json.Unmarshal([]byte(spec), i); err != nil {
		return nil, err
	}
	if i.Name == "" {
		return nil, ErrMissingName
	}
	return i, nil
}

func get(value string) (targets []*Info) {
	targets = make([]*Info, 0)
	if err := json.Unmarshal([]byte(value), &targets); err != nil {
		log.WithFields(log.Fields{"err": err, "value": value}).Warning("config")
		targets = nil
	}
	return
}

func doReload(pxycfg []*Info) {
	it, mod := Store.IterateW()
	for elem := range it {
		mod <- &libkv.Value{R: true}
		elem.X.(*Info).Cancel()
	}
	log.WithFields(log.Fields{"count": len(Targets)}).Debug("reload from args")
	for _, spec := range Targets {
		meta, err := parse(spec)
		if err != nil {
			log.WithFields(log.Fields{"err": err}).Warning("reload")
			continue
		}
		if err = Listen(meta); err != nil {
			if err != ErrProxyExist {
				log.WithFields(log.Fields{"err": err}).Debug("reload")
			}
		}
	}
	log.WithFields(log.Fields{"count": len(pxycfg)}).Debug("reload from cfgkey")
	for _, meta := range pxycfg {
		if err := Listen(meta); err != nil {
			if err != ErrProxyExist {
				log.WithFields(log.Fields{"err": err}).Debug("reload")
			}
		}
	}
}

func reloadWorker() chan<- []*Info {
	order := make(chan []*Info)
	go func() {
		for o := range order {
			doReload(o)
		}
	}()
	return order
}

func doWatch(c ctx.Context, watcher etcd.Watcher) <-chan []*Info {
	v := make(chan []*Info)
	go func() {
		evt, err := watcher.Next(c)
		if err != nil {
			log.WithFields(log.Fields{"err": err}).Debug("config")
			retry.Delay()
			v <- nil
		} else {
			retry.Reset()
			if evt.Node.Dir {
				log.WithFields(log.Fields{"key": evt.Node.Key}).Warning("not a valid node")
				v <- nil
			} else {
				log.WithFields(log.Fields{"key": evt.Node.Key}).Warning("cfgkey")
				switch evt.Action {
				default:
					v <- nil
{'fraction_non_alphanumeric': 0.18163869693978282, 'fraction_numerical': 0.0023692003948667323, 'mean_word_length': 3.4991119005328595, 'pattern_counts': {'":': 18, '<': 15, '<?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': '1623872', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1926, 'n_words': 426}