Don't use hasattr to lazy-load properties

This is a better pattern overall and most classes
has already had this changed as part of the service
refactor.

Change-Id: Ia6fb1f78a449bee48b9e4c86ca1334d0b28b28ef
This commit is contained in:
Erik Olof Gunnar Andersson 2019-10-20 15:37:24 -07:00
parent e7558b6c5c
commit 8fee76623e
3 changed files with 32 additions and 14 deletions

View File

@ -41,12 +41,14 @@ TSIG_RRSIZE = 10 + 64 + 160 + 1
class RequestHandler(xfr.XFRMixin): class RequestHandler(xfr.XFRMixin):
def __init__(self, storage, tg): def __init__(self, storage, tg):
self._central_api = None
self.storage = storage self.storage = storage
self.tg = tg self.tg = tg
@property @property
def central_api(self): def central_api(self):
if not hasattr(self, '_central_api'): if not self._central_api:
self._central_api = central_api.CentralAPI.get_instance() self._central_api = central_api.CentralAPI.get_instance()
return self._central_api return self._central_api

View File

@ -32,46 +32,55 @@ class TaskConfig(object):
Configuration mixin for the various configuration settings that Configuration mixin for the various configuration settings that
a task may want to access a task may want to access
""" """
def __init__(self):
self._config = None
self._delay = None
self._max_prop_time = None
self._max_retries = None
self._retry_interval = None
self._timeout = None
self._threshold_percentage = None
@property @property
def config(self): def config(self):
if not hasattr(self, '_config'): if not self._config:
self._config = CONF['service:worker'] self._config = CONF['service:worker']
return self._config return self._config
@property @property
def threshold_percentage(self): def threshold_percentage(self):
if not hasattr(self, '_threshold_percentage'): if self._threshold_percentage is None:
self._threshold_percentage = self.config.threshold_percentage self._threshold_percentage = self.config.threshold_percentage
return self._threshold_percentage return self._threshold_percentage
@property @property
def timeout(self): def timeout(self):
if not hasattr(self, '_timeout'): if self._timeout is None:
self._timeout = self.config.poll_timeout self._timeout = self.config.poll_timeout
return self._timeout return self._timeout
@property @property
def retry_interval(self): def retry_interval(self):
if not hasattr(self, '_retry_interval'): if self._retry_interval is None:
self._retry_interval = self.config.poll_retry_interval self._retry_interval = self.config.poll_retry_interval
return self._retry_interval return self._retry_interval
@property @property
def max_retries(self): def max_retries(self):
if not hasattr(self, '_max_retries'): if self._max_retries is None:
self._max_retries = self.config.poll_max_retries self._max_retries = self.config.poll_max_retries
return self._max_retries return self._max_retries
@property @property
def delay(self): def delay(self):
if not hasattr(self, '_delay'): if self._delay is None:
self._delay = self.config.poll_delay self._delay = self.config.poll_delay
return self._delay return self._delay
@property @property
def max_prop_time(self): def max_prop_time(self):
# Compute a time (seconds) by which things should have propagated # Compute a time (seconds) by which things should have propagated
if not hasattr(self, '_max_prop_time'): if self._max_prop_time is None:
self._max_prop_time = utils.max_prop_time( self._max_prop_time = utils.max_prop_time(
self.timeout, self.timeout,
self.max_retries, self.max_retries,
@ -92,13 +101,20 @@ class Task(TaskConfig):
- Can optionally return something - Can optionally return something
""" """
def __init__(self, executor, **kwargs): def __init__(self, executor, **kwargs):
super(Task, self).__init__()
self._storage = None
self._quota = None
self._central_api = None
self._worker_api = None
self.executor = executor self.executor = executor
self.task_name = self.__class__.__name__ self.task_name = self.__class__.__name__
self.options = {} self.options = {}
@property @property
def storage(self): def storage(self):
if not hasattr(self, '_storage'): if not self._storage:
# Get a storage connection # Get a storage connection
storage_driver = cfg.CONF['service:central'].storage_driver storage_driver = cfg.CONF['service:central'].storage_driver
self._storage = storage.get_storage(storage_driver) self._storage = storage.get_storage(storage_driver)
@ -106,20 +122,20 @@ class Task(TaskConfig):
@property @property
def quota(self): def quota(self):
if not hasattr(self, '_quota'): if not self._quota:
# Get a quota manager instance # Get a quota manager instance
self._quota = quota.get_quota() self._quota = quota.get_quota()
return self._quota return self._quota
@property @property
def central_api(self): def central_api(self):
if not hasattr(self, '_central_api'): if not self._central_api:
self._central_api = central_rpcapi.CentralAPI.get_instance() self._central_api = central_rpcapi.CentralAPI.get_instance()
return self._central_api return self._central_api
@property @property
def worker_api(self): def worker_api(self):
if not hasattr(self, '_worker_api'): if not self._worker_api:
self._worker_api = worker_rpcapi.WorkerAPI.get_instance() self._worker_api = worker_rpcapi.WorkerAPI.get_instance()
return self._worker_api return self._worker_api

View File

@ -155,7 +155,7 @@ class ZoneActor(base.Task, ThresholdMixin):
of targets (bool) of targets (bool)
""" """
def __init__(self, executor, context, pool, zone): def __init__(self, executor, context, pool, zone):
self.executor = executor super(ZoneActor, self).__init__(executor)
self.context = context self.context = context
self.pool = pool self.pool = pool
self.zone = zone self.zone = zone
@ -365,7 +365,7 @@ class ZonePoller(base.Task, ThresholdMixin):
number of nameservers in the pool number of nameservers in the pool
""" """
def __init__(self, executor, context, pool, zone): def __init__(self, executor, context, pool, zone):
self.executor = executor super(ZonePoller, self).__init__(executor)
self.context = context self.context = context
self.pool = pool self.pool = pool
self.zone = zone self.zone = zone