Skip to content

Commit

Permalink
#209: Make client handling values configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Apr 6, 2021
1 parent 416288c commit e4abe88
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
4 changes: 3 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"backend": "db",
"client_cache_size": 28,
"client_cache_size": 5,
"client_pool_init_size": 2,
"client_pool_max_capacity": 5,
"DB_URL": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb",
"ICAT_URL": "https://localhost:8181",
"icat_check_cert": false,
Expand Down
12 changes: 12 additions & 0 deletions datagateway_api/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def get_client_cache_size(self):
except KeyError:
sys.exit("Missing config value, client_cache_size")

def get_client_pool_init_size(self):
try:
return self.config["client_pool_init_size"]
except KeyError:
sys.exit("Missing config value, client_pool_init_size")

def get_client_pool_max_capacity(self):
try:
return self.config["client_pool_max_capacity"]
except KeyError:
sys.exit("Missing config value, client_pool_max_capacity")

def get_db_url(self):
try:
return self.config["DB_URL"]
Expand Down
6 changes: 5 additions & 1 deletion datagateway_api/common/icat/icat_client_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def create_client_pool():
"""

return ObjectPool(
ICATClient, min_init=5, max_capacity=20, max_reusable=0, expires=0,
ICATClient,
min_init=config.get_client_pool_init_size(),
max_capacity=config.get_client_pool_max_capacity(),
max_reusable=0,
expires=0,
)


Expand Down
4 changes: 3 additions & 1 deletion datagateway_api/common/icat/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from cachetools.lru import LRUCache

from datagateway_api.common.config import config

log = logging.getLogger()


Expand All @@ -17,7 +19,7 @@ class ExtendedLRUCache(LRUCache):
"""

def __init__(self):
super().__init__(maxsize=8)
super().__init__(maxsize=config.get_client_cache_size())

def popitem(self):
key, client = super().popitem()
Expand Down
22 changes: 21 additions & 1 deletion test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,33 @@ def test_invalid_backend_type(self, invalid_config):
class TestGetClientCacheSize:
def test_valid_client_cache_size(self, valid_config):
cache_size = valid_config.get_client_cache_size()
assert cache_size == 28
assert cache_size == 5

def test_invalid_client_cache_size(self, invalid_config):
with pytest.raises(SystemExit):
invalid_config.get_client_cache_size()


class TestGetClientPoolInitSize:
def test_valid_client_pool_init_size(self, valid_config):
pool_init_size = valid_config.get_client_pool_init_size()
assert pool_init_size == 2

def test_invalid_client_cache_size(self, invalid_config):
with pytest.raises(SystemExit):
invalid_config.get_client_pool_init_size()


class TestGetClientPoolMaxCapacity:
def test_valid_client_pool_init_size(self, valid_config):
pool_max_capacity = valid_config.get_client_pool_max_capacity()
assert pool_max_capacity == 5

def test_invalid_client_cache_size(self, invalid_config):
with pytest.raises(SystemExit):
invalid_config.get_client_pool_max_capacity()


class TestGetDBURL:
def test_valid_db_url(self, valid_config):
db_url = valid_config.get_db_url()
Expand Down

0 comments on commit e4abe88

Please sign in to comment.