Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
authserv
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
ai
authserv
Commits
ba83b3f8
Commit
ba83b3f8
authored
10 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
enforce type correctness for memcache
parent
9413124f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
authserv/ratelimit.py
+11
-5
11 additions, 5 deletions
authserv/ratelimit.py
with
11 additions
and
5 deletions
authserv/ratelimit.py
+
11
−
5
View file @
ba83b3f8
...
...
@@ -18,6 +18,12 @@ _whitelist_rx = [
for
x
in
WHITELIST
]
def
_tostr
(
s
):
if
isinstance
(
s
,
unicode
):
return
s
.
encode
(
'
utf-8
'
)
return
s
def
whitelisted
(
ip
):
for
rx
in
_whitelist_rx
:
if
rx
.
search
(
ip
):
...
...
@@ -79,7 +85,7 @@ class RateLimit(object):
self
.
period
=
period
def
check
(
self
,
mc
,
key
):
key
=
self
.
prefix
+
key
key
=
_tostr
(
self
.
prefix
+
key
)
try
:
result
=
mc
.
incr
(
key
)
except
:
...
...
@@ -109,7 +115,7 @@ def ratelimit_http_request(key_fn, count=0, period=0):
def
decoratorfn
(
fn
):
@functools.wraps
(
fn
)
def
_ratelimit
(
*
args
,
**
kwargs
):
key
=
key_fn
(
fn
.
__name__
,
args
)
key
=
_tostr
(
key_fn
(
fn
.
__name__
,
args
)
)
if
key
and
not
rl
.
check
(
app
.
memcache
,
key
):
app
.
logger
.
debug
(
'
ratelimited: %s
'
,
key
)
abort
(
503
)
...
...
@@ -128,12 +134,12 @@ class BlackList(object):
self
.
ttl
=
ttl
def
check
(
self
,
mc
,
key
):
key
=
self
.
prefix
+
key
key
=
_tostr
(
self
.
prefix
+
key
)
result
=
mc
.
get
(
key
)
return
result
is
None
def
incr
(
self
,
mc
,
key
):
key
=
self
.
prefix
+
key
key
=
_tostr
(
self
.
prefix
+
key
)
if
not
self
.
rl
.
check
(
mc
,
key
):
mc
.
set
(
key
,
'
true
'
,
time
=
self
.
ttl
)
...
...
@@ -155,7 +161,7 @@ def blacklist_on_auth_failure(key_fn, count=0, period=0, ttl=0, check_wl=False):
def
decoratorfn
(
fn
):
@functools.wraps
(
fn
)
def
_blacklist
(
*
args
,
**
kwargs
):
key
=
key_fn
(
fn
.
__name__
,
args
)
key
=
_tostr
(
key_fn
(
fn
.
__name__
,
args
)
)
if
not
key
:
return
fn
(
*
args
,
**
kwargs
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment