Skip to content

Commit 60b748b

Browse files
manisharmamanishndyakov
authored
fix(sentinel): handle empty address (#3577)
* improvements * linter fixes * prevention on unnecessary allocations in case of bad configuration * Test/Benchmark, old code with safety harness preventing panic --------- Co-authored-by: manish <manish.sharma@manifestit.io> Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
1 parent 284d93a commit 60b748b

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

export_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,7 @@ func (c *ModuleLoadexConfig) ToArgs() []interface{} {
106106
func ShouldRetry(err error, retryTimeout bool) bool {
107107
return shouldRetry(err, retryTimeout)
108108
}
109+
110+
func JoinErrors(errs []error) string {
111+
return joinErrors(errs)
112+
}

sentinel.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,11 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
843843
}
844844
}
845845

846+
// short circuit if no sentinels configured
847+
if len(c.sentinelAddrs) == 0 {
848+
return "", errors.New("redis: no sentinels configured")
849+
}
850+
846851
var (
847852
masterAddr string
848853
wg sync.WaitGroup
@@ -890,10 +895,12 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
890895
}
891896

892897
func joinErrors(errs []error) string {
898+
if len(errs) == 0 {
899+
return ""
900+
}
893901
if len(errs) == 1 {
894902
return errs[0].Error()
895903
}
896-
897904
b := []byte(errs[0].Error())
898905
for _, err := range errs[1:] {
899906
b = append(b, '\n')

sentinel_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,99 @@ func compareSlices(t *testing.T, a, b []string, name string) {
682682
}
683683
}
684684
}
685+
686+
type joinErrorsTest struct {
687+
name string
688+
errs []error
689+
expected string
690+
}
691+
692+
func TestJoinErrors(t *testing.T) {
693+
tests := []joinErrorsTest{
694+
{
695+
name: "empty slice",
696+
errs: []error{},
697+
expected: "",
698+
},
699+
{
700+
name: "single error",
701+
errs: []error{errors.New("first error")},
702+
expected: "first error",
703+
},
704+
{
705+
name: "two errors",
706+
errs: []error{errors.New("first error"), errors.New("second error")},
707+
expected: "first error\nsecond error",
708+
},
709+
{
710+
name: "multiple errors",
711+
errs: []error{
712+
errors.New("first error"),
713+
errors.New("second error"),
714+
errors.New("third error"),
715+
},
716+
expected: "first error\nsecond error\nthird error",
717+
},
718+
{
719+
name: "nil slice",
720+
errs: nil,
721+
expected: "",
722+
},
723+
}
724+
725+
for _, tt := range tests {
726+
t.Run(tt.name, func(t *testing.T) {
727+
result := redis.JoinErrors(tt.errs)
728+
if result != tt.expected {
729+
t.Errorf("joinErrors() = %q, want %q", result, tt.expected)
730+
}
731+
})
732+
}
733+
}
734+
735+
func BenchmarkJoinErrors(b *testing.B) {
736+
benchmarks := []joinErrorsTest{
737+
{
738+
name: "empty slice",
739+
errs: []error{},
740+
expected: "",
741+
},
742+
{
743+
name: "single error",
744+
errs: []error{errors.New("first error")},
745+
expected: "first error",
746+
},
747+
{
748+
name: "two errors",
749+
errs: []error{errors.New("first error"), errors.New("second error")},
750+
expected: "first error\nsecond error",
751+
},
752+
{
753+
name: "multiple errors",
754+
errs: []error{
755+
errors.New("first error"),
756+
errors.New("second error"),
757+
errors.New("third error"),
758+
},
759+
expected: "first error\nsecond error\nthird error",
760+
},
761+
{
762+
name: "nil slice",
763+
errs: nil,
764+
expected: "",
765+
},
766+
}
767+
for _, bm := range benchmarks {
768+
b.Run(bm.name, func(b *testing.B) {
769+
b.ResetTimer()
770+
b.RunParallel(func(pb *testing.PB) {
771+
for pb.Next() {
772+
result := redis.JoinErrors(bm.errs)
773+
if result != bm.expected {
774+
b.Errorf("joinErrors() = %q, want %q", result, bm.expected)
775+
}
776+
}
777+
})
778+
})
779+
}
780+
}

0 commit comments

Comments
 (0)