I sync Gmail messages of users account with a Ruby on Rails app. I use the google-api-ruby-client, and more spcifically the gem “google-apis-gmail_v1”.
I get very often 429 errors, which are caught, causing a restart of all the batch requests, and hence a lot of useless requests.
What does the code :
First : it lists “ids” of the Gmail messages with a “messages.list” call (cost : 5 unit of quota per call)
gmail = gmail_service(employee)
gmail.fetch_all(items: :messages) do |token|
gmail.list_user_messages(
'me',
page_token: token,
quota_user: employee.id,
include_spam_trash: false
)
end.each_slice(25) do |ids|
sleep(0.5) # THROTTLING MANAGEMENT
fetch_messages_from_ids(employee, ids.map(&:id))
sleep(0.5)
For each 25 messages, it gets the message with a “message.get” call (cost : 5 units of quota per message = 125 units of quota by batch)
def fetch_messages_from_ids(employee, messages_ids)
gmail.batch do |gmail|
messages_ids.each do |id|
gmail.get_user_message(
'me',
id,
quota_user: employee.id,
format: 'metadata',
fields: 'id, history_id, label_ids, payload, size_estimate'
) do |result, err|
# Manage API answers
end
end
end
end
I regularly (several times during a sync for an employee) get 429 errors.
I checked the Gmail API documentation.
Max quota unit consumed by second of my code : 5 (message.list) + 25 * 5 (25 messages.get in batch) = 130 units which is far under the 250 limit explained in Gmail API doc : https://developers.google.com/gmail/api/reference/quota
Why ?