Skip to content

Commit

Permalink
Avoid memory issues for non-MODSEQ servers.
Browse files Browse the repository at this point in the history
With very large mailboxes and no MODSEQ, the initial
sync can hit the server memory limit when loading the
entire mailbox to fetch flag state. Avoid this by
loading in 2000 message chunks.
  • Loading branch information
mrubinsk committed Jun 6, 2015
1 parent c971fa5 commit 145261b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions framework/ActiveSync/lib/Horde/ActiveSync/Imap/Adapter.php
Expand Up @@ -25,6 +25,11 @@
*/
class Horde_ActiveSync_Imap_Adapter
{
/**
* Maximum number of of messages to fetch from the IMAP server in one go.
*/
const MAX_FETCH = 2000;

/**
* @var Horde_ActiveSync_Interface_ImapFactory
*/
Expand Down Expand Up @@ -420,15 +425,22 @@ public function getMessageChanges(
$this->_procid));
$folder->primeFolder($search_ret['match']->ids);
} elseif (count($search_ret['match']->ids)) {
// No modseq.
$query = new Horde_Imap_Client_Fetch_Query();
$query->flags();
$fetch_ret = $imap->fetch($mbox, $query, array('ids' => $search_ret['match']));
foreach ($fetch_ret as $uid => $data) {
$flags[$uid] = array(
'read' => (array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false) ? 1 : 0
$cnt = ($search_ret['count'] / self::MAX_FETCH) + 1;
for ($i = 0; $i <= $cnt; $i++) {
$ids = new Horde_Imap_Client_Ids(
array_slice($search_ret['match']->ids, $i * self::MAX_FETCH, self::MAX_FETCH)
);
if (($options['protocolversion']) > Horde_ActiveSync::VERSION_TWOFIVE) {
$flags[$uid]['flagged'] = (array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false) ? 1 : 0;
$fetch_ret = $imap->fetch($mbox, $query, array('ids' => $ids));
foreach ($fetch_ret as $uid => $data) {
$flags[$uid] = array(
'read' => (array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false) ? 1 : 0
);
if (($options['protocolversion']) > Horde_ActiveSync::VERSION_TWOFIVE) {
$flags[$uid]['flagged'] = (array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false) ? 1 : 0;
}
}
}
$folder->setChanges($search_ret['match']->ids, $flags);
Expand Down

0 comments on commit 145261b

Please sign in to comment.