Email Monitor Synchronization

半世苍凉 提交于 2019-12-08 04:57:13

问题


I'm writing a C# program that monitors a dedicated Gmail account using POP3 for specialized command emails and reacts appropriately.

For maximum reliability, I will run this program on several computers throughout the country. I currently have a race condition where two instances of the program can read the same message before one of them deletes it, causing the message to be processed twice.

How can I make sure that each command is processed exactly once?

Gmail's POP3 access used to only serve each message once (making RETR and DELE a single atomic operation), but I can no longer reproduce this behavior.

The only method of communication between the computers is a SQL Server and an HTTP server (which I control).


回答1:


One option I've thought of is to use POP3's UIDL command, and have a table in SQL Server with a unique column of UIDLs that were already processed.

Then, before downloading each message, the daemon would INSERT the UIDL into the table, and, if it got an error, skip the message. (I'm assuming that SQL Server's INSERT command is an atomic operation).




回答2:


First I must admit I do not know what commands POP3 supports, but... if you can do an explicit 'DELE' and get an error if the message no longer exists, then I'd say:

  • RETR the message
  • DELE the message
  • Process only if DELE succeeded

EDIT:

After reading RFC1939, this approach should work; from the RFC:

DELE msg

     Arguments:
         a message-number (required) which may NOT refer to a
         message marked as deleted

     Restrictions:
         may only be given in the TRANSACTION state

     Discussion:
         The POP3 server marks the message as deleted.  Any future
         reference to the message-number associated with the message
         in a POP3 command generates an error.  The POP3 server does
         not actually delete the message until the POP3 session
         enters the UPDATE state.

     Possible Responses:
         +OK message deleted
         -ERR no such message

     Examples:
         C: DELE 1
         S: +OK message 1 deleted
            ...
         C: DELE 2
         S: -ERR message 2 already deleted

This is ofcourse assuming that the Gmail implementation actually honours the RFC.



来源:https://stackoverflow.com/questions/913487/email-monitor-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!