Repository: zeromicro/go-zero
Pull request #3844 (2024-01-13 14:47:38)

chore: refactor redis

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3844?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`368caa7`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/368caa7608693ad231b66dfd926413de27f3d795?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.67% compared to head [(`fe6210d`)](https://app.codecov.io/gh/zeromicro/go-zero/pull/3844?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.67%.

<details><summary>Additional details and impacted files</summary>



| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3844?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [core/stores/redis/hook.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3844?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-Y29yZS9zdG9yZXMvcmVkaXMvaG9vay5nbw==) | `100.00% <100.00%> (ø)` | |

... and [2 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3844/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
diff --git a/core/stores/redis/hook.go b/core/stores/redis/hook.go
index 09c41b16..fca21498 100644
--- a/core/stores/redis/hook.go
+++ b/core/stores/redis/hook.go
@@ -2,6 +2,7 @@ package redis
 
 import (
 	"context"
+	"errors"
 	"io"
 	"net"
 	"strings"
@@ -29,7 +30,6 @@ var (
 type hook struct{}
 
 func (h hook) DialHook(next red.DialHook) red.DialHook {
-	// no need to implement
 	return next
 }
 
@@ -84,22 +84,23 @@ func (h hook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipel
 }
 
 func formatError(err error) string {
-	if err == nil || err == red.Nil {
+	if err == nil || errors.Is(err, red.Nil) {
 		return ""
 	}
 
-	opErr, ok := err.(*net.OpError)
+	var opErr *net.OpError
+	ok := errors.As(err, &opErr)
 	if ok && opErr.Timeout() {
 		return "timeout"
 	}
 
-	switch err {
-	case io.EOF:
+	switch {
+	case err == io.EOF:
 		return "eof"
-	case context.DeadlineExceeded:
+	case errors.Is(err, context.DeadlineExceeded):
 		return "context deadline"
-	case breaker.ErrServiceUnavailable:
-		return "breaker"
+	case errors.Is(err, breaker.ErrServiceUnavailable):
+		return "breaker open"
 	default:
 		return "unexpected error"
 	}
@@ -140,7 +141,7 @@ func (h hook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context
 	return ctx, func(err error) {
 		defer span.End()
 
-		if err == nil || err == red.Nil {
+		if err == nil || errors.Is(err, red.Nil) {
 			span.SetStatus(codes.Ok, "")
 			return
 		}


Test patch:
diff --git a/core/stores/redis/hook_test.go b/core/stores/redis/hook_test.go
index cf7146f6..44b2e8fa 100644
--- a/core/stores/redis/hook_test.go
+++ b/core/stores/redis/hook_test.go
@@ -138,7 +138,7 @@ func TestFormatError(t *testing.T) {
 	assert.Equal(t, "context deadline", formatError(context.DeadlineExceeded))
 
 	// Test case: err is breaker.ErrServiceUnavailable
-	assert.Equal(t, "breaker", formatError(breaker.ErrServiceUnavailable))
+	assert.Equal(t, "breaker open", formatError(breaker.ErrServiceUnavailable))
 
 	// Test case: err is unknown
 	assert.Equal(t, "unexpected error", formatError(errors.New("some error")))