27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Rate limiting<br />

fine for a project with a small amount of tickets, but if a project grew to something say,<br />

the size of the Rails project, 14 then it would be problematic because Rails would have<br />

to instantiate thousands of new Ticket objects per-request. That’s no good.<br />

Now that you’re versed in the Ways of the API, you can tackle potential problems<br />

with it. One of the potential problems with this API is that you’ll have too many users<br />

accessing it all at once, which may cause performance problems with the application.<br />

To prevent this, you’ll implement the rate of requests people can make to your server.<br />

13.3 Rate limiting<br />

When a server receives too many requests, it can seem unresponsive. This is simply<br />

because it is too busy serving existing requests to serve the hoard of incoming<br />

requests. This can happen when an application provides an API to which many clients<br />

are connecting. To prevent this, you’ll implement rate-limiting on the API side of<br />

things, limiting users to only 100 API requests per hour.<br />

The way you’re going to do this is to add a new field to the users table that stores a<br />

count of how many requests the user has made per hour. To reset the user’s count<br />

back to zero, you’ll create a method that finds only the users who’ve made requests in<br />

the last hour and reset their counts.<br />

13.3.1 One request, two request, three request, four<br />

Currently in app/controllers/api/v1/base_controller.rb you’ve got code that only<br />

checks if the token specified is correct, and if so, assigns a user to the @current_user<br />

variable:<br />

def authenticate_user<br />

@current_user = User.find_by_authentication_token(params[:token])<br />

unless @current_user<br />

respond_with({ :error => "Token is invalid." })<br />

end<br />

end<br />

You’ll now be able to do whatever you wish to this user object in an API request. First,<br />

you’re going to make sure that it’s incrementing the request count for a user whenever<br />

they make an API request. For this, you need a field in the database to keep a<br />

track of user API requests. You’ll generate a migration using this command<br />

rails g migration add_request_count_to_users request_count:integer<br />

This migration will do exactly what you say it should do: add a field called<br />

request_count to the users table. You’ll need to modify this migration slightly so that<br />

the field defaults to 0, which you can do by replacing this line in the new migration:<br />

add_column :users, :request_count, :integer<br />

with this:<br />

add_column :users, :request_count, :integer, :default => 0<br />

14 6000 tickets, as of this writing.<br />

377

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!