build rate limiting: use a rate
This commit is contained in:
parent
7877c6ab94
commit
57770493fa
3 changed files with 18 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
|||
import logging
|
||||
import json
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import request
|
||||
|
||||
from app import app, dockerfile_build_queue
|
||||
|
@ -14,7 +16,8 @@ from util.morecollections import AttrDict
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_BUILD_QUEUE_SIZE = app.config.get('MAX_BUILD_QUEUE_SIZE', -1)
|
||||
MAX_BUILD_QUEUE_ITEMS = app.config.get('MAX_BUILD_QUEUE_ITEMS', -1)
|
||||
MAX_BUILD_QUEUE_SECS = app.config.get('MAX_BUILD_QUEUE_SECS', -1)
|
||||
|
||||
|
||||
class MaximumBuildsQueuedException(Exception):
|
||||
|
@ -23,9 +26,11 @@ class MaximumBuildsQueuedException(Exception):
|
|||
|
||||
def start_build(repository, prepared_build, pull_robot_name=None):
|
||||
queue_item_prefix = '%s/%s' % repository.namespace_user.username, repository.name
|
||||
available_builds = dockerfile_build_queue.num_available_jobs(queue_item_prefix)
|
||||
if available_builds >= MAX_BUILD_QUEUE_SIZE and MAX_BUILD_QUEUE_SIZE > -1:
|
||||
raise MaximumBuildsQueuedException()
|
||||
if MAX_BUILD_QUEUE_ITEMS > 0 and MAX_BUILD_QUEUE_SECS > 0:
|
||||
available_min = datetime.utcnow() - timedelta(seconds=-MAX_BUILD_QUEUE_SECS)
|
||||
available_builds = dockerfile_build_queue.num_available_jobs(available_min, queue_item_prefix)
|
||||
if available_builds > MAX_BUILD_QUEUE_ITEMS:
|
||||
raise MaximumBuildsQueuedException()
|
||||
|
||||
host = app.config['SERVER_HOSTNAME']
|
||||
repo_path = '%s/%s/%s' % (host, repository.namespace_user.username, repository.name)
|
||||
|
|
Reference in a new issue