Rack attack filling up hard drive space

638 Views Asked by At

I have also asked this on GitHub but it seems nobody is answering.

The problem is I am using Rack Attack as a gem for blocking scraping.

My config is located here

The problem I am running into is it is writing cache to the file system and filling up resulting in the following error

No space left on device@ rb_sysopen - /var/app/current/tmp/cache/.rack%3A%3Aattack%3A5249968%3Areq%2Fip%3A104.199.176.25320191129-1869-11sg00n

How do i prevent this from happening?

1

There are 1 best solutions below

0
On

The gem documentation doesn't provide any information about the caching mechanism, but looking in the source code tells that they're using the Rails.cache so you can look at that.

Based on the error you gave, your Rails application seem to use the :file_store cache (it's the default one), and the Rails documentation says:

As the cache will grow until the disk is full, it is recommended to periodically clear out old entries.

But I found a cleanup method which is present since a while (I checked from Rails version 3 to 6) and seem to know about expired data, but there were a bug preventing Rails to clean expired data that has been fixed in Rails 5.2. If you're using Rails 5.2 and newer try to set an expiration time to the cache:

config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 10.minutes }

Please update the cache path (second argument) and the adapt the expires_in value for your case.

Another option would be to change the Rails cache store to something like Redis (using the redis-rails gem) or Memcached.

Redis will delete the expired keys and Memcached delete the oldest keys when reaching the limit of allowed space.