8
8
namespace Magento \Framework \App \Backpressure \SlidingWindow ;
9
9
10
10
use Magento \Framework \App \Backpressure \ContextInterface ;
11
- use Credis_Client ;
12
11
use Magento \Framework \App \DeploymentConfig ;
13
12
use Magento \Framework \Exception \FileSystemException ;
14
13
use Magento \Framework \Exception \RuntimeException ;
14
+ use Magento \Framework \App \Backpressure \SlidingWindow \RedisRequestLogger \RedisClient ;
15
15
16
16
/**
17
17
* Logging requests to Redis
18
18
*/
19
19
class RedisRequestLogger implements RequestLoggerInterface
20
20
{
21
- /**
22
- * Keys for Redis settings
23
- */
24
- public const KEY_HOST = 'host ' ;
25
- public const KEY_PORT = 'port ' ;
26
- public const KEY_TIMEOUT = 'timeout ' ;
27
- public const KEY_PERSISTENT = 'persistent ' ;
28
- public const KEY_DB = 'db ' ;
29
- public const KEY_PASSWORD = 'password ' ;
30
- public const KEY_USER = 'user ' ;
31
-
32
- /**
33
- * Configuration paths for Redis settings
34
- */
35
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_SERVER = 'backpressure/logger/options/server ' ;
36
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PORT = 'backpressure/logger/options/port ' ;
37
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_TIMEOUT = 'backpressure/logger/options/timeout ' ;
38
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PERSISTENT = 'backpressure/logger/options/persistent ' ;
39
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_DB = 'backpressure/logger/options/db ' ;
40
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PASSWORD = 'backpressure/logger/options/password ' ;
41
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_USER = 'backpressure/logger/options/user ' ;
42
- public const CONFIG_PATH_BACKPRESSURE_LOGGER_ID_PREFIX = 'backpressure/logger/id-prefix ' ;
43
-
44
21
/**
45
22
* Identifier for Redis Logger type
46
23
*/
47
- public const VALUE_BACKPRESSURE_LOGGER_REDIS = 'redis ' ;
24
+ public const BACKPRESSURE_LOGGER_REDIS = 'redis ' ;
48
25
49
26
/**
50
- * Redis default settings
27
+ * Default prefix id
51
28
*/
52
- public const DEFAULT_REDIS_CONFIG_VALUES = [
53
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_SERVER => '127.0.0.1 ' ,
54
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PORT => 6379 ,
55
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_TIMEOUT => null ,
56
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PERSISTENT => '' ,
57
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_DB => 3 ,
58
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PASSWORD => null ,
59
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_USER => null ,
60
- ];
29
+ private const DEFAULT_PREFIX_ID = 'reqlog ' ;
61
30
62
31
/**
63
- * Config map
32
+ * Config path for backpressure logger id prefix
64
33
*/
65
- public const KEY_CONFIG_PATH_MAP = [
66
- self ::KEY_HOST => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_SERVER ,
67
- self ::KEY_PORT => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PORT ,
68
- self ::KEY_TIMEOUT => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_TIMEOUT ,
69
- self ::KEY_PERSISTENT => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PERSISTENT ,
70
- self ::KEY_DB => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_DB ,
71
- self ::KEY_PASSWORD => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PASSWORD ,
72
- self ::KEY_USER => self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_USER ,
73
- ];
34
+ public const CONFIG_PATH_BACKPRESSURE_LOGGER_ID_PREFIX = 'backpressure/logger/id-prefix ' ;
74
35
75
36
/**
76
- * @var Credis_Client
37
+ * @var RedisClient
77
38
*/
78
- private Credis_Client $ credisClient ;
39
+ private $ redisClient ;
79
40
80
41
/**
81
- * @var string
42
+ * @var DeploymentConfig
82
43
*/
83
- private string $ prefixId ;
44
+ private $ deploymentConfig ;
84
45
85
46
/**
86
- * @param DeploymentConfig $config
87
- * @throws FileSystemException
88
- * @throws RuntimeException
47
+ * @param RedisClient $redisClient
48
+ * @param DeploymentConfig $deploymentConfig
89
49
*/
90
- public function __construct (DeploymentConfig $ config )
91
- {
92
- $ this ->credisClient = new Credis_Client (
93
- $ this ->getHost ($ config ),
94
- $ this ->getPort ($ config ),
95
- $ this ->getTimeout ($ config ),
96
- $ this ->getPersistent ($ config ),
97
- $ this ->getDb ($ config ),
98
- $ this ->getPassword ($ config ),
99
- $ this ->getUser ($ config )
100
- );
101
-
102
- $ this ->prefixId = $ config ->get (self ::CONFIG_PATH_BACKPRESSURE_LOGGER_ID_PREFIX , '' );
50
+ public function __construct (
51
+ RedisClient $ redisClient ,
52
+ DeploymentConfig $ deploymentConfig
53
+ ) {
54
+ $ this ->redisClient = $ redisClient ;
55
+ $ this ->deploymentConfig = $ deploymentConfig ;
103
56
}
104
57
105
58
/**
@@ -108,20 +61,18 @@ public function __construct(DeploymentConfig $config)
108
61
public function incrAndGetFor (ContextInterface $ context , int $ timeSlot , int $ discardAfter ): int
109
62
{
110
63
$ id = $ this ->generateId ($ context , $ timeSlot );
111
- $ redis = $ this ->credisClient ->pipeline ();
112
-
113
- $ redis ->incrBy ($ id , 1 );
114
- $ redis ->expireAt ($ id , time () + $ discardAfter );
64
+ $ this ->redisClient ->incrBy ($ id , 1 );
65
+ $ this ->redisClient ->expireAt ($ id , time () + $ discardAfter );
115
66
116
- return (int )$ redis ->exec ()[0 ];
67
+ return (int )$ this -> redisClient ->exec ()[0 ];
117
68
}
118
69
119
70
/**
120
71
* @inheritDoc
121
72
*/
122
73
public function getFor (ContextInterface $ context , int $ timeSlot ): ?int
123
74
{
124
- $ value = $ this ->credisClient ->get ($ this ->generateId ($ context , $ timeSlot ));
75
+ $ value = $ this ->redisClient ->get ($ this ->generateId ($ context , $ timeSlot ));
125
76
126
77
return $ value ? (int )$ value : null ;
127
78
}
@@ -135,122 +86,26 @@ public function getFor(ContextInterface $context, int $timeSlot): ?int
135
86
*/
136
87
private function generateId (ContextInterface $ context , int $ timeSlot ): string
137
88
{
138
- return $ this ->prefixId
89
+ return $ this ->getPrefixId ()
139
90
. $ context ->getTypeId ()
140
91
. $ context ->getIdentityType ()
141
92
. $ context ->getIdentity ()
142
93
. $ timeSlot ;
143
94
}
144
95
145
96
/**
146
- * Returns Redis host
147
- *
148
- * @param DeploymentConfig $config
149
- * @return string
150
- * @throws FileSystemException
151
- * @throws RuntimeException
152
- */
153
- private function getHost (DeploymentConfig $ config ): string
154
- {
155
- return $ config ->get (
156
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_SERVER ,
157
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_SERVER ]
158
- );
159
- }
160
-
161
- /**
162
- * Returns Redis port
163
- *
164
- * @param DeploymentConfig $config
165
- * @return int
166
- * @throws FileSystemException
167
- * @throws RuntimeException
168
- */
169
- private function getPort (DeploymentConfig $ config ): int
170
- {
171
- return (int )$ config ->get (
172
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PORT ,
173
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PORT ]
174
- );
175
- }
176
-
177
- /**
178
- * Returns Redis timeout
97
+ * Returns prefix id
179
98
*
180
- * @param DeploymentConfig $config
181
- * @return float|null
182
- * @throws FileSystemException
183
- * @throws RuntimeException
184
- */
185
- private function getTimeout (DeploymentConfig $ config ): ?float
186
- {
187
- return (float )$ config ->get (
188
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_TIMEOUT ,
189
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_TIMEOUT ]
190
- );
191
- }
192
-
193
- /**
194
- * Returns Redis persistent
195
- *
196
- * @param DeploymentConfig $config
197
99
* @return string
198
- * @throws FileSystemException
199
- * @throws RuntimeException
200
- */
201
- private function getPersistent (DeploymentConfig $ config ): string
202
- {
203
- return $ config ->get (
204
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PERSISTENT ,
205
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PERSISTENT ]
206
- );
207
- }
208
-
209
- /**
210
- * Returns Redis db
211
- *
212
- * @param DeploymentConfig $config
213
- * @return int
214
- * @throws FileSystemException
215
- * @throws RuntimeException
216
- */
217
- private function getDb (DeploymentConfig $ config ): int
218
- {
219
- return (int )$ config ->get (
220
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_DB ,
221
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_DB ]
222
- );
223
- }
224
-
225
- /**
226
- * Returns Redis password
227
- *
228
- * @param DeploymentConfig $config
229
- * @return string|null
230
- * @throws FileSystemException
231
- * @throws RuntimeException
232
- */
233
- private function getPassword (DeploymentConfig $ config ): ?string
234
- {
235
- return $ config ->get (
236
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PASSWORD ,
237
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_PASSWORD ]
238
- );
239
- }
240
-
241
- /**
242
- * Returns Redis user
243
- *
244
- * @param DeploymentConfig $config
245
- * @return string|null
246
- * @throws FileSystemException
247
- * @throws RuntimeException
248
100
*/
249
- private function getUser ( DeploymentConfig $ config ): ? string
101
+ private function getPrefixId ( ): string
250
102
{
251
- return $ config ->get (
252
- self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_USER ,
253
- self ::DEFAULT_REDIS_CONFIG_VALUES [self ::CONFIG_PATH_BACKPRESSURE_LOGGER_REDIS_USER ]
254
- );
103
+ try {
104
+ return (string )$ this ->deploymentConfig ->get (
105
+ self ::CONFIG_PATH_BACKPRESSURE_LOGGER_ID_PREFIX ,
106
+ self ::DEFAULT_PREFIX_ID );
107
+ } catch (RuntimeException | FileSystemException $ e ) {
108
+ return self ::DEFAULT_PREFIX_ID ;
109
+ }
255
110
}
256
111
}
0 commit comments