Skip to content

Commit

Permalink
move the oauth2 last_used update to the bottom of the request
Browse files Browse the repository at this point in the history
this avoids acquiring a row lock (and holding it for the duration of
transactions that include a very slow query)

see: ansible#4694
  • Loading branch information
ryanpetrello committed Sep 10, 2019
1 parent edb7ddb commit e254735
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions awx/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ def authenticate(self, request):
)
))
setattr(user, 'oauth_scopes', [x for x in token.scope.split() if x])
setattr(user, 'oauth_token', token)

return ret
5 changes: 5 additions & 0 deletions awx/main/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.db.migrations.executor import MigrationExecutor
from django.db import IntegrityError, connection
from django.utils.functional import curry
from django.utils.timezone import now
from django.shortcuts import get_object_or_404, redirect
from django.apps import apps
from django.utils.deprecation import MiddlewareMixin
Expand Down Expand Up @@ -52,6 +53,10 @@ def process_response(self, request, response):
cprofile_file = self.save_profile_file(request)
response['cprofile_file'] = cprofile_file
perf_logger.info('api response times', extra=dict(python_objects=dict(request=request, response=response)))
token = getattr(request.user, 'oauth_token', None)
if token:
token.last_used = now()
token.save(update_fields=['last_used'])
return response

def save_profile_file(self, request):
Expand Down
4 changes: 0 additions & 4 deletions awx/main/models/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Django
from django.core.validators import RegexValidator
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

Expand Down Expand Up @@ -119,9 +118,6 @@ class Meta:

def is_valid(self, scopes=None):
valid = super(OAuth2AccessToken, self).is_valid(scopes)
if valid:
self.last_used = now()
self.save(update_fields=['last_used'])
return valid

def save(self, *args, **kwargs):
Expand Down

0 comments on commit e254735

Please sign in to comment.