Skip to content

Commit

Permalink
refactor zkRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
zhachen committed Jul 31, 2018
1 parent 55d4a38 commit a1d0691
Show file tree
Hide file tree
Showing 5 changed files with 849 additions and 292 deletions.
24 changes: 24 additions & 0 deletions core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,27 @@ func HandlePanic(f func()) {
}
}
}

// Split slices s into all substrings separated by sep and
// returns a slice of the substrings between those separators,
// specially trim all substrings.
func TrimSplit(s string, sep string) []string {
n := strings.Count(s, sep) + 1
a := make([]string, n)
i := 0
if sep == "" {
return strings.Split(s, sep)
}
for {
m := strings.Index(s, sep)
if m < 0 {
s = strings.TrimSpace(s)
break
}
a[i] = strings.TrimSpace(s[:m])
i++
s = s[m+len(sep):]
}
a[i] = s
return a[:i+1]
}
25 changes: 25 additions & 0 deletions core/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)

func TestParseExportString(t *testing.T) {
Expand Down Expand Up @@ -75,3 +76,27 @@ func TestHandlePanic(t *testing.T) {
})
panic("test panic")
}

func TestSplitTrim(t *testing.T) {
type SplitTest struct {
str string
sep string
expect []string
}
space := "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
var splitList = []SplitTest{
{"", "", []string{}},
{"abcd", "", []string{"a", "b", "c", "d"}},
{"☺☻☹", "", []string{"☺", "☻", "☹"}},
{"abcd", "a", []string{"", "bcd"}},
{"abcd", "z", []string{"abcd"}},
{space + "1....2....3....4" + space, "...", []string{"1", ".2", ".3", ".4"}},
{"☺☻☹", "☹", []string{"☺☻", ""}},
{"1\t " + space + "\n2\t", " ", []string{"1", "2"}},
{"fd , fds, ,df\n, \v\ff ds ,,fd s , fds ,", ",", []string{"fd", "fds", "", "df", "f ds", "", "fd s", "fds", ""}},
}
for _, tt := range splitList {
ret := TrimSplit(tt.str, tt.sep)
assert.Equal(t, tt.expect, ret)
}
}
2 changes: 1 addition & 1 deletion ha/failoverHA.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (f *FailOverHA) Call(request motan.Request, loadBalance motan.LoadBalance)
lastErr = respnose.GetException()
vlog.Warningf("FailOverHA call fail! url:%s, err:%+v\n", ep.GetURL().GetIdentity(), lastErr)
}
return getErrorResponse(request.GetRequestID(), fmt.Sprintf("call fail over %d times.Exception:%s", retries, lastErr.ErrMsg))
return getErrorResponse(request.GetRequestID(), fmt.Sprintf("FailOverHA call fail %d times.Exception:%s", retries+1, lastErr.ErrMsg))

}

Expand Down
Loading

0 comments on commit a1d0691

Please sign in to comment.