Skip to content

Commit

Permalink
Ring instrumentantions (redis#1017)
Browse files Browse the repository at this point in the history
* Ring instrumentantions
  • Loading branch information
aspacca authored and vmihailenco committed Apr 24, 2019
1 parent 3227091 commit 97e6ed8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ func NewRing(opt *RingOptions) *Ring {

ring.process = ring.defaultProcess
ring.processPipeline = ring.defaultProcessPipeline
ring.cmdable.setProcessor(ring.Process)

ring.init()

for name, addr := range opt.Addrs {
clopt := opt.clientOptions()
Expand All @@ -374,6 +375,10 @@ func NewRing(opt *RingOptions) *Ring {
return ring
}

func (c *Ring) init() {
c.cmdable.setProcessor(c.Process)
}

func (c *Ring) Context() context.Context {
if c.ctx != nil {
return c.ctx
Expand All @@ -392,6 +397,8 @@ func (c *Ring) WithContext(ctx context.Context) *Ring {

func (c *Ring) clone() *Ring {
cp := *c
cp.init()

return &cp
}

Expand Down
22 changes: 22 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis_test

import (
"context"
"crypto/rand"
"fmt"
"net"
Expand Down Expand Up @@ -104,6 +105,27 @@ var _ = Describe("Redis Ring", func() {
Expect(ringShard2.Info("keyspace").Val()).To(ContainSubstring("keys=100"))
})

It("propagates process for WithContext", func() {
var fromWrap []string
wrapper := func(oldProcess func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) error {
fromWrap = append(fromWrap, cmd.Name())

return oldProcess(cmd)
}
}

ctx := context.Background()
ring = ring.WithContext(ctx)
ring.WrapProcess(wrapper)

ring.Ping()
Expect(fromWrap).To(Equal([]string{"ping"}))

ring.Ping()
Expect(fromWrap).To(Equal([]string{"ping", "ping"}))
})

Describe("pipeline", func() {
It("distributes keys", func() {
pipe := ring.Pipeline()
Expand Down

0 comments on commit 97e6ed8

Please sign in to comment.