Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make task rate limiting opt in #4416

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions configs/test/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ coverage:
# Bucket to load code coverage information from.
bucket: test-coverage-bucket

rate_limit_tasks:
enabled: true

logs:
fuzzer:
# Bucket to store logs for fuzzer runs.
Expand Down
13 changes: 13 additions & 0 deletions src/clusterfuzz/_internal/base/tasks/task_rate_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import datetime

from clusterfuzz._internal.base import memoize
from clusterfuzz._internal.config import local_config
from clusterfuzz._internal.datastore import data_types
from clusterfuzz._internal.datastore import ndb_utils
from clusterfuzz._internal.metrics import logs
Expand All @@ -33,6 +35,13 @@ def _get_datetime_now():
_UTASK_PSEUDO_TASKS = {'uworker_main', 'postprocess', 'preprocess'}


@memoize.wrap(memoize.FifoInMemory(1))
def optin_to_task_rate_limiting():
enabled = local_config.ProjectConfig().get('rate_limit_tasks.enabled', False)
logs.info(f'Using async HTTP: {enabled}.')
return enabled


class TaskRateLimiter:
"""Rate limiter for tasks. This limits tasks to 100 erroneous runs or 2000
succesful runs in 6 hours. It keeps track of task completion when record_task
Expand All @@ -51,6 +60,8 @@ def __str__(self):

def record_task(self, success: bool) -> None:
"""Records a task and whether it completed succesfully."""
if not optin_to_task_rate_limiting():
return
if self.task_name in _UTASK_PSEUDO_TASKS:
# Don't rate limit these fake uworker tasks.
return
Expand All @@ -67,6 +78,8 @@ def record_task(self, success: bool) -> None:

def is_rate_limited(self) -> bool:
"""Checks if the given task is rate limited."""
if not optin_to_task_rate_limiting():
return False
if self.task_name in _UTASK_PSEUDO_TASKS:
# Don't rate limit these fake tasks.
return False
Expand Down
Loading