throttle.py 8.4 KB

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