Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttling speed of email sending process

Sorry the title is a bit crappy, I couldn't quite word it properly.

Edit: I should note this is a console c# app

I've prototyped out a system that works like so (this is rough pseudo-codeish):

var collection = grabfromdb();

foreach (item in collection) {
    SendAnEmail();
}

SendAnEmail:

SmtpClient mailClient = new SmtpClient;
mailClient.SendCompleted += new SendCompletedEventHandler(SendComplete);
mailClient.SendAsync('the mail message');

SendComplete:

if (anyErrors) {
    errorHandling()
}
else {
    HitDBAndMarkAsSendOK();    
}

Obviously this setup is not ideal. If the initial collection has, say 10,000 records, then it's going to new up 10,000 instances of smtpclient in fairly short order as quickly as it can step through the rows - and likely asplode in the process.

My ideal end game is to have something like 10 concurrent email going out at once.

A hacky solution comes to mind: Add a counter, that increments when SendAnEmail() is called, and decrements when SendComplete is sent. Before SendAnEmail() is called in the initial loop, check the counter, if it's too high, then sleep for a small period of time and then check it again.

I'm not sure that's such a great idea, and figure the SO hive mind would have a way to do this properly.

I have very little knowledge of threading and not sure if it would be an appropriate use here. Eg sending email in a background thread, first check the number of child threads to ensure there's not too many being used. Or if there is some type of 'thread throttling' built in.


Update

Following in the advice of Steven A. Lowe, I now have:

  • A Dictionary holding my emails and a unique key (this is the email que
  • A FillQue Method, which populates the dictionary
  • A ProcessQue method, which is a background thread. It checks the que, and SendAsycs any email in the que.
  • A SendCompleted delegate which removes the email from the que. And calls FillQue again.

I've a few problems with this setup. I think I've missed the boat with the background thread, should I be spawning one of these for each item in the dictionary? How can I get the thread to 'hang around' for lack of a better word, if the email que empties the thread ends.


final update

I've put a 'while(true) {}' in the background thread. If the que is empty, it waits a few seconds and tries again. If the que is repeatedly empty, i 'break' the while, and the program ends... Works fine. I'm a bit worried about the 'while(true)' business though..

like image 578
ChadT Avatar asked Mar 18 '09 04:03

ChadT


People also ask

What does throttling mean in email?

Email throttling is controlling the amount of email messages sent to one ISP or remote server at one time. Sometimes ISPs block messages when a high volume is sent by one sender at one time because they might be concerned its spam.

What is SMTP throttling?

Message throttling refers to a group of limits that are set on the number of messages and connections that can be processed by an Exchange server. These limits include message processing rates, SMTP connection rates, and SMTP session timeout values.

What does IPS throttled by recipient server mean?

Email throttling is what happens when the receiving Internet Service Provider (ISP) limits the amount of email they can accept from the sender during a specified period.


3 Answers

Short Answer

Use a queue as a finite buffer, processed by its own thread.

Long Answer

Call a fill-queue method to create a queue of emails, limited to (say) 10. Fill it with the first 10 unsent emails. Launch a thread to process the queue - for each email in the queue, send it asynch. When the queue is empty sleep for a while and check again. Have the completion delegate remove the sent or errored email from the queue and update the database, then call the fill-queue method to read more unsent emails into the queue (back up to the limit).

You'll only need locks around the queue operations, and will only have to manage (directly) the one thread to process the queue. You will never have more than N+1 threads active at once, where N is the queue limit.

like image 121
Steven A. Lowe Avatar answered Oct 05 '22 10:10

Steven A. Lowe


I believe your hacky solution actually would work. Just make sure you have a lock statement around the bits where you increment and decrement the counter:

class EmailSender
{
  object SimultaneousEmailsLock;
  int SimultaneousEmails;
  public string[] Recipients;

  void SendAll()
  {
    foreach(string Recipient in Recipients)
    {
      while (SimultaneousEmails>10) Thread.Sleep(10);
      SendAnEmail(Recipient);
    }
  }

  void SendAnEmail(string Recipient)
  {
    lock(SimultaneousEmailsLock)
    {
      SimultaneousEmails++;
    }

    ... send it ...
  }

  void FinishedEmailCallback()
  {
    lock(SimultaneousEmailsLock)
    {
      SimultaneousEmails--;
    }

    ... etc ...
  }
}
like image 31
Chris Avatar answered Oct 05 '22 09:10

Chris


I would add all my messages to a Queue, and then spawn i.e. 10 threads which sent emails until the Queue was empty. Pseudo'ish C# (probably wont compile):

class EmailSender
{
    Queue<Message> messages;
    List<Thread> threads;

    public Send(IEnumerable<Message> messages, int threads)
    {
        this.messages = new Queue<Message>(messages);
        this.threads = new List<Thread>();
        while(threads-- > 0)
            threads.Add(new Thread(SendMessages));

        threads.ForEach(t => t.Start());

        while(threads.Any(t => t.IsAlive))
            Thread.Sleep(50);
    }

    private SendMessages()
    {
        while(true)
        {
            Message m;
            lock(messages)
            {
                try
                {
                    m = messages.Dequeue();
                }
                catch(InvalidOperationException)
                {
                    // No more messages
                    return;
                }
            }

            // Send message in some way. Not in an async way, 
            // since we are already kind of async.

            Thread.Sleep(); // Perhaps take a quick rest
        }
    }
}

If the message is the same, and just having many recipients, just swap the Message with a Recipient, and add a single Message parameter to the Send method.

like image 25
Svish Avatar answered Oct 05 '22 10:10

Svish