Skip to content

Commit

Permalink
Do not throttle workers
Browse files Browse the repository at this point in the history
  • Loading branch information
negasora committed Sep 8, 2023
1 parent a0dcc9c commit a96f183
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
7 changes: 7 additions & 0 deletions decompiler_explorer/throttle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from rest_framework.throttling import AnonRateThrottle

from .utils import is_request_from_worker

class AnonRateThrottleSliding(AnonRateThrottle):
def get_cache_key(self, request, view):
if is_request_from_worker(request):
# Do not throttle workers
return None
return super().get_cache_key(request, view)

def throttle_failure(self):
if len(self.history) >= self.num_requests: # type: ignore
self.history.pop(-1)
Expand Down
12 changes: 12 additions & 0 deletions decompiler_explorer/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import hashlib

from django.conf import settings

def is_request_from_worker(request):
auth_header = request.META.get('HTTP_X_AUTH_TOKEN')
if auth_header is None:
return False
if settings.DEBUG:
return True
hashed_token = hashlib.sha256(auth_header.encode()).hexdigest()
return hashed_token == settings.WORKER_AUTH_TOKEN_HASH
16 changes: 2 additions & 14 deletions explorer/permissions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import hashlib

from django.conf import settings
from rest_framework.permissions import BasePermission, SAFE_METHODS

from decompiler_explorer.utils import is_request_from_worker

class IsWorkerOrAdmin(BasePermission):
def has_permission(self, request, view):
if bool(request.user and request.user.is_staff):
return True

auth_header = request.META.get('HTTP_X_AUTH_TOKEN')
if auth_header is None:
return False

if settings.DEBUG:
return True

hashed_token = hashlib.sha256(auth_header.encode()).hexdigest()

return hashed_token == settings.WORKER_AUTH_TOKEN_HASH
return is_request_from_worker(request)


class ReadOnly(BasePermission):
Expand Down

0 comments on commit a96f183

Please sign in to comment.