Exemplo n.º 1
0
Arquivo: views.py Projeto: mcmdhr/CSOJ
def _submit_code(user, problem_id, language, code):
    controller = BucketController(user_id=user.id,
                                  redis_conn=redis.Redis(host=settings.REDIS_CACHE["host"],
                                                         port=settings.REDIS_CACHE["port"],
                                                         db=settings.REDIS_CACHE["db"]),
                                  default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY)
    bucket = TokenBucket(fill_rate=settings.TOKEN_BUCKET_FILL_RATE,
                         capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY,
                         last_capacity=controller.last_capacity,
                         last_timestamp=controller.last_timestamp)
    if bucket.consume():
        controller.last_capacity -= 1
    else:
        return error_response(u"您提交的频率过快, 请等待%d秒" % int(bucket.expected_time() + 1))

    try:
        problem = Problem.objects.get(id=problem_id)
    except Problem.DoesNotExist:
        return error_response(u"题目不存在")
    submission = Submission.objects.create(user_id=user.id,
                                           language=language,
                                           code=code,
                                           problem_id=problem.id)

    try:
        _judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id,
                     problem.spj, problem.spj_language, problem.spj_code, problem.spj_version)
    except Exception as e:
        logger.error(e)
        return error_response(u"提交判题任务失败")
    return success_response({"submission_id": submission.id})
Exemplo n.º 2
0
def _submit_code(user, problem_id, language, code):
    controller = BucketController(user_id=user.id,
                                  redis_conn=redis.Redis(host=settings.REDIS_CACHE["host"],
                                                         port=settings.REDIS_CACHE["port"],
                                                         db=settings.REDIS_CACHE["db"]),
                                  default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY)
    bucket = TokenBucket(fill_rate=settings.TOKEN_BUCKET_FILL_RATE,
                         capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY,
                         last_capacity=controller.last_capacity,
                         last_timestamp=controller.last_timestamp)
    if bucket.consume():
        controller.last_capacity -= 1
    else:
        return error_response(u"您提交的频率过快, 请等待%d秒" % int(bucket.expected_time() + 1))

    try:
        problem = Problem.objects.get(id=problem_id)
    except Problem.DoesNotExist:
        return error_response(u"题目不存在")
    submission = Submission.objects.create(user_id=user.id,
                                           language=language,
                                           code=code,
                                           problem_id=problem.id)

    try:
        _judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id,
                     problem.spj, problem.spj_language, problem.spj_code, problem.spj_version)
    except Exception as e:
        logger.error(e)
        return error_response(u"提交判题任务失败")
    return success_response({"submission_id": submission.id})
Exemplo n.º 3
0
 def throttling(self, request):
     user_bucket = TokenBucket(key=str(request.user.id),
                               redis_conn=cache,
                               **SysOptions.throttling["user"])
     can_consume, wait = user_bucket.consume()
     if not can_consume:
         return "Please wait %d seconds" % (int(wait))
Exemplo n.º 4
0
 def throttling(self, request):
     # 使用 open_api 的请求暂不做限制
     auth_method = getattr(request, "auth_method", "")
     if auth_method == "api_key":
         return
     user_bucket = TokenBucket(key=str(request.user.id),
                               redis_conn=cache, **SysOptions.throttling["user"])
     can_consume, wait = user_bucket.consume()
     if not can_consume:
         return "Please wait %d seconds" % (int(wait))
Exemplo n.º 5
0
 def throttling(self, request, is_super_admin):
     # 超级管理员 的请求暂不做限制
     if is_super_admin:
         return
     user_bucket = TokenBucket(key=str(request.user.username),
                               redis_conn=cache,
                               capacity=2,
                               fill_rate=0.03,
                               default_capacity=2)
     can_consume, wait = user_bucket.consume()
     if not can_consume:
         return "Please wait %d seconds" % (int(wait))
