Skip to content

Commit

Permalink
feat: rename DedicatedWire to Dedicated
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Dec 26, 2021
1 parent d2af541 commit 951d69c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,18 @@ unintentional write commands between WATCH and EXEC. Otherwise, the EXEC may not
The dedicated connection shares the same connection pool with blocking commands.

```golang
c.DedicatedWire(func(client client.DedicatedSingleClient) error {
c.Dedicated(func(client *client.DedicatedSingleClient) error {
// watch keys first
client.Do(ctx, c.Cmd.Watch().Key("k1", "k2").Build())
client.Do(ctx, client.Cmd.Watch().Key("k1", "k2").Build())
// perform read here
client.Do(ctx, c.Cmd.Mget().Key("k1", "k2").Build())
client.Do(ctx, client.Cmd.Mget().Key("k1", "k2").Build())
// perform write with MULTI EXEC
client.DoMulti(
ctx,
c.Cmd.Multi().Build(),
c.Cmd.Set().Key("k1").Value("1").Build(),
c.Cmd.Set().Key("k2").Value("2").Build(),
c.Cmd.Exec().Build(),
client.Cmd.Multi().Build(),
client.Cmd.Set().Key("k1").Value("1").Build(),
client.Cmd.Set().Key("k2").Value("2").Build(),
client.Cmd.Exec().Build(),
)
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *SingleClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time
return resp
}

func (c *SingleClient) DedicatedWire(fn func(*DedicatedSingleClient) error) (err error) {
func (c *SingleClient) Dedicated(fn func(*DedicatedSingleClient) error) (err error) {
wire := c.conn.Acquire()
err = fn(&DedicatedSingleClient{Cmd: c.Cmd, wire: wire})
c.conn.Store(wire)
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,16 @@ func TestSingleClient(t *testing.T) {
}
})

t.Run("DedicatedWire Err", func(t *testing.T) {
t.Run("Dedicated Err", func(t *testing.T) {
v := errors.New("fn err")
if err := client.DedicatedWire(func(client *DedicatedSingleClient) error {
if err := client.Dedicated(func(client *DedicatedSingleClient) error {
return v
}); err != v {
t.Fatalf("unexpected err %v", err)
}
})

t.Run("DedicatedWire Delegate", func(t *testing.T) {
t.Run("Dedicated Delegate", func(t *testing.T) {
w := &mock.Wire{
DoFn: func(cmd cmds.Completed) proto.Result {
return proto.NewResult(proto.Message{Type: '+', String: "Delegate"}, nil)
Expand All @@ -188,7 +188,7 @@ func TestSingleClient(t *testing.T) {
}
stored = true
}
if err := client.DedicatedWire(func(c *DedicatedSingleClient) error {
if err := client.Dedicated(func(c *DedicatedSingleClient) error {
if v, err := c.Do(context.Background(), client.Cmd.Get().Key("a").Build()).ToString(); err != nil || v != "Delegate" {
t.Fatalf("unexpected respone %v %v", v, err)
}
Expand All @@ -205,7 +205,7 @@ func TestSingleClient(t *testing.T) {
t.Fatalf("unexpected err %v", err)
}
if !stored {
t.Fatalf("DedicatedWire desn't put back the wire")
t.Fatalf("Dedicated desn't put back the wire")
}
})

Expand Down
4 changes: 2 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ ret:
return resp
}

func (c *ClusterClient) DedicatedWire(fn func(*DedicatedClusterClient) error) (err error) {
func (c *ClusterClient) Dedicated(fn func(*DedicatedClusterClient) error) (err error) {
dcc := &DedicatedClusterClient{Cmd: c.Cmd, client: c, slot: cmds.InitSlot}
err = fn(dcc)
dcc.release()
Expand Down Expand Up @@ -363,7 +363,7 @@ func (c *DedicatedClusterClient) check(slot uint16) {
if c.slot == cmds.InitSlot {
c.slot = slot
} else if c.slot != slot {
panic("cross slot command in DedicatedWire is prohibited")
panic("cross slot command in Dedicated is prohibited")
}
}

Expand Down
28 changes: 14 additions & 14 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,40 +185,40 @@ func TestClusterClient(t *testing.T) {
<-called
})

t.Run("DedicatedWire Err", func(t *testing.T) {
t.Run("Dedicated Err", func(t *testing.T) {
v := errors.New("fn err")
if err := client.DedicatedWire(func(client *DedicatedClusterClient) error {
if err := client.Dedicated(func(client *DedicatedClusterClient) error {
return v
}); err != v {
t.Fatalf("unexpected err %v", err)
}
})

t.Run("DedicatedWire No Slot Err", func(t *testing.T) {
t.Run("Dedicated No Slot Err", func(t *testing.T) {
defer func() {
if err := recover(); err != "the first command in DedicatedClusterClient should contain the slot key" {
t.Errorf("DedicatedWire should panic if no slot is selected")
t.Errorf("Dedicated should panic if no slot is selected")
}
}()
client.DedicatedWire(func(c *DedicatedClusterClient) error {
client.Dedicated(func(c *DedicatedClusterClient) error {
return c.Do(context.Background(), client.Cmd.Info().Build()).Error()
})
})

t.Run("DedicatedWire Cross Slot Err", func(t *testing.T) {
t.Run("Dedicated Cross Slot Err", func(t *testing.T) {
defer func() {
if err := recover(); err != "cross slot command in DedicatedWire is prohibited" {
t.Errorf("DedicatedWire should panic if cross slots is used")
if err := recover(); err != "cross slot command in Dedicated is prohibited" {
t.Errorf("Dedicated should panic if cross slots is used")
}
}()
m.AcquireFn = func() wire { return &mock.Wire{} }
client.DedicatedWire(func(c *DedicatedClusterClient) error {
client.Dedicated(func(c *DedicatedClusterClient) error {
c.Do(context.Background(), client.Cmd.Get().Key("a").Build()).Error()
return c.Do(context.Background(), client.Cmd.Get().Key("b").Build()).Error()
})
})

t.Run("DedicatedWire Delegate", func(t *testing.T) {
t.Run("Dedicated Delegate", func(t *testing.T) {
w := &mock.Wire{
DoFn: func(cmd cmds.Completed) proto.Result {
return proto.NewResult(proto.Message{Type: '+', String: "Delegate"}, nil)
Expand All @@ -237,7 +237,7 @@ func TestClusterClient(t *testing.T) {
}
stored = true
}
if err := client.DedicatedWire(func(c *DedicatedClusterClient) error {
if err := client.Dedicated(func(c *DedicatedClusterClient) error {
if v, err := c.Do(context.Background(), client.Cmd.Get().Key("a").Build()).ToString(); err != nil || v != "Delegate" {
t.Fatalf("unexpected respone %v %v", v, err)
}
Expand All @@ -254,7 +254,7 @@ func TestClusterClient(t *testing.T) {
t.Fatalf("unexpected err %v", err)
}
if !stored {
t.Fatalf("DedicatedWire desn't put back the wire")
t.Fatalf("Dedicated desn't put back the wire")
}
})

Expand Down Expand Up @@ -348,7 +348,7 @@ func TestClusterClientErr(t *testing.T) {
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if err := client.DedicatedWire(func(c *DedicatedClusterClient) error {
if err := client.Dedicated(func(c *DedicatedClusterClient) error {
return c.Do(context.Background(), client.Cmd.Get().Key("a").Build()).Error()
}); err != ErrNoSlot {
t.Fatalf("unexpected err %v", err)
Expand All @@ -365,7 +365,7 @@ func TestClusterClientErr(t *testing.T) {
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if err := client.DedicatedWire(func(c *DedicatedClusterClient) error {
if err := client.Dedicated(func(c *DedicatedClusterClient) error {
for _, v := range c.DoMulti(context.Background(), client.Cmd.Get().Key("a").Build()) {
if err := v.Error(); err != nil {
return err
Expand Down

0 comments on commit 951d69c

Please sign in to comment.