From c41de8ded6e866166640a09ac31a27ff6fed0fb8 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 6 Dec 2016 20:40:54 -0500 Subject: [PATCH] build queue rate limiting: address PR comments --- data/queue.py | 2 +- endpoints/api/build.py | 1 + endpoints/building.py | 14 +++++++++----- endpoints/webhooks.py | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/data/queue.py b/data/queue.py index 659a485b0..07135b746 100644 --- a/data/queue.py +++ b/data/queue.py @@ -79,7 +79,7 @@ class WorkQueue(object): def num_available_jobs_between(self, available_min_time, available_max_time, prefix): """ - Returns the number of available queue items with a given prefix between the two provided times. + Returns the number of available queue items with a given prefix, between the two provided times. """ prefix = prefix.lstrip('/') available = self._available_jobs(available_max_time, diff --git a/endpoints/api/build.py b/endpoints/api/build.py index 36daf2bde..0b2ef5464 100644 --- a/endpoints/api/build.py +++ b/endpoints/api/build.py @@ -291,6 +291,7 @@ class RepositoryBuildList(RepositoryParamResource): build_request = start_build(repo, prepared, pull_robot_name=pull_robot_name) except MaximumBuildsQueuedException: abort(429, message='Maximum queued build rate exceeded.') + resp = build_status_view(build_request) repo_string = '%s/%s' % (namespace, repository) headers = { diff --git a/endpoints/building.py b/endpoints/building.py index d2684c8b8..83fd9e028 100644 --- a/endpoints/building.py +++ b/endpoints/building.py @@ -16,23 +16,27 @@ from util.morecollections import AttrDict logger = logging.getLogger(__name__) -MAX_BUILD_QUEUE_ITEMS = app.config.get('MAX_BUILD_QUEUE_ITEMS', -1) -MAX_BUILD_QUEUE_SECS = app.config.get('MAX_BUILD_QUEUE_SECS', -1) +MAX_BUILD_QUEUE_RATE_ITEMS = app.config.get('MAX_BUILD_QUEUE_RATE_ITEMS', -1) +MAX_BUILD_QUEUE_RATE_SECS = app.config.get('MAX_BUILD_QUEUE_RATE_SECS', -1) class MaximumBuildsQueuedException(Exception): + """ + This exception is raised when a build is requested, but the incoming build + would exceed the configured maximum build rate. + """ pass def start_build(repository, prepared_build, pull_robot_name=None): queue_item_prefix = '%s/%s' % (repository.namespace_user.username, repository.name) - if MAX_BUILD_QUEUE_ITEMS > 0 and MAX_BUILD_QUEUE_SECS > 0: + if MAX_BUILD_QUEUE_RATE_ITEMS > 0 and MAX_BUILD_QUEUE_RATE_SECS > 0: now = datetime.utcnow() - available_min = now - timedelta(seconds=MAX_BUILD_QUEUE_SECS) + available_min = now - timedelta(seconds=MAX_BUILD_QUEUE_RATE_SECS) available_builds = dockerfile_build_queue.num_available_jobs_between(available_min, now, queue_item_prefix) - if available_builds > MAX_BUILD_QUEUE_ITEMS: + if available_builds >= MAX_BUILD_QUEUE_RATE_ITEMS: raise MaximumBuildsQueuedException() host = app.config['SERVER_HOSTNAME'] diff --git a/endpoints/webhooks.py b/endpoints/webhooks.py index c89d9b26e..79a3e58d1 100644 --- a/endpoints/webhooks.py +++ b/endpoints/webhooks.py @@ -107,7 +107,7 @@ def build_trigger_webhook(trigger_uuid, **kwargs): try: start_build(repo, prepared, pull_robot_name=pull_robot_name) except MaximumBuildsQueuedException: - abort(429) + abort(429, message='Maximum queued build rate exceeded.') return make_response('Okay')