throttle.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from rest_framework.throttling import BaseThrottle
  2. from throttle.models import ListModel
  3. from utils.md5 import Md5
  4. from django.utils import timezone
  5. from django.conf import settings
  6. data = {}
  7. class VisitThrottle(BaseThrottle):
  8. def allow_request(self, request, view):
  9. if request.path in ['/api/docs/', '/api/debug/', '/api/']:
  10. return (False, None)
  11. else:
  12. ip = request.META.get('HTTP_X_FORWARDED_FOR') if request.META.get(
  13. 'HTTP_X_FORWARDED_FOR') else request.META.get('REMOTE_ADDR')
  14. openid = request.auth.openid
  15. appid = request.auth.appid
  16. if request.method.lower() == "get":
  17. ntime = timezone.now()
  18. ctime = ntime - timezone.timedelta(seconds=1)
  19. throttle_ctimelist = ListModel.objects.filter(method="get", create_time__lte=ctime)
  20. for i in throttle_ctimelist:
  21. i.delete()
  22. t_code = Md5.md5(ip)
  23. throttle_allocationlist = ListModel.objects.filter(openid=openid, appid=appid, ip=ip,
  24. method='get').order_by('id')
  25. throttle_count = throttle_allocationlist.count()
  26. if throttle_count == 0:
  27. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="get", t_code=t_code)
  28. return True
  29. else:
  30. throttle_last_create_time = throttle_allocationlist.first().create_time
  31. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="get", t_code=t_code)
  32. allocation_seconds_balance = (ntime - throttle_last_create_time).seconds
  33. data["visit_check"] = throttle_last_create_time
  34. if allocation_seconds_balance >= settings.ALLOCATION_SECONDS:
  35. return True
  36. else:
  37. if throttle_count >= settings.GET_THROTTLE:
  38. return False
  39. else:
  40. return True
  41. elif request.method.lower() == "post":
  42. ntime = timezone.now()
  43. ctime = ntime - timezone.timedelta(seconds=1)
  44. throttle_ctimelist = ListModel.objects.filter(method="post", create_time__lte=ctime)
  45. for i in throttle_ctimelist:
  46. i.delete()
  47. t_code = Md5.md5(ip)
  48. throttle_allocationlist = ListModel.objects.filter(openid=openid, appid=appid, ip=ip,
  49. method='post').order_by('id')
  50. throttle_count = throttle_allocationlist.count()
  51. if throttle_count == 0:
  52. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="post", t_code=t_code)
  53. return True
  54. else:
  55. throttle_last_create_time = throttle_allocationlist.first().create_time
  56. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="post", t_code=t_code)
  57. allocation_seconds_balance = (ntime - throttle_last_create_time).seconds
  58. data["visit_check"] = throttle_last_create_time
  59. if allocation_seconds_balance >= settings.ALLOCATION_SECONDS:
  60. return True
  61. else:
  62. if throttle_count >= settings.POST_THROTTLE:
  63. return False
  64. else:
  65. return True
  66. elif request.method.lower() == "put":
  67. ntime = timezone.now()
  68. ctime = ntime - timezone.timedelta(seconds=1)
  69. throttle_ctimelist = ListModel.objects.filter(method="put", create_time__lte=ctime)
  70. for i in throttle_ctimelist:
  71. i.delete()
  72. t_code = Md5.md5(ip)
  73. throttle_allocationlist = ListModel.objects.filter(openid=openid, appid=appid, ip=ip,
  74. method='put').order_by('id')
  75. throttle_count = throttle_allocationlist.count()
  76. if throttle_count == 0:
  77. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="put", t_code=t_code)
  78. return True
  79. else:
  80. throttle_last_create_time = throttle_allocationlist.first().create_time
  81. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="put", t_code=t_code)
  82. allocation_seconds_balance = (ntime - throttle_last_create_time).seconds
  83. data["visit_check"] = throttle_last_create_time
  84. if allocation_seconds_balance >= settings.ALLOCATION_SECONDS:
  85. return True
  86. else:
  87. if throttle_count >= settings.PUT_THROTTLE:
  88. return False
  89. else:
  90. return True
  91. elif request.method.lower() == "patch":
  92. ntime = timezone.now()
  93. ctime = ntime - timezone.timedelta(seconds=1)
  94. throttle_ctimelist = ListModel.objects.filter(method="patch", create_time__lte=ctime)
  95. for i in throttle_ctimelist:
  96. i.delete()
  97. t_code = Md5.md5(ip)
  98. throttle_allocationlist = ListModel.objects.filter(openid=openid, appid=appid, ip=ip,
  99. method='patch').order_by('id')
  100. throttle_count = throttle_allocationlist.count()
  101. if throttle_count == 0:
  102. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="patch", t_code=t_code)
  103. return True
  104. else:
  105. throttle_last_create_time = throttle_allocationlist.first().create_time
  106. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="patch", t_code=t_code)
  107. allocation_seconds_balance = (ntime - throttle_last_create_time).seconds
  108. data["visit_check"] = throttle_last_create_time
  109. if allocation_seconds_balance >= settings.ALLOCATION_SECONDS:
  110. return True
  111. else:
  112. if throttle_count >= settings.PATCH_THROTTLE:
  113. return False
  114. else:
  115. return True
  116. elif request.method.lower() == "delete":
  117. ntime = timezone.now()
  118. ctime = ntime - timezone.timedelta(seconds=1)
  119. throttle_ctimelist = ListModel.objects.filter(method="delete", create_time__lte=ctime)
  120. for i in throttle_ctimelist:
  121. i.delete()
  122. t_code = Md5.md5(ip)
  123. throttle_allocationlist = ListModel.objects.filter(openid=openid, appid=appid, ip=ip,
  124. method='delete').order_by('id')
  125. throttle_count = throttle_allocationlist.count()
  126. if throttle_count == 0:
  127. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="delete", t_code=t_code)
  128. return True
  129. else:
  130. throttle_last_create_time = throttle_allocationlist.first().create_time
  131. ListModel.objects.create(openid=openid, appid=appid, ip=ip, method="delete", t_code=t_code)
  132. allocation_seconds_balance = (ntime - throttle_last_create_time).seconds
  133. data["visit_check"] = throttle_last_create_time
  134. if allocation_seconds_balance >= settings.ALLOCATION_SECONDS:
  135. return True
  136. else:
  137. if throttle_count >= settings.DELETE_THROTTLE:
  138. return False
  139. else:
  140. return True
  141. else:
  142. return False
  143. def wait(self):
  144. ctime = timezone.now()
  145. wait_time = (ctime - data["visit_check"]).seconds
  146. balance_time = 1 - wait_time
  147. return balance_time