Skip to content

Commit 70bb3d3

Browse files
Merge pull request #12896 from nextcloud/fix/imap/log-number-connections-sync
fix(imap): log number of connections during CLI sync
2 parents a6f7d9f + d9c240c commit 70bb3d3

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/Command/SyncAccount.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Mail\Account;
1313
use OCA\Mail\Exception\IncompleteSyncException;
1414
use OCA\Mail\Exception\ServiceException;
15+
use OCA\Mail\IMAP\IMAPClientFactory;
1516
use OCA\Mail\IMAP\MailboxSync;
1617
use OCA\Mail\Service\AccountService;
1718
use OCA\Mail\Service\Sync\ImapToDbSynchronizer;
@@ -34,17 +35,20 @@ final class SyncAccount extends Command {
3435
private MailboxSync $mailboxSync;
3536
private ImapToDbSynchronizer $syncService;
3637
private LoggerInterface $logger;
38+
private IMAPClientFactory $clientFactory;
3739

3840
public function __construct(AccountService $service,
3941
MailboxSync $mailboxSync,
4042
ImapToDbSynchronizer $messageSync,
41-
LoggerInterface $logger) {
43+
LoggerInterface $logger,
44+
IMAPClientFactory $clientFactory) {
4245
parent::__construct();
4346

4447
$this->accountService = $service;
4548
$this->mailboxSync = $mailboxSync;
4649
$this->syncService = $messageSync;
4750
$this->logger = $logger;
51+
$this->clientFactory = $clientFactory;
4852
}
4953

5054
/**
@@ -95,5 +99,9 @@ private function sync(Account $account, bool $force, OutputInterface $output): v
9599
$output->writeln("<info>Batch of new messages sync'ed. " . $mbs . 'MB of memory in use</info>');
96100
$this->sync($account, $force, $output);
97101
}
102+
103+
foreach ($this->clientFactory->getLoginStats() as $host => $count) {
104+
$consoleLogger->debug(sprintf('%d IMAP connection(s) to %s', $count, $host));
105+
}
98106
}
99107
}

lib/IMAP/HordeImapClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class HordeImapClient extends Horde_Imap_Client_Socket {
2828
private ?IMemcache $rateLimiterCache = null;
2929
private ?ITimeFactory $timeFactory = null;
3030
private ?string $hash = null;
31+
private IMAPClientFactory $factory;
32+
33+
public function __construct(array $params, IMAPClientFactory $factory) {
34+
parent::__construct($params);
35+
$this->factory = $factory;
36+
}
3137

3238
public function enableRateLimiter(
3339
IMemcache $cache,
@@ -57,7 +63,9 @@ public function login() {
5763
private const RATE_LIMIT_WINDOW = 3 * 60 * 60;
5864

5965
protected function imapLogin() {
60-
return parent::_login();
66+
$result = parent::_login();
67+
$this->factory->recordLogin($this->_params['hostspec']);
68+
return $result;
6169
}
6270

6371
#[\Override]

lib/IMAP/IMAPClientFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
use function json_encode;
2828

2929
class IMAPClientFactory {
30+
/** @var array<string, int> */
31+
private array $loginCounts = [];
32+
3033
/** @var ICrypto */
3134
private $crypto;
3235

@@ -134,7 +137,7 @@ public function getClient(Account $account, bool $useCache = true): Horde_Imap_C
134137
$params['debug'] = $this->config->getSystemValue('datadirectory') . '/' . $fn;
135138
}
136139

137-
$client = new HordeImapClient($params);
140+
$client = new HordeImapClient($params, $this);
138141

139142
$rateLimitingCache = $this->cacheFactory->createDistributed('mail_imap_ratelimit');
140143
if ($rateLimitingCache instanceof IMemcache) {
@@ -143,4 +146,13 @@ public function getClient(Account $account, bool $useCache = true): Horde_Imap_C
143146

144147
return $client;
145148
}
149+
150+
public function recordLogin(string $host): void {
151+
$this->loginCounts[$host] = ($this->loginCounts[$host] ?? 0) + 1;
152+
}
153+
154+
/** @return array<string, int> */
155+
public function getLoginStats(): array {
156+
return $this->loginCounts;
157+
}
146158
}

0 commit comments

Comments
 (0)