From: Timo Sirainen Date: Fri, 26 Sep 2025 11:39:51 +0000 (+0300) Subject: lib-storage: Send mail_opened event only once per mail X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dbe7cc3fd19720bffcdc8a2436704b0d53634ef6;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Send mail_opened event only once per mail The first time mail is opened it is read into cache, while the following reads are likely to be using the cache. Sending a mail_opened event for the following reads just confuses statistics. --- diff --git a/src/lib-storage/index/index-mail.c b/src/lib-storage/index/index-mail.c index 70060ba4f7..a8a2faebe8 100644 --- a/src/lib-storage/index/index-mail.c +++ b/src/lib-storage/index/index-mail.c @@ -1878,6 +1878,7 @@ static void index_mail_reset_data(struct index_mail *mail) mail->mail.mail.seq = 0; mail->mail.mail.uid = 0; mail->mail.seq_pvt = 0; + mail->mail.mail_opened_event_sent = FALSE; mail->mail.mail.expunged = FALSE; mail->mail.mail.has_nuls = FALSE; mail->mail.mail.has_no_nuls = FALSE; diff --git a/src/lib-storage/mail-storage-private.h b/src/lib-storage/mail-storage-private.h index eaeb202013..813ab6cfd2 100644 --- a/src/lib-storage/mail-storage-private.h +++ b/src/lib-storage/mail-storage-private.h @@ -610,6 +610,8 @@ struct mail_private { bool autoexpunged:1; /* mail created by mailbox_search_*() */ bool search_mail:1; + /* mail_opened event has been sent for this mail/seq */ + bool mail_opened_event_sent:1; }; struct mailbox_list_context { diff --git a/src/lib-storage/mail.c b/src/lib-storage/mail.c index 8bd4e83e93..74f1e1a489 100644 --- a/src/lib-storage/mail.c +++ b/src/lib-storage/mail.c @@ -708,6 +708,14 @@ void mail_opened_event(struct mail *mail) { struct mail_private *pmail = container_of(mail, struct mail_private, mail); + + /* If istream is opened twice for the same mail, count it as a single + mail_opened event. Their cost is effectively the same, so having + two events would just be confusing the statistics. */ + if (pmail->mail_opened_event_sent) + return; + pmail->mail_opened_event_sent = TRUE; + struct event_passthrough *e = event_create_passthrough(mail_event(mail))-> set_name("mail_opened")->