From 0af59d66c42a865dbea18ea692512cc2ae4a5874 Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Mon, 18 Mar 2019 14:10:12 -0500 Subject: [PATCH 1/8] updated watchbackend --- main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 9a79dbb5c..3d3862bfb 100644 --- a/main.go +++ b/main.go @@ -426,6 +426,8 @@ func watchBackend(cfg *config.Config, first chan bool) { last string svccfg string mancfg string + next strings.Builder + once sync.Once ) @@ -441,26 +443,28 @@ func watchBackend(cfg *config.Config, first chan bool) { // manual config overrides service config // order matters - next := svccfg + "\n" + mancfg - if next == last { + next.WriteString(svccfg) + next.WriteString("\n") + next.WriteString(mancfg) + if next.String() == last { continue } - aliases, err := route.ParseAliases(next) + aliases, err := route.ParseAliases(next.String()) if err != nil { log.Printf("[WARN]: %s", err) } registry.Default.Register(aliases) - t, err := route.NewTable(next) + t, err := route.NewTable(next.String()) if err != nil { log.Printf("[WARN] %s", err) continue } route.SetTable(t) - logRoutes(t, last, next, cfg.Log.RoutesFormat) - last = next - + logRoutes(t, last, next.String(), cfg.Log.RoutesFormat) + last = next.String() + next.Reset() once.Do(func() { close(first) }) } } From 212302deeeff3a6e767d05e948dd6ac3e3cbee7a Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Tue, 19 Mar 2019 08:28:43 -0500 Subject: [PATCH 2/8] updated watchbackend --- main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 3d3862bfb..891791563 100644 --- a/main.go +++ b/main.go @@ -443,9 +443,12 @@ func watchBackend(cfg *config.Config, first chan bool) { // manual config overrides service config // order matters + next.Reset() next.WriteString(svccfg) next.WriteString("\n") next.WriteString(mancfg) + a := next.String() + log.Print("[DEBUG] Route", a) if next.String() == last { continue } @@ -464,11 +467,11 @@ func watchBackend(cfg *config.Config, first chan bool) { route.SetTable(t) logRoutes(t, last, next.String(), cfg.Log.RoutesFormat) last = next.String() - next.Reset() once.Do(func() { close(first) }) } } + func watchNoRouteHTML(cfg *config.Config) { html := registry.Default.WatchNoRouteHTML() for { From f2a359bce44092b82a26706fa8dd792e7665579c Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Tue, 19 Mar 2019 09:19:26 -0500 Subject: [PATCH 3/8] updated --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 891791563..1ed0d7632 100644 --- a/main.go +++ b/main.go @@ -447,8 +447,6 @@ func watchBackend(cfg *config.Config, first chan bool) { next.WriteString(svccfg) next.WriteString("\n") next.WriteString(mancfg) - a := next.String() - log.Print("[DEBUG] Route", a) if next.String() == last { continue } From f39611c4c623b73861a93b5962323aae9af4874c Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Wed, 20 Mar 2019 15:58:27 -0500 Subject: [PATCH 4/8] updated to buffer read --- main.go | 12 ++++++------ route/parse_new.go | 25 ++++++++++++++----------- route/table.go | 5 ++++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 1ed0d7632..5e0441d04 100644 --- a/main.go +++ b/main.go @@ -75,7 +75,7 @@ func main() { // warn once so that it is at the beginning of the log // this will also start the reminder go routine if necessary. - WarnIfRunAsRoot(cfg.Insecure) + //WarnIfRunAsRoot(cfg.Insecure) // setup profiling if enabled var prof interface { @@ -136,7 +136,7 @@ func main() { startServers(cfg) // warn again so that it is visible in the terminal - WarnIfRunAsRoot(cfg.Insecure) + //WarnIfRunAsRoot(cfg.Insecure) exit.Wait() log.Print("[INFO] Down") @@ -426,7 +426,7 @@ func watchBackend(cfg *config.Config, first chan bool) { last string svccfg string mancfg string - next strings.Builder + next bytes.Buffer once sync.Once @@ -434,13 +434,14 @@ func watchBackend(cfg *config.Config, first chan bool) { svc := registry.Default.WatchServices() man := registry.Default.WatchManual() + next.Grow(250000000) for { + select { case svccfg = <-svc: case mancfg = <-man: } - // manual config overrides service config // order matters next.Reset() @@ -450,14 +451,13 @@ func watchBackend(cfg *config.Config, first chan bool) { if next.String() == last { continue } - aliases, err := route.ParseAliases(next.String()) if err != nil { log.Printf("[WARN]: %s", err) } registry.Default.Register(aliases) - t, err := route.NewTable(next.String()) + t, err := route.NewTable(next) if err != nil { log.Printf("[WARN] %s", err) continue diff --git a/route/parse_new.go b/route/parse_new.go index 1ea9d5486..095343968 100644 --- a/route/parse_new.go +++ b/route/parse_new.go @@ -1,6 +1,8 @@ package route import ( + "bufio" + "bytes" "errors" "fmt" "regexp" @@ -65,25 +67,26 @@ route weight service host/path weight w tags "tag1,tag2" // The commands are parsed in order and order matters. // Deleting a route that has not been created yet yields // a different result than the other way around. -func Parse(in string) (defs []*RouteDef, err error) { +func Parse(in bytes.Buffer) (defs []*RouteDef, err error) { var def *RouteDef - for i, s := range strings.Split(in, "\n") { + scanner := bufio.NewScanner(&in) + for scanner.Scan() { def, err = nil, nil - s = strings.TrimSpace(s) + result := strings.TrimSpace(scanner.Text()) switch { - case reComment.MatchString(s) || reBlankLine.MatchString(s): + case reComment.MatchString(result) || reBlankLine.MatchString(result): continue - case reRouteAdd.MatchString(s): - def, err = parseRouteAdd(s) - case reRouteDel.MatchString(s): - def, err = parseRouteDel(s) - case reRouteWeight.MatchString(s): - def, err = parseRouteWeight(s) + case reRouteAdd.MatchString(result): + def, err = parseRouteAdd(result) + case reRouteDel.MatchString(result): + def, err = parseRouteDel(result) + case reRouteWeight.MatchString(result): + def, err = parseRouteWeight(result) default: err = errors.New("syntax error: 'route' expected") } if err != nil { - return nil, fmt.Errorf("line %d: %s", i+1, err) + return nil, fmt.Errorf("line %d: %s", err) } defs = append(defs, def) } diff --git a/route/table.go b/route/table.go index 4e12fc182..e243ae660 100644 --- a/route/table.go +++ b/route/table.go @@ -7,6 +7,8 @@ import ( "log" "net/http" "net/url" + "os" + "runtime/pprof" "sort" "strings" "sync" @@ -108,7 +110,8 @@ func hostpath(prefix string) (host string, path string) { return p[0], "/" + p[1] } -func NewTable(s string) (t Table, err error) { +func NewTable(s bytes.Buffer) (t Table, err error) { + pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) defs, err := Parse(s) if err != nil { return nil, err From c49aecfa1f5c7985651d86f426591512c5c2585c Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Thu, 21 Mar 2019 10:53:12 -0500 Subject: [PATCH 5/8] updated --- route/table.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/route/table.go b/route/table.go index e243ae660..3e322bc54 100644 --- a/route/table.go +++ b/route/table.go @@ -7,8 +7,6 @@ import ( "log" "net/http" "net/url" - "os" - "runtime/pprof" "sort" "strings" "sync" @@ -111,7 +109,6 @@ func hostpath(prefix string) (host string, path string) { } func NewTable(s bytes.Buffer) (t Table, err error) { - pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) defs, err := Parse(s) if err != nil { return nil, err From 45db500c087a5f860c62a02357bb4e424c09b4eb Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Thu, 21 Mar 2019 10:55:36 -0500 Subject: [PATCH 6/8] re-enabled warnasroot --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5e0441d04..8977c4121 100644 --- a/main.go +++ b/main.go @@ -75,7 +75,7 @@ func main() { // warn once so that it is at the beginning of the log // this will also start the reminder go routine if necessary. - //WarnIfRunAsRoot(cfg.Insecure) + WarnIfRunAsRoot(cfg.Insecure) // setup profiling if enabled var prof interface { @@ -136,7 +136,7 @@ func main() { startServers(cfg) // warn again so that it is visible in the terminal - //WarnIfRunAsRoot(cfg.Insecure) + WarnIfRunAsRoot(cfg.Insecure) exit.Wait() log.Print("[INFO] Down") From 8b1189a9ab991de899972c2b5915b650f2c14809 Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Thu, 21 Mar 2019 10:56:12 -0500 Subject: [PATCH 7/8] go fmt --- main.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.go b/main.go index 8977c4121..80a6690da 100644 --- a/main.go +++ b/main.go @@ -426,8 +426,7 @@ func watchBackend(cfg *config.Config, first chan bool) { last string svccfg string mancfg string - next bytes.Buffer - + next bytes.Buffer once sync.Once ) @@ -469,7 +468,6 @@ func watchBackend(cfg *config.Config, first chan bool) { } } - func watchNoRouteHTML(cfg *config.Config) { html := registry.Default.WatchNoRouteHTML() for { From cc34117c193feea0589e911b7a3c1b8ffc5639ef Mon Sep 17 00:00:00 2001 From: "Michael.Murphy" Date: Mon, 25 Mar 2019 08:26:47 -0500 Subject: [PATCH 8/8] removed buffer grow --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 80a6690da..3d34d145c 100644 --- a/main.go +++ b/main.go @@ -433,7 +433,6 @@ func watchBackend(cfg *config.Config, first chan bool) { svc := registry.Default.WatchServices() man := registry.Default.WatchManual() - next.Grow(250000000) for {