Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On the road to V1 #33

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
killed self
  • Loading branch information
thrawn01 committed Jun 5, 2018
commit 74e453a62785f752909420eef4260300973a2997
6 changes: 3 additions & 3 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type StdLogger interface {

type NullLogger struct{}

func (self *NullLogger) Print(...interface{}) {}
func (self *NullLogger) Printf(string, ...interface{}) {}
func (self *NullLogger) Println(...interface{}) {}
func (nl *NullLogger) Print(...interface{}) {}
func (nl *NullLogger) Printf(string, ...interface{}) {}
func (nl *NullLogger) Println(...interface{}) {}

// ***********************************************
// Public Word Formatting Functions
Expand Down
16 changes: 8 additions & 8 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ func NewTestLogger() *TestLogger {
return &TestLogger{""}
}

func (self *TestLogger) Print(stuff ...interface{}) {
self.result = self.result + fmt.Sprint(stuff...) + "|"
func (tl *TestLogger) Print(stuff ...interface{}) {
tl.result = tl.result + fmt.Sprint(stuff...) + "|"
}

func (self *TestLogger) Printf(format string, stuff ...interface{}) {
self.result = self.result + fmt.Sprintf(format, stuff...) + "|"
func (tl *TestLogger) Printf(format string, stuff ...interface{}) {
tl.result = tl.result + fmt.Sprintf(format, stuff...) + "|"
}

func (self *TestLogger) Println(stuff ...interface{}) {
self.result = self.result + fmt.Sprintln(stuff...) + "|"
func (tl *TestLogger) Println(stuff ...interface{}) {
tl.result = tl.result + fmt.Sprintln(stuff...) + "|"
}

func (self *TestLogger) GetEntry() string {
return self.result
func (tl *TestLogger) GetEntry() string {
return tl.result
}

var _ = Describe("args", func() {
Expand Down
40 changes: 20 additions & 20 deletions backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

const MAX_BACKOFF_WAIT = 2 * time.Second
const maxBackOffWait = 2 * time.Second

// A ChangeEvent is a representation of an key=value update, delete or expire. Args attempts to match
// a rule to the change and includes the matched rule in the ChangeEvent. If args is unable to match
Expand Down Expand Up @@ -67,28 +67,28 @@ type Backend interface {
Close()
}

func (self *Parser) FromBackend(backend Backend) (*Options, error) {
func (p *Parser) FromBackend(backend Backend) (*Options, error) {

options, err := self.ParseBackend(backend)
options, err := p.ParseBackend(backend)
if err != nil {
return options, err
}
// Apply the etcd values to the commandline and environment variables
return self.Apply(options)
return p.Apply(options)
}

func (self *Parser) ParseBackend(backend Backend) (*Options, error) {
values := self.NewOptions()
func (p *Parser) ParseBackend(backend Backend) (*Options, error) {
values := p.NewOptions()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer func() { cancel() }()
//
for _, rule := range self.rules {
for _, rule := range p.rules {
key := rule.BackendKey()
if rule.HasFlag(IsConfigGroup) {
pairs, err := backend.List(ctx, key)
if err != nil {
self.info("args.ParseBackend(): Failed to list '%s' - '%s'", key.Group, err.Error())
p.info("args.ParseBackend(): Failed to list '%s' - '%s'", key.Group, err.Error())
continue
}
// Iterate through all the key=values pairs for this group
Expand All @@ -100,15 +100,15 @@ func (self *Parser) ParseBackend(backend Backend) (*Options, error) {
pair, err := backend.Get(ctx, key)
if err != nil {
// This can be a normal occurrence, and probably shouldn't be logged
//self.info("args.ParseBackend(): Failed to fetch key '%s' - %s", key.Name, err.Error())
//p.info("args.ParseBackend(): Failed to fetch key '%s' - %s", key.Name, err.Error())
continue
}
values.Group(pair.Key.Group).Set(pair.Key.Name, pair.Value)
}
return values, nil
}

func (self *Parser) Watch(backend Backend, callBack func(ChangeEvent, error)) WatchCancelFunc {
func (p *Parser) Watch(backend Backend, callBack func(ChangeEvent, error)) WatchCancelFunc {
var isRunning sync.WaitGroup
var once sync.Once
done := make(chan struct{})
Expand Down Expand Up @@ -137,7 +137,7 @@ func (self *Parser) Watch(backend Backend, callBack func(ChangeEvent, error)) Wa
}

// find the rule this key is for
rule := self.findRule(event.Key)
rule := p.findRule(event.Key)
if rule != nil {
event.Rule = rule
}
Expand All @@ -151,7 +151,7 @@ func (self *Parser) Watch(backend Backend, callBack func(ChangeEvent, error)) Wa
Retry:
// Cancel our current context and sleep
cancel()
self.sleep()
p.sleep()
}
}()

Expand All @@ -165,8 +165,8 @@ func (self *Parser) Watch(backend Backend, callBack func(ChangeEvent, error)) Wa
}
}

func (self *Parser) findRule(key Key) *Rule {
for _, rule := range self.rules {
func (p *Parser) findRule(key Key) *Rule {
for _, rule := range p.rules {
if rule.HasFlag(IsConfigGroup) {
if rule.Group == key.Group {
return rule
Expand All @@ -180,12 +180,12 @@ func (self *Parser) findRule(key Key) *Rule {
return nil
}

func (self *Parser) sleep() {
self.attempts = self.attempts + 1
delay := time.Duration(self.attempts) * 2 * time.Millisecond
if delay > MAX_BACKOFF_WAIT {
delay = MAX_BACKOFF_WAIT
func (p *Parser) sleep() {
p.attempts = p.attempts + 1
delay := time.Duration(p.attempts) * 2 * time.Millisecond
if delay > maxBackOffWait {
delay = maxBackOffWait
}
self.log.Printf("Backend Retry in %v ...", delay)
p.log.Printf("Backend Retry in %v ...", delay)
time.Sleep(delay)
}
28 changes: 14 additions & 14 deletions backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewTestBackend() args.Backend {
keys: map[string]args.Pair{
"/root/bind": {Key: args.Key{Name: "bind"}, Value: "thrawn01.org:3366"}},
lists: map[string][]args.Pair{
"/root/endpoints": []args.Pair{
"/root/endpoints": {
{
Key: args.Key{Group: "endpoints", Name: "endpoint1"},
Value: "http://endpoint1.com:3366",
Expand All @@ -33,7 +33,7 @@ func NewTestBackend() args.Backend {
Value: `{ "host": "endpoint2", "port": "3366" }`,
},
},
"/root/watch": []args.Pair{
"/root/watch": {
{
Key: args.Key{Group: "watch", Name: "endpoint1"},
Value: "http://endpoint1.com:3366",
Expand All @@ -47,51 +47,51 @@ func fullKey(key args.Key) string {
return fmt.Sprintf("/root/%s", key.Join("/"))
}

func (self *TestBackend) Get(ctx context.Context, key args.Key) (args.Pair, error) {
pair, ok := self.keys[fullKey(key)]
func (tb *TestBackend) Get(ctx context.Context, key args.Key) (args.Pair, error) {
pair, ok := tb.keys[fullKey(key)]
if !ok {
return args.Pair{}, errors.New(fmt.Sprintf("'%s' not found", fullKey(key)))
}
return pair, nil
}

func (self *TestBackend) List(ctx context.Context, key args.Key) ([]args.Pair, error) {
pairs, ok := self.lists[fullKey(key)]
func (tb *TestBackend) List(ctx context.Context, key args.Key) ([]args.Pair, error) {
pairs, ok := tb.lists[fullKey(key)]
if !ok {
return []args.Pair{}, errors.New(fmt.Sprintf("'%s' not found", fullKey(key)))
}
return pairs, nil
}

func (self *TestBackend) Set(ctx context.Context, key args.Key, value string) error {
self.keys[fullKey(key)] = args.Pair{Key: key, Value: value}
func (tb *TestBackend) Set(ctx context.Context, key args.Key, value string) error {
tb.keys[fullKey(key)] = args.Pair{Key: key, Value: value}
return nil
}

// Watch monitors store for changes to key.
func (self *TestBackend) Watch(ctx context.Context, key string) (<-chan args.ChangeEvent, error) {
func (tb *TestBackend) Watch(ctx context.Context, key string) (<-chan args.ChangeEvent, error) {
changeChan := make(chan args.ChangeEvent, 2)

go func() {
var event args.ChangeEvent
select {
case event = <-watchChan:
changeChan <- event
case <-self.close:
case <-tb.close:
close(changeChan)
return
}
}()
return changeChan, nil
}

func (self *TestBackend) Close() {
if self.close != nil {
close(self.close)
func (tb *TestBackend) Close() {
if tb.close != nil {
close(tb.close)
}
}

func (self *TestBackend) GetRootKey() string {
func (tb *TestBackend) GetRootKey() string {
return "/root"
}

Expand Down
80 changes: 40 additions & 40 deletions benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Options struct {
values OptValues
}

func (self *Options) Get(key string) string {
return self.values[key]
func (o *Options) Get(key string) string {
return o.values[key]
}

type ArgParser interface {
Expand All @@ -24,14 +24,14 @@ type Api struct {
Parser ArgParser
}

func (self *Api) NewServer() http.Handler {
func (s *Api) NewServer() http.Handler {
router := http.NewServeMux()
router.HandleFunc("/", self.Index)
router.HandleFunc("/", s.Index)
return router
}

func (self *Api) Index(resp http.ResponseWriter, req *http.Request) {
opts := self.Parser.GetOpts()
func (s *Api) Index(resp http.ResponseWriter, req *http.Request) {
opts := s.Parser.GetOpts()
fmt.Fprintf(resp, "TestValue: %s", opts.Get("test-value"))
}

Expand All @@ -47,12 +47,12 @@ func NewParserBench(values OptValues) *ParserBench {
return parser
}

func (self *ParserBench) SetOpts(values OptValues) {
self.opts = &Options{values}
func (pb *ParserBench) SetOpts(values OptValues) {
pb.opts = &Options{values}
}

func (self *ParserBench) GetOpts() *Options {
return self.opts
func (pb *ParserBench) GetOpts() *Options {
return pb.opts
}

type ParserBenchMutex struct {
Expand All @@ -67,18 +67,18 @@ func NewParserBenchMutex(values OptValues) *ParserBenchMutex {
return parser
}

func (self *ParserBenchMutex) SetOpts(values OptValues) {
self.mutex.Lock()
self.opts = &Options{values}
self.mutex.Unlock()
func (pbm *ParserBenchMutex) SetOpts(values OptValues) {
pbm.mutex.Lock()
pbm.opts = &Options{values}
pbm.mutex.Unlock()
}

func (self *ParserBenchMutex) GetOpts() *Options {
self.mutex.Lock()
func (pbm *ParserBenchMutex) GetOpts() *Options {
pbm.mutex.Lock()
defer func() {
self.mutex.Unlock()
pbm.mutex.Unlock()
}()
return self.opts
return pbm.opts
}

// =================================================================
Expand All @@ -93,18 +93,18 @@ func NewParserBenchRWMutex(values OptValues) *ParserBenchRWMutex {
return parser
}

func (self *ParserBenchRWMutex) SetOpts(values OptValues) {
self.mutex.Lock()
self.opts = &Options{values}
self.mutex.Unlock()
func (pbmr *ParserBenchRWMutex) SetOpts(values OptValues) {
pbmr.mutex.Lock()
pbmr.opts = &Options{values}
pbmr.mutex.Unlock()
}

func (self *ParserBenchRWMutex) GetOpts() *Options {
self.mutex.Lock()
func (pbmr *ParserBenchRWMutex) GetOpts() *Options {
pbmr.mutex.Lock()
defer func() {
self.mutex.Unlock()
pbmr.mutex.Unlock()
}()
return self.opts
return pbmr.opts
}

// =================================================================
Expand All @@ -122,33 +122,33 @@ func NewParserBenchChannel(values OptValues) *ParserBenchChannel {
return parser
}

func (self *ParserBenchChannel) Open() {
func (pbc *ParserBenchChannel) Open() {
go func() {
defer func() {
close(self.get)
close(self.set)
close(self.done)
close(pbc.get)
close(pbc.set)
close(pbc.done)
}()
for {
select {
case self.get <- self.opts:
case value := <-self.set:
self.opts = value
case <-self.done:
case pbc.get <- pbc.opts:
case value := <-pbc.set:
pbc.opts = value
case <-pbc.done:
return
}
}
}()
}

func (self *ParserBenchChannel) Close() {
self.done <- true
func (pbc *ParserBenchChannel) Close() {
pbc.done <- true
}

func (self *ParserBenchChannel) SetOpts(values OptValues) {
self.set <- &Options{values}
func (pbc *ParserBenchChannel) SetOpts(values OptValues) {
pbc.set <- &Options{values}
}

func (self *ParserBenchChannel) GetOpts() *Options {
return <-self.get
func (pbc *ParserBenchChannel) GetOpts() *Options {
return <-pbc.get
}
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func hasFlags(src, flag ParseFlag) bool {

// Sets all the flags given on dest
func setFlags(dest *ParseFlag, flag ParseFlag) {
*dest = (*dest | flag)
*dest = *dest | flag
}

// Returns a curl command representation of the passed http.Request
Expand Down
Loading