Skip to content
/ sekitome Public

A proud member of bicycle parade. Pure rack application that uses Redis expirable keys.

License

Notifications You must be signed in to change notification settings

Envek/sekitome

Repository files navigation

Sekitome

A small pure Rack application to answer simple question: was this username seen in last X seconds or not (usually used for throttling).

Task

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”}

Solution

Service

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).

Database

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.

Launch

Via Docker Compose

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

Manually

You will need to have installed:

  • Recent MRI Ruby version (recommended: 2.3)
  • Recent [Redis] (2.6+, recommended: 3.2)

Follow these simple steps:

  1. Launch [Redis] somewhere accessible from this machine and construct an URL to access it, like redis://127.0.0.1:6379/0

  2. Install required gems by executing bundle install in this directory.

  3. Launch service with command like:

    env 'REDIS_URL=redis://localhost/0' rackup -s puma -p 3000 -O "Threads=0:${MAX_THREADS:-16}"
    
  4. Access API endpoint on URL like this: http://localhost:3000/?username=Envek

  5. Access one more time.

  6. PROFIT!

Testing

env 'REDIS_URL=redis://localhost/0' bundle exec ruby test/integration.rb

Configuration

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.

Operations

Surviving restarts

About the name

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.

License

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)

About

A proud member of bicycle parade. Pure rack application that uses Redis expirable keys.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages