Skip to content

Commit

Permalink
Fix Memory Leak in WatchBackend - fixes #595
Browse files Browse the repository at this point in the history
  • Loading branch information
murphymj25 authored and aaronhurt committed Apr 17, 2019
1 parent efac452 commit 65b31ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
20 changes: 10 additions & 10 deletions main.go
Expand Up @@ -432,6 +432,7 @@ func watchBackend(cfg *config.Config, first chan bool) {
svccfg string
mancfg string
customBE string
next bytes.Buffer

once sync.Once
)
Expand All @@ -454,19 +455,21 @@ func watchBackend(cfg *config.Config, first chan bool) {
man := registry.Default.WatchManual()

for {

select {
case svccfg = <-svc:
case mancfg = <-man:
}

// manual config overrides service config
// order matters
next := svccfg + "\n" + mancfg
if next == last {
next.Reset()
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)
}
Expand All @@ -478,14 +481,11 @@ func watchBackend(cfg *config.Config, first chan bool) {
continue
}
route.SetTable(t)
logRoutes(t, last, next, cfg.Log.RoutesFormat)
last = next

logRoutes(t, last, next.String(), cfg.Log.RoutesFormat)
last = next.String()
once.Do(func() { close(first) })
}

}

}

func watchNoRouteHTML(cfg *config.Config) {
Expand Down
25 changes: 14 additions & 11 deletions route/parse_new.go
@@ -1,6 +1,8 @@
package route

import (
"bufio"
"bytes"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions route/table.go
Expand Up @@ -108,8 +108,7 @@ 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) {
defs, err := Parse(s)
if err != nil {
return nil, err
Expand Down

0 comments on commit 65b31ca

Please sign in to comment.