Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord (Rails 3.0.1): API Can't Handle Too Many Requests?
    primarykey
    data
    text
    <p>I have an API that services a web-based plugin for processing email. The API is responsible for two things:</p> <ol> <li>Creating SessionIDs so the plugin can setup a dynamic link; and </li> <li>Once an email is sent, for receiving that SessionID, the email recipients and subject line, to store the information into a new session.</li> </ol> <p>Imagine the scenario where the plugin sends a request to the API: </p> <pre><code>PUT http://server.com/api/email/update/&lt;SessionID&gt; -d "to=&lt;address1,address2&gt;&amp;subject=&lt;subject&gt;" </code></pre> <p>In testing this works fine: the data is saved normally. However, the plugin can't help but send that request several times a second, bombarding my server with identical requests. The result is that I get my EmailSession object saving multiple copies of the recipients. </p> <p>In terms of my database schema, I have an EmailSession model, which has_many EmailRecipients. </p> <p>Here's the relevant part of the update method in my API's controller:</p> <pre><code>@email_session = EmailSession.find_or_create_by_session_id(:session_id =&gt; params[:id], :user_id =&gt; @user.id) if opts[:params][:cm_to].blank? == false self.email_recipients.destroy_all unless opts[:params][:cm_to].blank? opts[:params][:cm_to].strip.split(",").each do |t| self.email_recipients &lt;&lt; EmailRecipient.create(:recipient_email =&gt; t) end end end </code></pre> <p>Admittedly, the "find_or_create" dynamic method is new to me, and I wonder if there's something about that screwing up the works. </p> <p>The symptoms I'm seeing include:</p> <ul> <li><p>ActiveRecord errors complaining about attempts to save a non-unique key into the database (I have an index on the SessionId)</p></li> <li><p>Duplicate recipients ending up in the EmailRecipients collection</p></li> <li><p>In the case of multiple users employing the plugin, I get recipients from other emails ending up in the wrong email session collections.</p></li> </ul> <p>I've attempted to employ delayed_job to attempt to serialize these requests somehow. I haven't had much luck with it thanks to various bugs in the current release. But I'm wondering if there's a more fundamental problem with my approach to this solution? Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload