throttle.py 8.5 KB

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