diff --git a/swift/common/middleware/slo.py b/swift/common/middleware/slo.py index 65dde1823b..f500f598b5 100644 --- a/swift/common/middleware/slo.py +++ b/swift/common/middleware/slo.py @@ -157,6 +157,11 @@ from swift.common.middleware.bulk import get_response_body, \ ACCEPTABLE_FORMATS, Bulk +DEFAULT_MIN_SEGMENT_SIZE = 1024 * 1024 # 1 MiB +DEFAULT_MAX_MANIFEST_SEGMENTS = 1000 +DEFAULT_MAX_MANIFEST_SIZE = 1024 * 1024 * 2 # 2 MiB + + def parse_input(raw_data): """ Given a request will parse the body and return a list of dictionaries @@ -575,16 +580,15 @@ class StaticLargeObject(object): :param conf: The configuration dict for the middleware. """ - def __init__(self, app, conf): + def __init__(self, app, conf, min_segment_size=DEFAULT_MIN_SEGMENT_SIZE, + max_manifest_segments=DEFAULT_MAX_MANIFEST_SEGMENTS, + max_manifest_size=DEFAULT_MAX_MANIFEST_SIZE): self.conf = conf self.app = app self.logger = get_logger(conf, log_route='slo') - self.max_manifest_segments = int(self.conf.get('max_manifest_segments', - 1000)) - self.max_manifest_size = int(self.conf.get('max_manifest_size', - 1024 * 1024 * 2)) - self.min_segment_size = int(self.conf.get('min_segment_size', - 1024 * 1024)) + self.max_manifest_segments = max_manifest_segments + self.max_manifest_size = max_manifest_size + self.min_segment_size = min_segment_size self.max_get_time = int(self.conf.get('max_get_time', 86400)) self.rate_limit_after_segment = int(self.conf.get( 'rate_limit_after_segment', '10')) @@ -859,8 +863,23 @@ class StaticLargeObject(object): def filter_factory(global_conf, **local_conf): conf = global_conf.copy() conf.update(local_conf) - register_swift_info('slo') + + max_manifest_segments = int(conf.get('max_manifest_segments', + DEFAULT_MAX_MANIFEST_SEGMENTS)) + max_manifest_size = int(conf.get('max_manifest_size', + DEFAULT_MAX_MANIFEST_SIZE)) + min_segment_size = int(conf.get('min_segment_size', + DEFAULT_MIN_SEGMENT_SIZE)) + + register_swift_info('slo', + max_manifest_segments=max_manifest_segments, + max_manifest_size=max_manifest_size, + min_segment_size=min_segment_size) def slo_filter(app): - return StaticLargeObject(app, conf) + return StaticLargeObject( + app, conf, + max_manifest_segments=max_manifest_segments, + max_manifest_size=max_manifest_size, + min_segment_size=min_segment_size) return slo_filter diff --git a/test/unit/common/middleware/test_slo.py b/test/unit/common/middleware/test_slo.py index 9e8444c845..01090aa6fe 100644 --- a/test/unit/common/middleware/test_slo.py +++ b/test/unit/common/middleware/test_slo.py @@ -19,7 +19,7 @@ import unittest from copy import deepcopy from mock import patch from hashlib import md5 -from swift.common import swob +from swift.common import swob, utils from swift.common.exceptions import ListingIterError, SegmentError from swift.common.middleware import slo from swift.common.utils import json, split_path @@ -1318,5 +1318,21 @@ class TestSloCopyHook(SloTestCase): self.assertEqual(modified_resp.etag, md5("obj-etag").hexdigest()) +class TestSwiftInfo(unittest.TestCase): + def setUp(self): + utils._swift_info = {} + utils._swift_admin_info = {} + + def test_registered_defaults(self): + mware = slo.filter_factory({})('have to pass in an app') + swift_info = utils.get_swift_info() + self.assertTrue('slo' in swift_info) + self.assertEqual(swift_info['slo'].get('max_manifest_segments'), + mware.max_manifest_segments) + self.assertEqual(swift_info['slo'].get('min_segment_size'), + mware.min_segment_size) + self.assertEqual(swift_info['slo'].get('max_manifest_size'), + mware.max_manifest_size) + if __name__ == '__main__': unittest.main()