-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
pybreaker.py
826 lines (697 loc) · 24.8 KB
/
pybreaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#-*- coding:utf-8 -*-
"""
Threadsafe pure-Python implementation of the Circuit Breaker pattern, described
by Michael T. Nygard in his book 'Release It!'.
For more information on this and other patterns and best practices, buy the
book at http://pragprog.com/titles/mnee/release-it
"""
import types
import time
import calendar
import logging
from datetime import datetime, timedelta
from functools import wraps
import threading
try:
from tornado import gen
HAS_TORNADO_SUPPORT = True
except ImportError:
HAS_TORNADO_SUPPORT = False
try:
from redis.exceptions import RedisError
HAS_REDIS_SUPPORT = True
except ImportError:
HAS_REDIS_SUPPORT = False
__all__ = (
'CircuitBreaker', 'CircuitBreakerListener', 'CircuitBreakerError',
'CircuitMemoryStorage', 'CircuitRedisStorage', 'STATE_OPEN', 'STATE_CLOSED',
'STATE_HALF_OPEN',)
STATE_OPEN = 'open'
STATE_CLOSED = 'closed'
STATE_HALF_OPEN = 'half-open'
class CircuitBreaker(object):
"""
More abstractly, circuit breakers exists to allow one subsystem to fail
without destroying the entire system.
This is done by wrapping dangerous operations (typically integration points)
with a component that can circumvent calls when the system is not healthy.
This pattern is described by Michael T. Nygard in his book 'Release It!'.
"""
def __init__(self, fail_max=5, reset_timeout=60, exclude=None,
listeners=None, state_storage=None):
"""
Creates a new circuit breaker with the given parameters.
"""
self._lock = threading.RLock()
if not state_storage:
self._state_storage = CircuitMemoryStorage(STATE_CLOSED)
else:
self._state_storage = state_storage
self._state = CircuitClosedState(self)
self._fail_max = fail_max
self._reset_timeout = reset_timeout
self._excluded_exceptions = list(exclude or [])
self._listeners = list(listeners or [])
@property
def fail_counter(self):
"""
Returns the current number of consecutive failures.
"""
return self._state_storage.counter
@property
def fail_max(self):
"""
Returns the maximum number of failures tolerated before the circuit is
opened.
"""
return self._fail_max
@fail_max.setter
def fail_max(self, number):
"""
Sets the maximum `number` of failures tolerated before the circuit is
opened.
"""
self._fail_max = number
@property
def reset_timeout(self):
"""
Once this circuit breaker is opened, it should remain opened until the
timeout period, in seconds, elapses.
"""
return self._reset_timeout
@reset_timeout.setter
def reset_timeout(self, timeout):
"""
Sets the `timeout` period, in seconds, this circuit breaker should be
kept open.
"""
self._reset_timeout = timeout
@property
def state(self):
"""
Returns the current state of this circuit breaker.
"""
with self._lock:
name = self._state_storage.state
if name != self._state.name:
if name == STATE_CLOSED:
self._state = CircuitClosedState(self, self._state, notify=True)
elif name == STATE_OPEN:
self._state = CircuitOpenState(self, self._state, notify=True)
else:
self._state = CircuitHalfOpenState(self, self._state, notify=True)
return self._state
@property
def current_state(self):
"""
Returns a string that identifies this circuit breaker's state, i.e.,
'closed', 'open', 'half-open'.
"""
return self._state_storage.state
@property
def excluded_exceptions(self):
"""
Returns the list of excluded exceptions, e.g., exceptions that should
not be considered system errors by this circuit breaker.
"""
return tuple(self._excluded_exceptions)
def add_excluded_exception(self, exception):
"""
Adds an exception to the list of excluded exceptions.
"""
with self._lock:
self._excluded_exceptions.append(exception)
def add_excluded_exceptions(self, *exceptions):
"""
Adds exceptions to the list of excluded exceptions.
"""
for exc in exceptions:
self.add_excluded_exception(exc)
def remove_excluded_exception(self, exception):
"""
Removes an exception from the list of excluded exceptions.
"""
with self._lock:
self._excluded_exceptions.remove(exception)
def _inc_counter(self):
"""
Increments the counter of failed calls.
"""
self._state_storage.increment_counter()
def is_system_error(self, exception):
"""
Returns whether the exception `exception` is considered a signal of
system malfunction. Business exceptions should not cause this circuit
breaker to open.
"""
texc = type(exception)
for exc in self._excluded_exceptions:
if issubclass(texc, exc):
return False
return True
def call(self, func, *args, **kwargs):
"""
Calls `func` with the given `args` and `kwargs` according to the rules
implemented by the current state of this circuit breaker.
"""
with self._lock:
return self.state.call(func, *args, **kwargs)
def call_async(self, func, *args, **kwargs):
"""
Calls async `func` with the given `args` and `kwargs` according to the rules
implemented by the current state of this circuit breaker.
Return a closure to prevent import errors when using without tornado present
"""
@gen.coroutine
def wrapped():
with self._lock:
ret = yield self.state.call_async(func, *args, **kwargs)
raise gen.Return(ret)
return wrapped()
def open(self):
"""
Opens the circuit, e.g., the following calls will immediately fail
until timeout elapses.
"""
with self._lock:
self._state_storage.state = STATE_OPEN
self._state = CircuitOpenState(self, self._state, notify=True)
def half_open(self):
"""
Half-opens the circuit, e.g. lets the following call pass through and
opens the circuit if the call fails (or closes the circuit if the call
succeeds).
"""
with self._lock:
self._state_storage.state = STATE_HALF_OPEN
self._state = CircuitHalfOpenState(self, self._state, notify=True)
def close(self):
"""
Closes the circuit, e.g. lets the following calls execute as usual.
"""
with self._lock:
self._state_storage.state = STATE_CLOSED
self._state = CircuitClosedState(self, self._state, notify=True)
def __call__(self, *call_args, **call_kwargs):
"""
Returns a wrapper that calls the function `func` according to the rules
implemented by the current state of this circuit breaker.
Optionally takes the keyword argument `__pybreaker_call_coroutine`,
which will will call `func` as a Tornado co-routine.
"""
call_async = call_kwargs.pop('__pybreaker_call_async', False)
if call_async and not HAS_TORNADO_SUPPORT:
raise ImportError('No module named tornado')
def _outer_wrapper(func):
@wraps(func)
def _inner_wrapper(*args, **kwargs):
if call_async:
return self.call_async(func, *args, **kwargs)
return self.call(func, *args, **kwargs)
return _inner_wrapper
if call_args:
return _outer_wrapper(*call_args)
return _outer_wrapper
@property
def listeners(self):
"""
Returns the registered listeners as a tuple.
"""
return tuple(self._listeners)
def add_listener(self, listener):
"""
Registers a listener for this circuit breaker.
"""
with self._lock:
self._listeners.append(listener)
def add_listeners(self, *listeners):
"""
Registers listeners for this circuit breaker.
"""
for listener in listeners:
self.add_listener(listener)
def remove_listener(self, listener):
"""
Unregisters a listener of this circuit breaker.
"""
with self._lock:
self._listeners.remove(listener)
class CircuitBreakerStorage(object):
"""
Defines the underlying storage for a circuit breaker - the underlying
implementation should be in a subclass that overrides the method this
class defines.
"""
def __init__(self, name):
"""
Creates a new instance identified by `name`.
"""
self._name = name
@property
def name(self):
"""
Returns a human friendly name that identifies this state.
"""
return self._name
@property
def state(self):
"""
Override this method to retrieve the current circuit breaker state.
"""
pass
@state.setter
def state(self, state):
"""
Override this method to set the current circuit breaker state.
"""
pass
def increment_counter(self):
"""
Override this method to increase the failure counter by one.
"""
pass
def reset_counter(self):
"""
Override this method to set the failure counter to zero.
"""
pass
@property
def counter(self):
"""
Override this method to retrieve the current value of the failure counter.
"""
pass
@property
def opened_at(self):
"""
Override this method to retrieve the most recent value of when the
circuit was opened.
"""
pass
@opened_at.setter
def opened_at(self, datetime):
"""
Override this method to set the most recent value of when the circuit
was opened.
"""
pass
class CircuitMemoryStorage(CircuitBreakerStorage):
"""
Implements a `CircuitBreakerStorage` in local memory.
"""
def __init__(self, state):
"""
Creates a new instance with the given `state`.
"""
super(CircuitMemoryStorage, self).__init__('memory')
self._fail_counter = 0
self._opened_at = None
self._state = state
@property
def state(self):
"""
Returns the current circuit breaker state.
"""
return self._state
@state.setter
def state(self, state):
"""
Set the current circuit breaker state to `state`.
"""
self._state = state
def increment_counter(self):
"""
Increases the failure counter by one.
"""
self._fail_counter += 1
def reset_counter(self):
"""
Sets the failure counter to zero.
"""
self._fail_counter = 0
@property
def counter(self):
"""
Returns the current value of the failure counter.
"""
return self._fail_counter
@property
def opened_at(self):
"""
Returns the most recent value of when the circuit was opened.
"""
return self._opened_at
@opened_at.setter
def opened_at(self, datetime):
"""
Sets the most recent value of when the circuit was opened to
`datetime`.
"""
self._opened_at = datetime
class CircuitRedisStorage(CircuitBreakerStorage):
"""
Implements a `CircuitBreakerStorage` using redis.
"""
BASE_NAMESPACE = 'pybreaker'
logger = logging.getLogger(__name__)
def __init__(self, state, redis_object, namespace=None, fallback_circuit_state=STATE_CLOSED):
"""
Creates a new instance with the given `state` and `redis` object. The
redis object should be similar to pyredis' StrictRedis class. If there
are any connection issues with redis, the `fallback_circuit_state` is
used to determine the state of the circuit.
"""
# Module does not exist, so this feature is not available
if not HAS_REDIS_SUPPORT:
raise ImportError("CircuitRedisStorage can only be used if the required dependencies exist")
super(CircuitRedisStorage, self).__init__('redis')
try:
self.RedisError = __import__('redis').exceptions.RedisError
except ImportError:
# Module does not exist, so this feature is not available
raise ImportError("CircuitRedisStorage can only be used if 'redis' is available")
self._redis = redis_object
self._namespace_name = namespace
self._fallback_circuit_state = fallback_circuit_state
self._redis.setnx(self._namespace('fail_counter'), 0)
self._redis.setnx(self._namespace('state'), str(state))
@property
def state(self):
"""
Returns the current circuit breaker state.
"""
try:
return self._redis.get(self._namespace('state')).decode('utf-8')
except self.RedisError:
self.logger.error('RedisError: falling back to default circuit state', exc_info=True)
return self._fallback_circuit_state
@state.setter
def state(self, state):
"""
Set the current circuit breaker state to `state`.
"""
try:
self._redis.set(self._namespace('state'), str(state))
except self.RedisError:
self.logger.error('RedisError', exc_info=True)
pass
def increment_counter(self):
"""
Increases the failure counter by one.
"""
try:
self._redis.incr(self._namespace('fail_counter'))
except self.RedisError:
self.logger.error('RedisError', exc_info=True)
pass
def reset_counter(self):
"""
Sets the failure counter to zero.
"""
try:
self._redis.set(self._namespace('fail_counter'), 0)
except self.RedisError:
self.logger.error('RedisError', exc_info=True)
pass
@property
def counter(self):
"""
Returns the current value of the failure counter.
"""
try:
value = self._redis.get(self._namespace('fail_counter'))
if value:
return int(value)
else:
return 0
except self.RedisError:
self.logger.error('RedisError: Assuming no errors', exc_info=True)
return 0
@property
def opened_at(self):
"""
Returns a datetime object of the most recent value of when the circuit
was opened.
"""
try:
timestamp = self._redis.get(self._namespace('opened_at'))
if timestamp:
return datetime(*time.gmtime(int(timestamp))[:6])
except self.RedisError:
self.logger.error('RedisError', exc_info=True)
return None
@opened_at.setter
def opened_at(self, now):
"""
Atomically sets the most recent value of when the circuit was opened
to `now`. Stored in redis as a simple integer of unix epoch time.
To avoid timezone issues between different systems, the passed in
datetime should be in UTC.
"""
try:
key = self._namespace('opened_at')
def set_if_greater(pipe):
current_value = pipe.get(key)
next_value = int(calendar.timegm(now.timetuple()))
pipe.multi()
if not current_value or next_value > int(current_value):
pipe.set(key, next_value)
self._redis.transaction(set_if_greater, key)
except self.RedisError:
self.logger.error('RedisError', exc_info=True)
pass
def _namespace(self, key):
name_parts = [self.BASE_NAMESPACE, key]
if self._namespace_name:
name_parts.insert(0, self._namespace_name)
return ':'.join(name_parts)
class CircuitBreakerListener(object):
"""
Listener class used to plug code to a ``CircuitBreaker`` instance when
certain events happen.
"""
def before_call(self, cb, func, *args, **kwargs):
"""
This callback function is called before the circuit breaker `cb` calls
`fn`.
"""
pass
def failure(self, cb, exc):
"""
This callback function is called when a function called by the circuit
breaker `cb` fails.
"""
pass
def success(self, cb):
"""
This callback function is called when a function called by the circuit
breaker `cb` succeeds.
"""
pass
def state_change(self, cb, old_state, new_state):
"""
This callback function is called when the state of the circuit breaker
`cb` state changes.
"""
pass
class CircuitBreakerState(object):
"""
Implements the behavior needed by all circuit breaker states.
"""
def __init__(self, cb, name):
"""
Creates a new instance associated with the circuit breaker `cb` and
identified by `name`.
"""
self._breaker = cb
self._name = name
@property
def name(self):
"""
Returns a human friendly name that identifies this state.
"""
return self._name
def _handle_error(self, exc):
"""
Handles a failed call to the guarded operation.
"""
if self._breaker.is_system_error(exc):
self._breaker._inc_counter()
for listener in self._breaker.listeners:
listener.failure(self._breaker, exc)
self.on_failure(exc)
else:
self._handle_success()
raise exc
def _handle_success(self):
"""
Handles a successful call to the guarded operation.
"""
self._breaker._state_storage.reset_counter()
self.on_success()
for listener in self._breaker.listeners:
listener.success(self._breaker)
def call(self, func, *args, **kwargs):
"""
Calls `func` with the given `args` and `kwargs`, and updates the
circuit breaker state according to the result.
"""
ret = None
self.before_call(func, *args, **kwargs)
for listener in self._breaker.listeners:
listener.before_call(self._breaker, func, *args, **kwargs)
try:
ret = func(*args, **kwargs)
if isinstance(ret, types.GeneratorType):
return self.generator_call(ret)
except BaseException as e:
self._handle_error(e)
else:
self._handle_success()
return ret
def call_async(self, func, *args, **kwargs):
"""
Calls async `func` with the given `args` and `kwargs`, and updates the
circuit breaker state according to the result.
Return a closure to prevent import errors when using without tornado present
"""
@gen.coroutine
def wrapped():
ret = None
self.before_call(func, *args, **kwargs)
for listener in self._breaker.listeners:
listener.before_call(self._breaker, func, *args, **kwargs)
try:
ret = yield func(*args, **kwargs)
if isinstance(ret, types.GeneratorType):
raise gen.Return(self.generator_call(ret))
except BaseException as e:
self._handle_error(e)
else:
self._handle_success()
raise gen.Return(ret)
return wrapped()
def generator_call(self, wrapped_generator):
try:
value = yield next(wrapped_generator)
while True:
value = yield wrapped_generator.send(value)
except StopIteration:
self._handle_success()
raise
except BaseException as e:
self._handle_error(e)
def before_call(self, func, *args, **kwargs):
"""
Override this method to be notified before a call to the guarded
operation is attempted.
"""
pass
def on_success(self):
"""
Override this method to be notified when a call to the guarded
operation succeeds.
"""
pass
def on_failure(self, exc):
"""
Override this method to be notified when a call to the guarded
operation fails.
"""
pass
class CircuitClosedState(CircuitBreakerState):
"""
In the normal "closed" state, the circuit breaker executes operations as
usual. If the call succeeds, nothing happens. If it fails, however, the
circuit breaker makes a note of the failure.
Once the number of failures exceeds a threshold, the circuit breaker trips
and "opens" the circuit.
"""
def __init__(self, cb, prev_state=None, notify=False):
"""
Moves the given circuit breaker `cb` to the "closed" state.
"""
super(CircuitClosedState, self).__init__(cb, STATE_CLOSED)
self._breaker._state_storage.reset_counter()
if notify:
for listener in self._breaker.listeners:
listener.state_change(self._breaker, prev_state, self)
def on_failure(self, exc):
"""
Moves the circuit breaker to the "open" state once the failures
threshold is reached.
"""
if self._breaker._state_storage.counter >= self._breaker.fail_max:
self._breaker.open()
error_msg = 'Failures threshold reached, circuit breaker opened'
raise CircuitBreakerError(error_msg)
class CircuitOpenState(CircuitBreakerState):
"""
When the circuit is "open", calls to the circuit breaker fail immediately,
without any attempt to execute the real operation. This is indicated by the
``CircuitBreakerError`` exception.
After a suitable amount of time, the circuit breaker decides that the
operation has a chance of succeeding, so it goes into the "half-open" state.
"""
def __init__(self, cb, prev_state=None, notify=False):
"""
Moves the given circuit breaker `cb` to the "open" state.
"""
super(CircuitOpenState, self).__init__(cb, STATE_OPEN)
self._breaker._state_storage.opened_at = datetime.utcnow()
if notify:
for listener in self._breaker.listeners:
listener.state_change(self._breaker, prev_state, self)
def before_call(self, func, *args, **kwargs):
"""
After the timeout elapses, move the circuit breaker to the "half-open"
state; otherwise, raises ``CircuitBreakerError`` without any attempt
to execute the real operation.
"""
timeout = timedelta(seconds=self._breaker.reset_timeout)
opened_at = self._breaker._state_storage.opened_at
if opened_at and datetime.utcnow() < opened_at + timeout:
error_msg = 'Timeout not elapsed yet, circuit breaker still open'
raise CircuitBreakerError(error_msg)
else:
self._breaker.half_open()
return self._breaker.call(func, *args, **kwargs)
def call(self, func, *args, **kwargs):
"""
Delegate the call to before_call, if the time out is not elapsed it will throw an exception, otherwise we get
the results from the call performed after the state is switch to half-open
"""
return self.before_call(func, *args, **kwargs)
class CircuitHalfOpenState(CircuitBreakerState):
"""
In the "half-open" state, the next call to the circuit breaker is allowed
to execute the dangerous operation. Should the call succeed, the circuit
breaker resets and returns to the "closed" state. If this trial call fails,
however, the circuit breaker returns to the "open" state until another
timeout elapses.
"""
def __init__(self, cb, prev_state=None, notify=False):
"""
Moves the given circuit breaker `cb` to the "half-open" state.
"""
super(CircuitHalfOpenState, self).__init__(cb, STATE_HALF_OPEN)
if notify:
for listener in self._breaker._listeners:
listener.state_change(self._breaker, prev_state, self)
def on_failure(self, exc):
"""
Opens the circuit breaker.
"""
self._breaker.open()
raise CircuitBreakerError('Trial call failed, circuit breaker opened')
def on_success(self):
"""
Closes the circuit breaker.
"""
self._breaker.close()
class CircuitBreakerError(Exception):
"""
When calls to a service fails because the circuit is open, this error is
raised to allow the caller to handle this type of exception differently.
"""
pass