Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Show more breadcrumbs
retribute.me
api
Commits
58b21157
Verified
Commit
58b21157
authored
6 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
Implement proper caching closing
parent
1f2b53b3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
retribute_api/cache.py
+8
-0
8 additions, 0 deletions
retribute_api/cache.py
retribute_api/search/consumers.py
+20
-5
20 additions, 5 deletions
retribute_api/search/consumers.py
with
28 additions
and
5 deletions
retribute_api/cache.py
+
8
−
0
View file @
58b21157
...
...
@@ -15,6 +15,9 @@ class Backend:
async
def
set
(
self
,
key
):
raise
NotImplementedError
async
def
close
(
self
):
return
class
Dummy
(
Backend
):
def
__init__
(
self
):
...
...
@@ -59,6 +62,11 @@ class Redis(Backend):
r
=
await
self
.
redis
()
await
r
.
set
(
key
,
json
.
dumps
(
value
))
async
def
close
(
self
):
if
not
self
.
_redis
:
return
await
self
.
_redis
.
close
()
def
get_default
():
return
Redis
(
settings
.
ASYNC_REDIS_PARAMS
)
This diff is collapsed.
Click to expand it.
retribute_api/search/consumers.py
+
20
−
5
View file @
58b21157
...
...
@@ -36,6 +36,18 @@ def wrapper_500(callback):
return
callback
def
with_cache
(
f
):
async
def
inner
(
*
args
,
**
kwargs
):
c
=
kwargs
.
setdefault
(
"
cache
"
,
cache
.
get_default
())
try
:
return
await
f
(
*
args
,
**
kwargs
)
except
Exception
:
await
c
.
close
()
raise
return
inner
def
ignore_aiohttp_ssl_eror
(
loop
,
aiohttpversion
=
"
3.5.4
"
):
"""
Ignore aiohttp #3535 issue with SSL data after close
...
...
@@ -84,7 +96,8 @@ def ignore_aiohttp_ssl_eror(loop, aiohttpversion="3.5.4"):
class
SearchSingleConsumer
(
AsyncHttpConsumer
):
@wrapper_500
async
def
handle
(
self
,
body
):
@with_cache
async
def
handle
(
self
,
body
,
cache
):
lookup_type
=
self
.
scope
[
"
url_route
"
][
"
kwargs
"
][
"
lookup_type
"
]
lookup
=
self
.
scope
[
"
url_route
"
][
"
kwargs
"
][
"
lookup
"
]
ignore_aiohttp_ssl_eror
(
asyncio
.
get_running_loop
())
...
...
@@ -94,7 +107,7 @@ class SearchSingleConsumer(AsyncHttpConsumer):
await
json_response
(
self
,
400
,
{
"
detail
"
:
"
Invalid lookup
"
})
try
:
async
with
aiohttp
.
client
.
ClientSession
(
timeout
=
aiohttp_timeout
)
as
session
:
data
=
await
source
.
get
(
lookup
,
session
,
cache
=
cache
.
get_default
()
)
data
=
await
source
.
get
(
lookup
,
session
,
cache
=
cache
)
profile
=
sources
.
result_to_retribute_profile
(
lookup_type
,
lookup
,
data
)
except
(
exceptions
.
SearchError
,
aiohttp
.
ClientError
)
as
e
:
await
json_response
(
self
,
400
,
{
"
detail
"
:
e
.
message
})
...
...
@@ -105,9 +118,9 @@ class SearchSingleConsumer(AsyncHttpConsumer):
await
json_response
(
self
,
200
,
profile
)
async
def
do_lookup
(
lookup
,
lookup_type
,
session
,
source
,
results
):
async
def
do_lookup
(
lookup
,
lookup_type
,
session
,
source
,
results
,
cache
):
try
:
data
=
await
source
.
get
(
lookup
,
session
,
cache
=
cache
.
get_default
()
)
data
=
await
source
.
get
(
lookup
,
session
,
cache
=
cache
)
profile
=
sources
.
result_to_retribute_profile
(
lookup_type
,
lookup
,
data
)
except
(
exceptions
.
SearchError
,
...
...
@@ -126,7 +139,8 @@ async def do_lookup(lookup, lookup_type, session, source, results):
class
SearchMultipleConsumer
(
AsyncHttpConsumer
):
@wrapper_500
async
def
handle
(
self
,
body
):
@with_cache
async
def
handle
(
self
,
body
,
cache
):
if
self
.
scope
[
"
method
"
]
==
"
OPTIONS
"
:
return
await
self
.
send_response
(
200
,
...
...
@@ -167,6 +181,7 @@ class SearchMultipleConsumer(AsyncHttpConsumer):
session
=
session
,
source
=
source
,
results
=
results
,
cache
=
cache
,
)
)
try
:
...
...
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