A small pure Rack application to answer simple question: was this username seen in last X seconds or not (usually used for throttling).
The service receives one case insensitive GET parameter: username
. If this parameter wasn't received during the last minute, service returns JSON:
{“result”: “OK”}
If this username was received during the last minute, service returns JSON:
{“result”: “<username> throttled”}
API is written on Ruby as Rack application. Pure Rack applications are very lightweight and fast, can be run in any compatible application server (I chose fast threaded Puma).
Redis is fast and battle-tested key-value in-memory database which persists data on disk. Also it has ability to expire keys and that's why it's chosen as database for this service.
You will need to have installed:
- Recent [Docker]
- Recent [Docker Compose]
Then just execute next command from this directory:
docker-compose up
Access Sekitome multiple times on URL like this: http://localhost:3000/?username=Envek
You will need to have installed:
- Recent MRI Ruby version (recommended: 2.3)
- Recent [Redis] (2.6+, recommended: 3.2)
Follow these simple steps:
-
Launch [Redis] somewhere accessible from this machine and construct an URL to access it, like
redis://127.0.0.1:6379/0
-
Install required gems by executing
bundle install
in this directory. -
Launch service with command like:
env 'REDIS_URL=redis://localhost/0' rackup -s puma -p 3000 -O "Threads=0:${MAX_THREADS:-16}"
-
Access API endpoint on URL like this: http://localhost:3000/?username=Envek
-
Access one more time.
-
…
-
PROFIT!
env 'REDIS_URL=redis://localhost/0' bundle exec ruby test/integration.rb
Next environment variables will change behavior of this application:
REDIS_URL
(required) — URL to connect to Redis.THROTTLE_TIMEOUT
— A number of seconds after which username will be forgot. Default is 60 (1 minute).MAX_THREADS
— maximum number of threads to be used by Puma application server, also affects size of connection pool to [Redis] accordingly. Default is 16.
-
For deploying new versions of code Puma Phased Restart usage should be considered: https://github.com/puma/puma#normal-vs-hot-vs-phased-restart
-
[Redis] should be configured to use Append-Only File (AOF), to avoid data loss after unexpected restarts. See the docs for details: https://redis.io/topics/persistence
A word sekitome is a verbal noun from japanese verb sekitomeru (堰き止める) with direct meaning of to dam (a river) and figurative meaning of to impede, delay, slow down. Close enough to meaning of throttling I believe.
Can be freely used, distributed, and modified under the terms of the [MIT License]. See the LICENSE file.
[Redis]: https://redis.io/ (Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker) [MIT License]: https://opensource.org/licenses/MIT (A short and simple permissive license with conditions only requiring preservation of copyright and license notices) [Docker]: https://www.docker.com/ (Docker is the world’s leading software containerization platform) [Docker Compose]: https://docs.docker.com/compose/ (Compose is a tool for defining and running multi-container Docker applications)