Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CronSync.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Settings::configure([
'api_key' => 'put_your_key_here',
'db_path' => '/tmp/anticrawler.sqlite',
'visitor_forget_after' => 60 * 60 * 24 * 30,
// 'pending_requests_max_length' => 200000,
// 'requests_backend' => 'keydb',
// 'keydb_host' => '127.0.0.1',
// 'keydb_port' => 6379,
Expand Down
9 changes: 8 additions & 1 deletion KeyDBManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public static function storeRequest(RequestDto $request, ResultDto $result): voi
'request_status' => $result->status,
], JSON_UNESCAPED_SLASHES);

self::connectFromSettings()->rPush(self::pendingKey(), $payload === false ? '{}' : $payload);
$redis = self::connectFromSettings();
$redis->rPush(self::pendingKey(), $payload === false ? '{}' : $payload);
$redis->lTrim(self::pendingKey(), -self::pendingRequestsMaxLength(), -1);
}

public static function countPendingRequests(): int
Expand Down Expand Up @@ -218,4 +220,9 @@ private static function visitorTtl(): int
{
return max(1, (int)Settings::$visitorForgetAfter);
}

private static function pendingRequestsMaxLength(): int
{
return max(1, (int)Settings::$pendingRequestsMaxLength);
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ List of settings:
| min_sync_interval | int | Minimum time interval between synchronizations when using default sync behavior, in seconds |
| max_sync_interval | int | Maximum time interval between synchronizations when using default sync behavior, in seconds |
| visitor_forget_after | int | Time limit for storing visitor data in the library database, in seconds (decrease this if you have storage issues) |
| pending_requests_max_length | int | Maximum number of request entries retained in the KeyDB pending queue. Defaults to `100000`; older entries are trimmed away |
| max_rows_before_sync | int | Maximum number of requests stored between synchronizations when using default sync behavior |
| sync_by_cron | bool | Set this to true to use the cron synchronization mechanism. See `CronSync.php.example` |
| requests_backend | string | Backend switch for both request logs and visitor-state data: `sqlite` (default) or `keydb` |
Expand All @@ -93,5 +94,5 @@ SQLite can generate high I/O load when the traffic is high. If you see this, you
]);
```

In this mode, request logs and visitor presence data are stored in KeyDB. `visitor_forget_after` is applied as visitor key TTL.
In this mode, request logs and visitor presence data are stored in KeyDB. `visitor_forget_after` is applied as visitor key TTL, and `pending_requests_max_length` caps the pending request list by count.
SQLite is still used for `kv`, `lists`, and `user_agents` tables.
5 changes: 5 additions & 0 deletions Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Settings
/** @var int */
public static $visitorForgetAfter = 60 * 60 * 24 * 30; // One month

/** @var int */
public static $pendingRequestsMaxLength = 100000;

/** @var int */
public static $maxRowsBeforeSync = 20000;

Expand Down Expand Up @@ -69,6 +72,8 @@ public static function configure(array $options): void
self::$minSyncInterval = $options['min_sync_interval'] ?? self::$minSyncInterval;
self::$maxSyncInterval = $options['max_sync_interval'] ?? self::$maxSyncInterval;
self::$visitorForgetAfter = $options['visitor_forget_after'] ?? self::$visitorForgetAfter;
self::$pendingRequestsMaxLength = $options['pending_requests_max_length']
?? self::$pendingRequestsMaxLength;
self::$maxRowsBeforeSync = $options['max_rows_before_sync'] ?? self::$maxRowsBeforeSync;
self::$syncByCron = $options['sync_by_cron'] ?? self::$syncByCron;
self::$requestsBackend = $options['requests_backend'] ?? self::$requestsBackend;
Expand Down