@@ -61,11 +61,13 @@ public function configure(
61
61
string $ userAgent ,
62
62
string $ apiKey ,
63
63
int $ cacheExpirationForCleanIp ,
64
- int $ cacheExpirationForBadIp
64
+ int $ cacheExpirationForBadIp ,
65
+ string $ fallbackRemediation
65
66
): void {
66
67
$ this ->liveMode = $ liveMode ;
67
68
$ this ->cacheExpirationForCleanIp = $ cacheExpirationForCleanIp ;
68
69
$ this ->cacheExpirationForBadIp = $ cacheExpirationForBadIp ;
70
+ $ this ->fallbackRemediation = $ fallbackRemediation ;
69
71
$ cacheConfigItem = $ this ->adapter ->getItem ('cacheConfig ' );
70
72
$ cacheConfig = $ cacheConfigItem ->get ();
71
73
$ this ->warmedUp = (\is_array ($ cacheConfig ) && isset ($ cacheConfig ['warmed_up ' ])
@@ -211,8 +213,9 @@ private function formatRemediationFromDecision(?array $decision): array
211
213
$ duration = time () + $ this ->cacheExpirationForCleanIp ;
212
214
if (!$ this ->liveMode ) {
213
215
// In stream mode we considere an clean IP forever... until the next resync.
214
- $ duration = PHP_INT_MAX ;
216
+ $ duration = \ PHP_INT_MAX ;
215
217
}
218
+
216
219
return [Constants::REMEDIATION_BYPASS , $ duration , 0 ];
217
220
}
218
221
@@ -257,8 +260,9 @@ private function saveRemediations(array $decisions): bool
257
260
return $ this ->commit ();
258
261
}
259
262
260
- private function removeRemediations (array $ decisions ): bool
263
+ private function removeRemediations (array $ decisions ): int
261
264
{
265
+ $ count = 0 ;
262
266
foreach ($ decisions as $ decision ) {
263
267
if (\is_int ($ decision ['start_ip ' ]) && \is_int ($ decision ['end_ip ' ])) {
264
268
$ ipRange = array_map ('long2ip ' , range ($ decision ['start_ip ' ], $ decision ['end_ip ' ]));
@@ -271,15 +275,19 @@ private function removeRemediations(array $decisions): bool
271
275
$ success = false ;
272
276
}
273
277
}
274
- if (!$ success ) {
278
+ if ($ success ) {
279
+ ++$ count ;
280
+ } else {
275
281
// The API may return stale deletion events due to API design.
276
282
// Ignoring them is therefore not a problem.
277
283
$ this ->logger ->debug ('' , ['type ' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE ' , 'decision ' => $ decision ['id ' ]]);
278
284
}
279
285
}
280
286
}
281
287
282
- return $ this ->commit ();
288
+ $ this ->commit ();
289
+
290
+ return $ count ;
283
291
}
284
292
285
293
/**
@@ -291,9 +299,8 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
291
299
if (\count ($ decisions )) {
292
300
foreach ($ decisions as $ decision ) {
293
301
if (!\in_array ($ decision ['type ' ], Constants::ORDERED_REMEDIATIONS )) {
294
- $ fallback = $ this ->config ['fallback_remediation ' ];
295
- $ this ->logger ->warning ('' , ['type ' => 'UNKNOWN_REMEDIATION ' , 'unknown ' => $ decision ['type ' ], 'fallback ' => $ fallback ]);
296
- $ decision ['type ' ] = $ fallback ;
302
+ $ this ->logger ->warning ('' , ['type ' => 'UNKNOWN_REMEDIATION ' , 'unknown ' => $ decision ['type ' ], 'fallback ' => $ this ->fallbackRemediation ]);
303
+ $ decision ['type ' ] = $ this ->fallbackRemediation ;
297
304
}
298
305
$ remediation = $ this ->formatRemediationFromDecision ($ decision );
299
306
$ remediationResult = $ this ->addRemediationToCacheItem ($ ip , $ remediation [0 ], $ remediation [1 ], $ remediation [2 ]);
@@ -380,8 +387,7 @@ public function pullUpdates(): array
380
387
381
388
$ nbDeleted = 0 ;
382
389
if ($ deletedDecisions ) {
383
- $ this ->removeRemediations ($ deletedDecisions );
384
- $ nbDeleted = \count ($ deletedDecisions );
390
+ $ nbDeleted = $ this ->removeRemediations ($ deletedDecisions );
385
391
}
386
392
387
393
$ nbNew = 0 ;
@@ -485,8 +491,8 @@ public function prune(): bool
485
491
private function setCustomErrorHandler (): void
486
492
{
487
493
if ($ this ->adapter instanceof MemcachedAdapter) {
488
- set_error_handler (function () {
489
- throw new BouncerException ('Error when connecting to Memcached. Please fix the Memcached DSN or select another cache technology. ' );
494
+ set_error_handler (function ($ errno , $ errmsg ) {
495
+ throw new BouncerException ('Error when connecting to Memcached. Please fix the Memcached DSN or select another cache technology. Original message was: ' . $ errmsg );
490
496
});
491
497
}
492
498
}
0 commit comments