@@ -566,10 +566,38 @@ impl Imap {
566
566
}
567
567
session. new_mail = false ;
568
568
569
+ let mut read_cnt = 0 ;
570
+ loop {
571
+ let ( n, fetch_more) = self
572
+ . fetch_new_msg_batch ( context, session, folder, folder_meaning)
573
+ . await ?;
574
+ read_cnt += n;
575
+ if !fetch_more {
576
+ return Ok ( read_cnt > 0 ) ;
577
+ }
578
+ }
579
+ }
580
+
581
+ /// Returns number of messages processed and whether the function should be called again.
582
+ async fn fetch_new_msg_batch (
583
+ & mut self ,
584
+ context : & Context ,
585
+ session : & mut Session ,
586
+ folder : & str ,
587
+ folder_meaning : FolderMeaning ,
588
+ ) -> Result < ( usize , bool ) > {
569
589
let uid_validity = get_uidvalidity ( context, folder) . await ?;
570
590
let old_uid_next = get_uid_next ( context, folder) . await ?;
591
+ info ! (
592
+ context,
593
+ "fetch_new_msg_batch({folder}): UIDVALIDITY={uid_validity}, UIDNEXT={old_uid_next}."
594
+ ) ;
571
595
572
- let msgs = session. prefetch ( old_uid_next) . await . context ( "prefetch" ) ?;
596
+ let uids_to_prefetch = 500 ;
597
+ let msgs = session
598
+ . prefetch ( old_uid_next, uids_to_prefetch)
599
+ . await
600
+ . context ( "prefetch" ) ?;
573
601
let read_cnt = msgs. len ( ) ;
574
602
575
603
let download_limit = context. download_limit ( ) . await ?;
@@ -729,7 +757,8 @@ impl Imap {
729
757
largest_uid_fetched
730
758
} ;
731
759
732
- let actually_download_messages_future = async move {
760
+ let actually_download_messages_future = async {
761
+ let sender = sender;
733
762
let mut uids_fetch_in_batch = Vec :: with_capacity ( max ( uids_fetch. len ( ) , 1 ) ) ;
734
763
let mut fetch_partially = false ;
735
764
uids_fetch. push ( ( 0 , !uids_fetch. last ( ) . unwrap_or ( & ( 0 , false ) ) . 1 ) ) ;
@@ -764,14 +793,17 @@ impl Imap {
764
793
// if the message has arrived after selecting mailbox
765
794
// and determining its UIDNEXT and before prefetch.
766
795
let mut new_uid_next = largest_uid_fetched + 1 ;
767
- if fetch_res. is_ok ( ) {
796
+ let fetch_more = fetch_res. is_ok ( ) && {
797
+ let prefetch_uid_next = old_uid_next + uids_to_prefetch;
768
798
// If we have successfully fetched all messages we planned during prefetch,
769
799
// then we have covered at least the range between old UIDNEXT
770
800
// and UIDNEXT of the mailbox at the time of selecting it.
771
- new_uid_next = max ( new_uid_next, mailbox_uid_next) ;
801
+ new_uid_next = max ( new_uid_next, min ( prefetch_uid_next , mailbox_uid_next) ) ;
772
802
773
803
new_uid_next = max ( new_uid_next, largest_uid_skipped. unwrap_or ( 0 ) + 1 ) ;
774
- }
804
+
805
+ prefetch_uid_next < mailbox_uid_next
806
+ } ;
775
807
if new_uid_next > old_uid_next {
776
808
set_uid_next ( context, folder, new_uid_next) . await ?;
777
809
}
@@ -788,7 +820,7 @@ impl Imap {
788
820
// establish a new session if this one is broken.
789
821
fetch_res?;
790
822
791
- Ok ( read_cnt > 0 )
823
+ Ok ( ( read_cnt, fetch_more ) )
792
824
}
793
825
794
826
/// Read the recipients from old emails sent by the user and add them as contacts.
0 commit comments