@@ -35,37 +35,11 @@ defmodule ExLimiter.Plug do
3535 """
3636 import Plug.Conn
3737
38+ alias ExLimiter.Bucket
39+
3840 @ compile_opts Application . compile_env ( :ex_limiter , __MODULE__ , [ ] )
39- @ limiter @ compile_opts [ :limiter ] || ExLimiter
40-
41- defmodule Config do
42- @ moduledoc false
43- @ compile_opts Application . compile_env ( :ex_limiter , ExLimiter.Plug , [ ] )
44- @ limit @ compile_opts [ :limit ] || 10
45- @ scale @ compile_opts [ :scale ] || 1000
46- @ fallback @ compile_opts [ :fallback ] || ExLimiter.Plug
47-
48- defstruct scale: @ scale ,
49- limit: @ limit ,
50- bucket: & ExLimiter.Plug . get_bucket / 1 ,
51- consumes: nil ,
52- decorate: nil ,
53- fallback: @ fallback
54-
55- def new ( opts ) do
56- contents =
57- opts
58- |> Map . new ( )
59- |> Map . put_new ( :consumes , fn _ -> 1 end )
60- |> Map . put_new ( :decorate , & ExLimiter.Plug . decorate / 2 )
61-
62- struct ( __MODULE__ , contents )
63- end
64- end
6541
66- def get_bucket ( % { private: % { phoenix_controller: contr , phoenix_action: ac } } = conn ) do
67- "#{ contr } .#{ ac } .#{ ip ( conn ) } "
68- end
42+ def get_bucket ( % { private: % { phoenix_controller: contr , phoenix_action: ac } } = conn ) , do: "#{ contr } .#{ ac } .#{ ip ( conn ) } "
6943
7044 def render_error ( conn , :rate_limited ) do
7145 conn
@@ -76,28 +50,46 @@ defmodule ExLimiter.Plug do
7650 @ spec decorate ( Plug.Conn . t ( ) , { :ok , Bucket . t ( ) } | { :rate_limited , bucket_name :: binary } ) :: Plug.Conn . t ( )
7751 def decorate ( conn , _ ) , do: conn
7852
79- def init ( opts ) , do: Config . new ( opts )
53+ def consume ( _conn ) , do: 1
54+
55+ def init ( opts \\ [ ] ) do
56+ @ compile_opts
57+ |> Keyword . merge ( opts )
58+ |> Keyword . validate! (
59+ limiter: ExLimiter ,
60+ limit: 10 ,
61+ scale: 1000 ,
62+ fallback: __MODULE__ ,
63+ bucket: & __MODULE__ . get_bucket / 1 ,
64+ consumes: & __MODULE__ . consume / 1 ,
65+ decorate: & __MODULE__ . decorate / 2
66+ )
67+ |> Map . new ( )
68+ end
69+
70+ def call ( conn , config ) do
71+ % {
72+ limiter: limiter ,
73+ bucket: bucket_fun ,
74+ scale: scale ,
75+ limit: limit ,
76+ consumes: consume_fun ,
77+ decorate: decorate_fun ,
78+ fallback: fallback
79+ } = config
8080
81- def call ( conn , % Config {
82- bucket: bucket_fun ,
83- scale: scale ,
84- limit: limit ,
85- consumes: consume_fun ,
86- decorate: decorate_fun ,
87- fallback: fallback
88- } ) do
8981 bucket_name = bucket_fun . ( conn )
9082
91- case @ limiter . consume ( bucket_name , consume_fun . ( conn ) , scale: scale , limit: limit ) do
83+ case limiter . consume ( bucket_name , consume_fun . ( conn ) , scale: scale , limit: limit ) do
9284 { :ok , bucket } = response ->
93- remaining = @ limiter . remaining ( bucket , scale: scale , limit: limit )
85+ remaining = limiter . remaining ( bucket , scale: scale , limit: limit )
9486
9587 conn
9688 |> put_rate_limit_headers ( limit , scale , remaining )
9789 |> decorate_fun . ( response )
9890
9991 { :error , :rate_limited } ->
100- remaining = @ limiter . remaining ( % ExLimiter. Bucket{ key: bucket_name } , scale: scale , limit: limit )
92+ remaining = limiter . remaining ( % Bucket { key: bucket_name } , scale: scale , limit: limit )
10193
10294 conn
10395 |> put_rate_limit_headers ( limit , scale , remaining )
0 commit comments