1
1
package goredis8
2
2
3
3
import (
4
+ "sync"
5
+
4
6
"github.com/go-redis/redis/v8"
5
7
6
- trm "github.com/avito-tech/go-transaction-manager/trm/v2"
8
+ "github.com/avito-tech/go-transaction-manager/trm/v2"
7
9
)
8
10
9
11
const (
@@ -20,30 +22,33 @@ type Settings struct {
20
22
isMulti * bool
21
23
watchKeys []string
22
24
txDecorator []TxDecorator
23
- ret * []redis.Cmder
25
+
26
+ ret * []redis.Cmder
27
+ muRet sync.RWMutex
24
28
}
25
29
26
30
// NewSettings creates Settings.
27
- func NewSettings (trms trm.Settings , oo ... Opt ) (Settings , error ) {
31
+ func NewSettings (trms trm.Settings , oo ... Opt ) (* Settings , error ) {
28
32
s := & Settings {
29
33
Settings : trms ,
30
34
isMulti : nil ,
31
35
watchKeys : nil ,
32
36
txDecorator : nil ,
33
37
ret : nil ,
38
+ muRet : sync.RWMutex {},
34
39
}
35
40
36
41
for _ , o := range oo {
37
42
if err := o (s ); err != nil {
38
- return Settings {} , err
43
+ return nil , err
39
44
}
40
45
}
41
46
42
- return * s , nil
47
+ return s , nil
43
48
}
44
49
45
50
// MustSettings returns Settings if err is nil and panics otherwise.
46
- func MustSettings (trms trm.Settings , oo ... Opt ) Settings {
51
+ func MustSettings (trms trm.Settings , oo ... Opt ) * Settings {
47
52
s , err := NewSettings (trms , oo ... )
48
53
if err != nil {
49
54
panic (err )
@@ -53,8 +58,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings {
53
58
}
54
59
55
60
// EnrichBy fills nil properties from external Settings.
56
- func (s Settings ) EnrichBy (in trm.Settings ) trm.Settings {
57
- external , ok := in .(Settings )
61
+ func (s * Settings ) EnrichBy (in trm.Settings ) trm.Settings {
62
+ external , ok := in .(* Settings )
58
63
if ok {
59
64
if s .IsMultiOrNil () == nil {
60
65
s = s .SetIsMulti (external .IsMultiOrNil ())
@@ -68,8 +73,8 @@ func (s Settings) EnrichBy(in trm.Settings) trm.Settings {
68
73
s = s .SetTxDecorators (external .TxDecorators ()... )
69
74
}
70
75
71
- if s .Return () == nil {
72
- s = s .SetReturn (external .Return ())
76
+ if s .ReturnPtr () == nil {
77
+ s = s .SetReturn (external .ReturnPtr ())
73
78
}
74
79
}
75
80
@@ -79,7 +84,7 @@ func (s Settings) EnrichBy(in trm.Settings) trm.Settings {
79
84
}
80
85
81
86
// IsMulti - true uses redis MULTI cmd.
82
- func (s Settings ) IsMulti () bool {
87
+ func (s * Settings ) IsMulti () bool {
83
88
if s .isMulti == nil {
84
89
return DefaultMulti
85
90
}
@@ -88,64 +93,89 @@ func (s Settings) IsMulti() bool {
88
93
}
89
94
90
95
// IsMultiOrNil returns IsMulti or nil.
91
- func (s Settings ) IsMultiOrNil () * bool {
96
+ func (s * Settings ) IsMultiOrNil () * bool {
92
97
return s .isMulti
93
98
}
94
99
95
100
// SetIsMulti set using or not Multi for transaction, see https://redis.uptrace.dev/guide/go-redis-pipelines.html#transactions.
96
- func (s Settings ) SetIsMulti (in * bool ) Settings {
101
+ func (s * Settings ) SetIsMulti (in * bool ) * Settings {
97
102
return s .setIsMulti (in )
98
103
}
99
104
100
- func (s Settings ) setIsMulti (in * bool ) Settings {
105
+ func (s * Settings ) setIsMulti (in * bool ) * Settings {
101
106
s .isMulti = in
102
107
103
108
return s
104
109
}
105
110
106
111
// WatchKeys returns keys for watching.
107
- func (s Settings ) WatchKeys () []string {
112
+ func (s * Settings ) WatchKeys () []string {
108
113
return s .watchKeys
109
114
}
110
115
111
116
// SetWatchKeys sets keys for watching, see https://redis.uptrace.dev/guide/go-redis-pipelines.html#watch.
112
- func (s Settings ) SetWatchKeys (in []string ) Settings {
117
+ func (s * Settings ) SetWatchKeys (in []string ) * Settings {
113
118
return s .setWatchKeys (in )
114
119
}
115
120
116
- func (s Settings ) setWatchKeys (in []string ) Settings {
121
+ func (s * Settings ) setWatchKeys (in []string ) * Settings {
117
122
s .watchKeys = in
118
123
119
124
return s
120
125
}
121
126
122
127
// TxDecorators returns TxDecorator decorators.
123
- func (s Settings ) TxDecorators () []TxDecorator {
128
+ func (s * Settings ) TxDecorators () []TxDecorator {
124
129
return s .txDecorator
125
130
}
126
131
127
132
// SetTxDecorators sets TxDecorator decorators.
128
- func (s Settings ) SetTxDecorators (in ... TxDecorator ) Settings {
133
+ func (s * Settings ) SetTxDecorators (in ... TxDecorator ) * Settings {
129
134
return s .setTxDecorator (in ... )
130
135
}
131
136
132
- func (s Settings ) setTxDecorator (in ... TxDecorator ) Settings {
137
+ func (s * Settings ) setTxDecorator (in ... TxDecorator ) * Settings {
133
138
s .txDecorator = in
134
139
135
140
return s
136
141
}
137
142
138
- // Return returns []redis.Cmder from Transaction.
139
- func (s Settings ) Return () * []redis.Cmder {
143
+ // ReturnPtr returns link to save []redis.Cmder from Transaction.
144
+ func (s * Settings ) ReturnPtr () * []redis.Cmder {
145
+ s .muRet .RLock ()
146
+ defer s .muRet .RUnlock ()
147
+
140
148
return s .ret
141
149
}
142
150
151
+ // Return returns []redis.Cmder from Transaction.
152
+ func (s * Settings ) Return () []redis.Cmder {
153
+ res := s .ReturnPtr ()
154
+ if res != nil {
155
+ return * s .ReturnPtr ()
156
+ }
157
+
158
+ return nil
159
+ }
160
+
161
+ // AppendReturn append []redis.Cmder from Transaction.
162
+ func (s * Settings ) AppendReturn (cmds ... redis.Cmder ) {
163
+ if s .ReturnPtr () == nil {
164
+ return
165
+ }
166
+
167
+ s .muRet .Lock ()
168
+ defer s .muRet .Unlock ()
169
+
170
+ * s .ret = append (* s .ret , cmds ... )
171
+ }
172
+
143
173
// SetReturn sets link to save []redis.Cmder from Transaction.
144
- func (s Settings ) SetReturn (in * []redis.Cmder ) Settings {
174
+ func (s * Settings ) SetReturn (in * []redis.Cmder ) * Settings {
145
175
return s .setReturn (in )
146
176
}
147
177
148
- func (s Settings ) setReturn (in * []redis.Cmder ) Settings {
178
+ func (s * Settings ) setReturn (in * []redis.Cmder ) * Settings {
149
179
s .ret = in
150
180
151
181
return s
0 commit comments