-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add context version Do func? #310
Comments
Please give a description of the problem you are trying to solve in your application and how Redigo connections will solve that problem when given a context. |
OK. Suppose we have an component which has a package main
import (
"context"
"fmt"
"github.com/garyburd/redigo/redis"
)
type RedisConn struct {
redis.Conn
}
func (c *RedisConn) DoContext(ctx context.Context, cmd string, args ...interface{}) (r interface{}, err error) {
traceId := ctx.Value("trace-id")
fmt.Printf("trace-id:%s, cmd:%s, args:%v", traceId, cmd, args)
//return c.Conn.DoContext(ctx, cmd, args...)
return c.Conn.Do(cmd, args...)
}
func DialRedis(target string, opts ...redis.DialOption) (*RedisConn, error) {
c, err := redis.Dial("tcp", target, opts...)
if err != nil {
return nil, err
}
return &RedisConn{Conn: c}, nil
}
func Query(ctx context.Context, keyword string) (string, error) {
traceId := ctx.Value("trace-id")
// just to show we can log the things related to one spec tace-id
fmt.Printf("trace-id:%s", traceId)
// we don't use connpool here for demo
c, err := DialRedis("localhost:6389")
if err != nil {
return "", err
}
return redis.String(c.DoContext(ctx, "GET", keyword))
}
func main() {
key := "key"
ctx := context.WithValue(context.Background(), "trace-id", "0123456789")
if val, err := Query(ctx, key); err != nil {
fmt.Println(err)
} else {
fmt.Printf("key:%s, val:%s\n", key, val)
}
} As you can see from above, with |
The application works with no change to Redigo. Adding a method to redis.Conn is a breaking change. You can hide RedisConn from the application by following the pattern in NewLoggingConn.
Wrap a plain connection with the tracing connection wherever you need tracing. If you use a pool, then add the wrapper in a Get method wrapper:
|
Thanks, Got it:) |
Sometimes we need to add tracing(opentracing, for example) to redis call. but the current version only supply
Do
function which does not have a context.Context parameter, so is it reasonable to add aDoContext
function to redigo? just like the sql version here, thanks.The text was updated successfully, but these errors were encountered: