Ejemplo n.º 1
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"
Ejemplo 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})
Ejemplo n.º 3
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)