Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"500 Backend Error" using Gmail API - safe to retry?

I'm sending messages via the Gmail API. In particular, I am trying to send 5-7 emails from the same account to different users (1 each) within about 2 seconds.

About 8% of those emails are failing with this error:

&googleapi.Error{
    Code:500, 
    Message:"Backend Error", Body:`{
        "error": {
            "errors": [
            {
                "domain": "global",
                "reason": "backendError",
                "message": "Backend Error"
            }
            ],
            "code": 500,
            "message": "Backend Error"
        }
    }`, 
    Header:http.Header(nil),
    Errors:[]googleapi.ErrorItem{
        googleapi.ErrorItem{Reason:"backendError", Message:"Backend Error"}
    }
}

It doesn't seem like it's specific to a particular account, as 6/7 emails may succeed.

I'm hesitant to retry this for fear of sending 2 emails to the same person.

Is there any way to tell whether this message is safe to retry?

like image 476
Kevin Burke Avatar asked Apr 26 '17 05:04

Kevin Burke


People also ask

Is Gmail API safe?

If you're looking for a way to integrate Gmail into your own applications, the Gmail API is just what you need! If you're looking for a reliable way to send emails from your web or mobile app, Gmail is a great option. Its API is safe and credible, so your messages won't end up in the spam folder.

Is using Gmail API free?

Gmail API is available for free, but it has certain daily usage limits for API calls. Daily usage: 1 billion API calls per day. Per User Rate Limit: 250 API calls per user per second.

Does Google charge for Gmail API?

The Gmail API, like Gmail itself, is (currently) a free service from Google.

How does Gmail API work?

The Gmail API is a RESTful API that can be used to access Gmail mailboxes and send mail. For most web applications the Gmail API is the best choice for authorized access to a user's Gmail data and is suitable for various applications, such as: Read-only mail extraction, indexing, and backup.


1 Answers

"code": 500, "message": "Backend Error"

Is basically an issue with Google server. Either the request you are making took to long or the server preforming the request is busy and the request again took to long. It doesn't sound like what you are doing should be causing the problem.

Tips when not to run: Don't run on the hour you will be completing with everyone who has cron jobs set up also don't run at midnight (PDT) as this is when quotas reset and again you will be completing with everyone who blew out yesterdays quota.

Solution:

The normal solution is to wait a few seconds then send the same request again. (Implementing Exponential Backoff)

The flow for implementing simple exponential backoff is as follows.

  1. Make a request to the API
  2. Receive an error response that has a retry-able error code
  3. Wait 1s + random_number_milliseconds seconds
  4. Retry request
  5. Receive an error response that has a retry-able error code
  6. Wait 2s + random_number_milliseconds seconds
  7. Retry request
  8. Receive an error response that has a retry-able error code
  9. Wait 4s + random_number_milliseconds seconds
  10. Retry request
  11. Receive an error response that has a retry-able error code
  12. Wait 8s + random_number_milliseconds seconds
  13. Retry request
  14. Receive an error response that has a retry-able error code
  15. Wait 16s + random_number_milliseconds seconds
  16. Retry request

If you still get an error, stop and log the error.

like image 61
DaImTo Avatar answered Oct 06 '22 06:10

DaImTo