-
Notifications
You must be signed in to change notification settings - Fork 165
/
tx.go
145 lines (115 loc) · 3.23 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package rueidiscompat
import (
"context"
"errors"
"time"
"unsafe"
"github.com/redis/rueidis"
)
var TxFailedErr = errors.New("redis: transaction failed")
var _ Pipeliner = (*TxPipeline)(nil)
type rePipeline = Pipeline
func newTxPipeline(real rueidis.Client) *TxPipeline {
return &TxPipeline{rePipeline: newPipeline(real)}
}
type TxPipeline struct {
*rePipeline
}
func (c *TxPipeline) Exec(ctx context.Context) ([]Cmder, error) {
p := c.comp.client.(*proxy)
if len(p.cmds) == 0 {
return nil, nil
}
rets := c.rets
cmds := p.cmds
c.rets = nil
p.cmds = nil
cmds = append(cmds, c.comp.client.B().Multi().Build(), c.comp.client.B().Exec().Build())
for i := len(cmds) - 2; i >= 1; i-- {
j := i - 1
cmds[j], cmds[i] = cmds[i], cmds[j]
}
resp := p.DoMulti(ctx, cmds...)
results, err := resp[len(resp)-1].ToArray()
if rueidis.IsRedisNil(err) {
err = TxFailedErr
}
for i, r := range results {
rets[i].from(*(*rueidis.RedisResult)(unsafe.Pointer(&proxyresult{
err: resp[i+1].NonRedisError(),
val: r,
})))
}
return rets, err
}
func (c *TxPipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
if err := fn(c); err != nil {
return nil, err
}
return c.Exec(ctx)
}
func (c *TxPipeline) Pipeline() Pipeliner {
return c
}
func (c *TxPipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
return c.Pipelined(ctx, fn)
}
func (c *TxPipeline) TxPipeline() Pipeliner {
return c
}
var _ rueidis.Client = (*txproxy)(nil)
type txproxy struct {
rueidis.CoreClient
}
func (p *txproxy) DoCache(_ context.Context, _ rueidis.Cacheable, _ time.Duration) (resp rueidis.RedisResult) {
panic("not implemented")
}
func (p *txproxy) DoMultiCache(_ context.Context, _ ...rueidis.CacheableTTL) (resp []rueidis.RedisResult) {
panic("not implemented")
}
func (p *txproxy) DoStream(_ context.Context, _ rueidis.Completed) rueidis.RedisResultStream {
panic("not implemented")
}
func (p *txproxy) DoMultiStream(_ context.Context, _ ...rueidis.Completed) rueidis.MultiRedisResultStream {
panic("not implemented")
}
func (p *txproxy) Dedicated(_ func(rueidis.DedicatedClient) error) (err error) {
panic("not implemented")
}
func (p *txproxy) Dedicate() (client rueidis.DedicatedClient, cancel func()) {
panic("not implemented")
}
func (p *txproxy) Nodes() map[string]rueidis.Client {
panic("not implemented")
}
type Tx interface {
CoreCmdable
Watch(ctx context.Context, keys ...string) *StatusCmd
Unwatch(ctx context.Context, keys ...string) *StatusCmd
Close(ctx context.Context) error
}
func newTx(client rueidis.DedicatedClient, cancel func()) *tx {
return &tx{CoreCmdable: NewAdapter(&txproxy{CoreClient: client}), cancel: cancel}
}
type tx struct {
CoreCmdable
cancel func()
}
func (t *tx) Watch(ctx context.Context, keys ...string) *StatusCmd {
ret := &StatusCmd{}
if len(keys) != 0 {
client := t.CoreCmdable.(*Compat).client
ret.from(client.Do(ctx, client.B().Watch().Key(keys...).Build()))
}
return ret
}
func (t *tx) Unwatch(ctx context.Context, _ ...string) *StatusCmd {
ret := &StatusCmd{}
client := t.CoreCmdable.(*Compat).client
ret.from(client.Do(ctx, client.B().Unwatch().Build()))
return ret
}
func (t *tx) Close(_ context.Context) error {
t.cancel()
return nil
}