Exemplo n.º 6
0
    def throttling(self, request):
        user_controller = BucketController(
            factor=request.user.id,
            redis_conn=cache,
            default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY)
        user_bucket = TokenBucket(
            fill_rate=settings.TOKEN_BUCKET_FILL_RATE,
            capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY,
            last_capacity=user_controller.last_capacity,
            last_timestamp=user_controller.last_timestamp)
        if user_bucket.consume():
            user_controller.last_capacity -= 1
        else:
            return "Please wait %d seconds" % int(user_bucket.expected_time() +
                                                  1)

        ip_controller = BucketController(
            factor=request.session["ip"],
            redis_conn=cache,
            default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY * 3)

        ip_bucket = TokenBucket(
            fill_rate=settings.TOKEN_BUCKET_FILL_RATE * 3,
            capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY * 3,
            last_capacity=ip_controller.last_capacity,
            last_timestamp=ip_controller.last_timestamp)
        if ip_bucket.consume():
            ip_controller.last_capacity -= 1
        else:
            return "Captcha is required"
Exemplo n.º 7
0
    def post(self, request):
        """
        提交代码
        ---
        request_serializer: CreateSubmissionSerializer
        """
        controller = BucketController(
            user_id=request.user.id,
            redis_conn=redis.Redis(host=settings.REDIS_CACHE["host"],
                                   port=settings.REDIS_CACHE["port"],
                                   db=settings.REDIS_CACHE["db"]),
            default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY)
        bucket = TokenBucket(fill_rate=settings.TOKEN_BUCKET_FILL_RATE,
                             capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY,
                             last_capacity=controller.last_capacity,
                             last_timestamp=controller.last_timestamp)
        if bucket.consume():
            controller.last_capacity -= 1
        else:
            return error_response(u"您提交的频率过快, 请等待%d秒" %
                                  int(bucket.expected_time() + 1))

        serializer = CreateSubmissionSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                problem = Problem.objects.get(id=data["problem_id"])
            except Problem.DoesNotExist:
                return error_response(u"题目不存在")
            submission = Submission.objects.create(user_id=request.user.id,
                                                   language=int(
                                                       data["language"]),
                                                   code=data["code"],
                                                   problem_id=problem.id)

            try:
                _judge.delay(submission, problem.time_limit,
                             problem.memory_limit, problem.test_case_id)
            except Exception as e:
                logger.error(e)
                return error_response(u"提交判题任务失败")
            return success_response({"submission_id": submission.id})
        else:
            return serializer_invalid_response(serializer)
Exemplo n.º 8
0
    def post(self, request):
        """
        提交代码
        ---
        request_serializer: CreateSubmissionSerializer
        """
        controller = BucketController(user_id=request.user.id,
                                      redis_conn=redis.Redis(host=settings.REDIS_CACHE["host"],
                                                             port=settings.REDIS_CACHE["port"],
                                                             db=settings.REDIS_CACHE["db"]),
                                      default_capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY)
        bucket = TokenBucket(fill_rate=settings.TOKEN_BUCKET_FILL_RATE,
                             capacity=settings.TOKEN_BUCKET_DEFAULT_CAPACITY,
                             last_capacity=controller.last_capacity,
                             last_timestamp=controller.last_timestamp)
        if bucket.consume():
            controller.last_capacity -= 1
        else:
            return error_response(u"您提交的频率过快, 请等待%d秒" % int(bucket.expected_time() + 1))

        serializer = CreateSubmissionSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                problem = Problem.objects.get(id=data["problem_id"])
            except Problem.DoesNotExist:
                return error_response(u"题目不存在")
            submission = Submission.objects.create(user_id=request.user.id,
                                                   language=int(data["language"]),
                                                   code=data["code"],
                                                   problem_id=problem.id)

            try:
                _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id)
            except Exception as e:
                logger.error(e)
                return error_response(u"提交判题任务失败")
            return success_response({"submission_id": submission.id})
        else:
            return serializer_invalid_response(serializer)
Exemplo n.º 9
0
 def throttling(self, request):
     user_bucket = TokenBucket(key=str(request.user.id),
                               redis_conn=cache, **SysOptions.throttling["user"])
     can_consume, wait = user_bucket.consume()
     if not can_consume:
         return "Please wait %d seconds" % (int(wait))