-
Notifications
You must be signed in to change notification settings - Fork 194
refactor(backend): improve block page performance by reusing Sieve connections #1604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shadow243
wants to merge
9
commits into
cypht-org:master
Choose a base branch
from
Shadow243:refactor-block-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ef5123
refactor(backend): improve block page performance by reusing Sieve co…
Shadow243 daf2e0a
fix(backend): adding sieve pool for authentication
Shadow243 fd33a98
fix(backend): refactor sieve pool with cypht cache class
Shadow243 115c4e1
fix(backend): refactor sieve pool usage testing with postale
Shadow243 9168e58
fix(backend): refactor sieve cache class and separate it from connection
Shadow243 a024f8a
fix(backend): refactor sieve filter module to use SieveService
Shadow243 9b6a033
fix(backend): simplify initialization and resolve related issues
Shadow243 b9d33c2
test(backend): add unit tests for SieveScriptCache and SieveConnectio…
Shadow243 7a0bea3
fix(backend): update SieveService initialization to handle host and p…
Shadow243 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php | ||
| ini_set('memory_limit', '2048M'); | ||
kroky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| use PhpSieveManager\ManageSieve\Client; | ||
| class SieveConnectionPool | ||
kroky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private static $config = []; | ||
| private static $connections = []; | ||
| private static $lastConnectionTimes = []; | ||
| /** | ||
| * Default timeout for connections in seconds. | ||
| */ | ||
| private static $timeout = 600; | ||
|
|
||
| /** | ||
| * Cache TTL in seconds | ||
| */ | ||
| private static $scriptCacheTTL = 600; | ||
|
|
||
| /** | ||
| * Cache instance (injected or global) | ||
| */ | ||
| private static $cache = null; | ||
|
|
||
| private function __construct() {} | ||
|
|
||
| /** | ||
| * Optionally inject a global cache instance (Hm_Cache) | ||
| */ | ||
| public static function setCache($cacheInstance) | ||
| { | ||
| if(!self::$cache) { | ||
| self::$cache = $cacheInstance; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the configuration for all servers | ||
| */ | ||
| public static function setConfig(array $servers) | ||
| { | ||
| if (empty($servers)) { | ||
| throw new Exception("Configuration cannot be empty."); | ||
| } | ||
| foreach ($servers as $key => $server) { | ||
| if (!isset($server['host'], $server['port'], $server['username'], $server['password'])) { | ||
| throw new Exception("Each server configuration must include 'host', 'port', 'username', and 'password'."); | ||
| } | ||
| if (!is_string($server['host']) || !is_int($server['port']) || !is_string($server['username']) || !is_string($server['password'])) { | ||
| throw new Exception("Invalid types in server configuration for '$key'."); | ||
| } | ||
| if (isset(self::$config[$key])) { | ||
| self::$config[$key] = array_merge(self::$config[$key], $server); | ||
| } else { | ||
| self::$config[$key] = $server; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get a Sieve connection by its key (server identifier) | ||
| */ | ||
| public static function get($key) | ||
| { | ||
| if (!isset(self::$config[$key])) { | ||
| throw new Exception("No configuration found for '$key'"); | ||
| } | ||
|
|
||
| if (!isset(self::$connections[$key]) || !self::isAlive($key)) { | ||
| self::$connections[$key] = self::connectServer(self::$config[$key]); | ||
| self::$lastConnectionTimes[$key] = time(); | ||
| } | ||
|
|
||
| return self::$connections[$key]; | ||
kroky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Get a script by name for a given connection key with persistent caching | ||
| */ | ||
| public static function getScript($key, string $scriptName) | ||
| { | ||
| if (!self::$cache) { | ||
| throw new Exception("Cache instance not set. Call SieveConnectionPool::setCache() first."); | ||
| } | ||
|
|
||
| $cacheKey = "sieve_script_{$key}_{$scriptName}"; | ||
|
|
||
| // Try to fetch from persistent cache | ||
| $cached = self::$cache->get($cacheKey, false, true); | ||
|
|
||
| if ($cached && isset($cached['time']) && (time() - $cached['time']) < self::$scriptCacheTTL) { | ||
| return $cached['data']; | ||
| } | ||
|
|
||
| // Cache miss — fetch from server | ||
| $client = self::get($key); | ||
| $script = $client->getScript($scriptName); | ||
|
|
||
| // Save into persistent cache | ||
| self::$cache->set($cacheKey, [ | ||
| 'data' => $script, | ||
| 'time' => time() | ||
| ], self::$scriptCacheTTL, true); | ||
|
|
||
| return $script; | ||
| } | ||
|
|
||
| /** | ||
| * Optional: clear cached script | ||
| */ | ||
| public static function clearScriptCache($key, string $scriptName) | ||
| { | ||
| if (!self::$cache) { | ||
| return false; | ||
| } | ||
| $cacheKey = "sieve_script_{$key}_{$scriptName}"; | ||
| return self::$cache->del($cacheKey); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the connection is still alive based on timeout | ||
| */ | ||
| private static function isAlive($key) | ||
| { | ||
| return isset(self::$lastConnectionTimes[$key]) && | ||
| (time() - self::$lastConnectionTimes[$key]) < self::$timeout; | ||
| } | ||
|
|
||
| /** | ||
| * Establish the Sieve connection | ||
| */ | ||
| private static function connectServer(array $serverConfig) | ||
| { | ||
| $client = new Client($serverConfig['host'], $serverConfig['port']); | ||
| $client->connect( | ||
kroky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $serverConfig['username'], | ||
| $serverConfig['password'], | ||
| $serverConfig['secure'] ?? true, | ||
| "", | ||
| $serverConfig['authType'] ?? "PLAIN" | ||
| ); | ||
| return $client; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.