Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #133 from WeTransfer/gh-131-new-rate-limiter
Browse files Browse the repository at this point in the history
Move to Redis backend for our rate limiter
  • Loading branch information
dsnipe authored Aug 29, 2017
2 parents d11cc31 + 758cb81 commit 5781f84
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 52 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ matrix:
elixir: 1.4.0
services:
- postgresql
- redis-server
addons:
postgresql: "9.5"
cache:
Expand Down
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ config :phoenix, :generators,
config :xperiments, :cors,
origin: "*"

config :xperiments, redis_url: "redis://localhost"

config :xperiments, :js_config,
reporting_url: "https://analytics.google.com/analytics/web/?authuser=1#my-reports/5IyMQAn0Tcqdu2Va8V9BIg/a69714416w130256140p134086343/%3F_u.date00%3D20170227%26_u.date01%3D20170227%26_u.sampleOption%3Dmoreprecision%26_u.sampleSize%3D500000/"

Expand All @@ -58,6 +60,7 @@ config :guardian, Guardian,

config :bodyguard, current_user: {Guardian.Plug, :current_resource}


# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
4 changes: 2 additions & 2 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ config :xperiments, Xperiments.Web.Endpoint,
http: [port: 8080],
url: [host: {:system, "HOST", "xperiments.wetransfer.net"}, port: 80],
cache_static_manifest: "priv/static/cache_manifest.json",
secret_key_base: "${SECRET_KEY_BASE}",
server: true,
root: ".",
version: Mix.Project.config[:version]
Expand All @@ -22,8 +23,7 @@ config :ueberauth, Ueberauth.Strategy.Google.OAuth,
client_secret: "${GOOGLE_CLIENT_SECRET}",
redirect_uri: "${GOOGLE_OAUTH_CALLBACK}"

config :xperiments, Xperiments.Web.Endpoint,
secret_key_base: "${SECRET_KEY_BASE}"
config :xperiments, redis_url: "${REDIS_URL}"

config :guardian, Guardian,
secret_key: "${SECRET_KEY_BASE}"
Expand Down
4 changes: 2 additions & 2 deletions lib/xperiments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule Xperiments do
# Define workers and child supervisors to be supervised
children = [
supervisor(Xperiments.Repo, []),
worker(Hammer.Backend.ETS, [[expiry_ms: 1000 * 60 * 60,
cleanup_interval_ms: 1000 * 60 * 10]]),
worker(Hammer.Backend.Redis, [[expiry_ms: 1000 * 60 * 60,
redix_config: Application.get_env(:xperiments, :redis_url)]]),
supervisor(Xperiments.Web.Endpoint, []),
supervisor(Xperiments.Assigner.Supervisor, [])
]
Expand Down
2 changes: 1 addition & 1 deletion lib/xperiments/web/plugs/rate_limit_plug.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Xperiments.Plug.RateLimit do
use Hammer, backend: Hammer.Backend.ETS, only: [:check_rate]
use Hammer, backend: Hammer.Backend.Redis, only: [:check_rate]
import Phoenix.Controller, only: [json: 2]
import Plug.Conn
require Logger
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ defmodule Xperiments.Mixfile do
{:appsignal, "~> 0.0"},
{:remote_ip, "~> 0.1.0"},
{:hammer, "~> 0.1.0"},
{:hammer_backend_redis, "~> 0.1.0"},
{:ex_machina, "~> 1.0", only: :test},
{:mock, "~> 0.2.0", only: :test}]
end
Expand Down
94 changes: 48 additions & 46 deletions mix.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/controllers/assigner_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Xperiments.AssignerControllerTest do
use Xperiments.Web.ConnCase, async: false
import Mock
alias Xperiments.Assigner.ExperimentSupervisor
use Hammer, backend: Hammer.Backend.ETS, only: [:delete_buckets]
use Hammer, backend: Hammer.Backend.Redis, only: [:delete_buckets]

setup do
for e_pid <- ExperimentSupervisor.experiment_pids() do
Expand Down

0 comments on commit 5781f84

Please sign in to comment.