mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-19 04:31:35 +00:00
Perf reduce DB writes on last_login updates
This commit is contained in:
parent
c7a14fbc56
commit
b3598f5e9c
@ -1,10 +1,12 @@
|
||||
import hashlib
|
||||
from datetime import timedelta
|
||||
from pathlib import Path
|
||||
|
||||
from channels.db import database_sync_to_async
|
||||
from channels.middleware import BaseMiddleware
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AnonymousUser, User, update_last_login
|
||||
from django.utils import timezone
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework.exceptions import AuthenticationFailed
|
||||
@ -84,6 +86,8 @@ class RobotTokenSHA256AuthenticationMiddleWare:
|
||||
# Check if it is an existing robot.
|
||||
try:
|
||||
token = Token.objects.get(key=token_sha256_b91)
|
||||
# Update last login every 2 minutes (avoid too many DB writes)
|
||||
if token.user.last_login < timezone.now() - timedelta(minutes=2):
|
||||
update_last_login(None, token.user)
|
||||
|
||||
except Token.DoesNotExist:
|
||||
|
Loading…
Reference in New Issue
Block a user