mirror of
https://github.com/RoboSats/robosats.git
synced 2025-07-20 09:43:23 +00:00
Perf reduce DB writes on last_login updates
This commit is contained in:
@ -1,10 +1,12 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
from datetime import timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from channels.db import database_sync_to_async
|
from channels.db import database_sync_to_async
|
||||||
from channels.middleware import BaseMiddleware
|
from channels.middleware import BaseMiddleware
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import AnonymousUser, User, update_last_login
|
from django.contrib.auth.models import AnonymousUser, User, update_last_login
|
||||||
|
from django.utils import timezone
|
||||||
from django.utils.deprecation import MiddlewareMixin
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.exceptions import AuthenticationFailed
|
from rest_framework.exceptions import AuthenticationFailed
|
||||||
@ -84,7 +86,9 @@ class RobotTokenSHA256AuthenticationMiddleWare:
|
|||||||
# Check if it is an existing robot.
|
# Check if it is an existing robot.
|
||||||
try:
|
try:
|
||||||
token = Token.objects.get(key=token_sha256_b91)
|
token = Token.objects.get(key=token_sha256_b91)
|
||||||
update_last_login(None, token.user)
|
# 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:
|
except Token.DoesNotExist:
|
||||||
# If we get here the user does not have a robot on this coordinator
|
# If we get here the user does not have a robot on this coordinator
|
||||||
|
Reference in New Issue
Block a user