|
15 | 15 | package redis |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
18 | 19 | "crypto/sha1" |
19 | 20 | "encoding/hex" |
20 | 21 | "io" |
21 | 22 | "strings" |
| 23 | + |
| 24 | + "go.opencensus.io/trace" |
22 | 25 | ) |
23 | 26 |
|
24 | 27 | // Script encapsulates the source, hash and key count for a Lua script. See |
@@ -64,10 +67,22 @@ func (s *Script) Hash() string { |
64 | 67 | // script using the EVALSHA command. If the command fails because the script is |
65 | 68 | // not loaded, then Do evaluates the script using the EVAL command (thus |
66 | 69 | // causing the script to load). |
67 | | -func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) { |
68 | | - v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...) |
| 70 | +func (s *Script) Do(c ConnWithContext, keysAndArgs ...interface{}) (interface{}, error) { |
| 71 | + return s.DoContext(context.Background(), c, keysAndArgs...) |
| 72 | +} |
| 73 | + |
| 74 | +// DoContext evaluates the script. Under the covers, DoContext optimistically evaluates the |
| 75 | +// script using the EVALSHA command. If the command fails because the script is |
| 76 | +// not loaded, then DoContext evaluates the script using the EVAL command (thus |
| 77 | +// causing the script to load). |
| 78 | +func (s *Script) DoContext(ctx context.Context, c ConnWithContext, keysAndArgs ...interface{}) (interface{}, error) { |
| 79 | + ctx, span := trace.StartSpan(ctx, "redis.(*Script).DoContext") |
| 80 | + defer span.End() |
| 81 | + |
| 82 | + v, err := c.DoContext(ctx, "EVALSHA", s.args(s.hash, keysAndArgs)...) |
69 | 83 | if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") { |
70 | | - v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...) |
| 84 | + span.Annotate(nil, "missed script cache") |
| 85 | + v, err = c.DoContext(ctx, "EVAL", s.args(s.src, keysAndArgs)...) |
71 | 86 | } |
72 | 87 | return v, err |
73 | 88 | } |
|
0 commit comments