头图

Redigo issue 579

reply.go sliceHelper does not handle Redis errors within the slice#579

1. What is the problem

In the scenario of using Envoy to proxy Redis, execute MGET to obtain data, one of the Redis services has a problem, and Envoy returns the following error message
upstream failure

And what comes back through redigo is

redigo: unexpected element type for ByteSlices, got type redis.Error

Obviously there is a problem with the encapsulation, resulting in the error prompt is not direct enough.

Second, how to solve

See pr 580

2.1 ByteSlices and MGET

Let's look at the example code of issue 579, and the call in question is here:

values, err := redis.ByteSlices(conn.Do("MGET", params...))

So where is the source of the problem?

This needs to see what redis.ByteSlices() has done.

Let me summarize directly: traverse all the data returned by MGET, make a type assertion, and return an error message if the assertion fails.

The problem then lies in the following two points:

  1. When Envoy proxy Redis, in the case of no response from redis or other situations, Envoy returns a custom error message. ( Envoy Documentation - upstream failure error )
  2. Redigo made a rough judgment and made a type assertion v.([]byte) on the returned value. The slice assertion failed, and a custom error message was returned, and the error message did not carry the content returned by Envoy.

2.2 switch and v.(type)

if v.([]byte) type assertion is a byte slice
transformed into
switch v := v.(type) case []byte: case Error:

Instead of judging the type of a single byte slice, assert which type it is.

So what type is Error ? It is actually String . We call this feature "type definition" type Error string

and Envoy proxy Redis returns the data returned in the error message scenario, the type assertion will enter case Error

Third, reproduce the problem

Install Envoy, proxy Redis, kill Redis, and you can reproduce "no upstream host" (although it is not upstream failure, but it is also String).

4. Expansion

4.1 What is Envoy mentioned in the article?

Implementation Framework of Service Mesh Architecture

reference
Container Design Patterns - sidcar

4.2 How do Envoy and Redis combine?

Under the Service Mesh architecture, Envoy proxies multiple Redis clusters.

reference
Envoy Redis source code analysis

4.3 Go

4.3.1 Type aliases, and type definitions

The Error mentioned in the article is actually String, that is, the type definition. It should be noted that it should not be confused with the concept of type alias.

// 将NewInt定义为int类型
type NewInt int

// 将int取一个别名叫IntAlias
type IntAlias = int

reference
Go language type keyword (type alias)
Understanding Go 1.9 type aliases

4.3.2 Callback function

Callbacks are used in redis.ByteSlices() and other fun

reference
cyent.github.io

image.png


sown
149 声望268 粉丝

学习不能停