본문 바로가기
Category/Django

Throttling 적용하기

by Corinee 2025. 7. 21.
728x90
반응형

UserRateThrottle(인증된 유저에 대한), AnonRateThrottle(익명 사용자에 대한), ScopedRateThrottle(해당 ID나 IP에 대한) 또는 커스텀하여 적용할 수 있습니다.

 

Throttling을 적용하고자 하는 앱에 다음과 같이 초당 1회(테스트)로 제한하도록 설정한 후에 카테고리 목록을 가져오는 뷰셋에 추가해주었습니다.

# throttling.py

from rest_framework.throttling import AnonRateThrottle, UserRateThrottle


class MOUThrottle(UserRateThrottle):
    rate = "1/s"


class MOUAnonThrottle(AnonRateThrottle):
    rate = "1/s"

 

from rest_framework import viewsets

from mou.throttling import MOUAnonThrottle, MOUThrottle

from .models import MOU, MOUCategory
from .serializers import MOUCategorySerializer


class MOUCategoryViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = MOUCategory.objects.all().order_by("name")
    serializer_class = MOUCategorySerializer
    throttle_classes = [MOUThrottle, MOUAnonThrottle]

 

Throttling을 적용한 후의 응답은 다음과 같습니다.