circuitbreaker.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from __future__ import absolute_import
  2. import logging
  3. import six
  4. log = logging.getLogger(__name__)
  5. class CircuitBreakerMetaclass(type):
  6. __instances__ = dict()
  7. __blacklist__ = ('CircuitBreaker', 'CircuitBreakerMetaclass')
  8. def __new__(cls, name, bases, attrs):
  9. if name in cls.__blacklist__:
  10. return super(CircuitBreakerMetaclass, cls).__new__(cls, name,
  11. bases, attrs)
  12. class_name = attrs.get('__circuit_breaker_name__',
  13. '{}CircuitBreaker'.format(name))
  14. new_class = super(CircuitBreakerMetaclass, cls).__new__(cls,
  15. class_name,
  16. bases, attrs)
  17. setattr(new_class, 'circuit_breaker_name', class_name)
  18. if class_name not in cls.__instances__:
  19. cls.__instances__[class_name] = new_class
  20. return cls.__instances__[class_name]
  21. class CircuitBreaker(six.with_metaclass(CircuitBreakerMetaclass, object)):
  22. __circuit_breaker_name__ = None