Skip to content

Commit adc159b

Browse files
committed
Separate out into files
1 parent 6c8fccf commit adc159b

File tree

5 files changed

+105
-96
lines changed

5 files changed

+105
-96
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

bucket.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Bucket:
2+
def __init__(self, ts, count):
3+
self.ts = ts
4+
self.count = count

circuit_breaker.py

+2-96
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,8 @@
1-
import os
2-
import random
31
import requests
42
import time
53

6-
7-
def main():
8-
breaker = CircuitBreaker(threshold=5, window=5)
9-
bad_good_ratio = 0.2 # Percentage of 500s (vs 200s)
10-
11-
while True:
12-
schmutz = random.random()
13-
if schmutz <= bad_good_ratio:
14-
status_code = 500
15-
else:
16-
status_code = 200
17-
18-
try:
19-
breaker.get('http://httpbin.org/status/' + str(status_code))
20-
is_circuit_closed = True
21-
except CircuitOpenException:
22-
is_circuit_closed = False
23-
24-
print_buckets(breaker, is_circuit_closed)
25-
26-
27-
def print_buckets(breaker, is_circuit_closed):
28-
"""
29-
Prints bucket information.
30-
31-
Example output:
32-
33-
current time: Thu Jan 5 16:25:19 2017
34-
current window: 1483629914 - 1483629919
35-
circuit state: closed
36-
37-
BUCKET_TS ERROR_COUNT WITHIN_WINDOW
38-
0 0 no
39-
1483629916 1 yes
40-
0 0 no
41-
1483629918 1 yes
42-
1483629919 1 yes
43-
"""
44-
os.system('clear')
45-
46-
# Assign preamble and header values
47-
current_time = int(time.time())
48-
window_lower_bound = current_time - breaker.error_window
49-
headers = ['BUCKET_TS', 'ERROR_COUNT', 'WITHIN_WINDOW']
50-
51-
# Print the preamble and table headers
52-
def circuit_state_label():
53-
if is_circuit_closed is True:
54-
return '\033[92mclosed\033[39m' # Green
55-
else:
56-
return '\033[91mopen\033[39m' # Red
57-
58-
print 'current time: %s' % time.ctime(current_time)
59-
print 'current window: %s - %s' % (window_lower_bound, current_time)
60-
print 'circuit state: %s' % circuit_state_label()
61-
print
62-
print '{:<15} {:<15} {:<15}'.format(*headers)
63-
64-
def relative_time_label(bucket):
65-
if bucket.ts == 0 or current_time - bucket.ts == 0:
66-
return '0'
67-
else:
68-
return '-%d' % (current_time - bucket.ts)
69-
70-
def within_window_prefix(within_window):
71-
if within_window is True:
72-
return '\033[33m'
73-
else:
74-
return '\033[39m'
75-
76-
# Print the table rows
77-
for bucket in breaker.buckets:
78-
relative_time = relative_time_label(bucket)
79-
within_window = current_time - bucket.ts <= breaker.error_window
80-
within_window_label = 'yes' if within_window is True else 'no'
81-
format_args = [within_window_prefix(within_window), relative_time,
82-
bucket.count, within_window_label]
83-
print '{!s}{:<15} {:<15} {:<15}\033[39m'.format(*format_args)
84-
85-
print
86-
87-
88-
class CircuitOpenException(Exception):
89-
pass
90-
91-
92-
class Bucket:
93-
def __init__(self, ts, count):
94-
self.ts = ts
95-
self.count = count
4+
from bucket import Bucket
5+
from circuit_errors import CircuitOpenException
966

977

988
class CircuitBreaker:
@@ -155,7 +65,3 @@ def is_circuit_open(self):
15565
Returns True if circuit is open. Otherwise, returns False.
15666
"""
15767
return self.count_errors_in_window() >= self.error_threshold
158-
159-
160-
if __name__ == '__main__':
161-
main()

circuit_errors.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class CircuitOpenException(Exception):
2+
pass

program.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
import random
3+
import time
4+
5+
from circuit_breaker import CircuitBreaker, CircuitOpenException
6+
7+
8+
CONSOLE_GREEN = '\033[92m'
9+
CONSOLE_RED = '\033[91m'
10+
CONSOLE_WHITE = '\033[39m'
11+
HTTP_BIN_URL = 'http://httpbin.org/status/'
12+
13+
14+
def main():
15+
breaker = CircuitBreaker(threshold=5, window=5)
16+
bad_good_ratio = 0.2 # Percentage of 500s (vs 200s)
17+
18+
while True:
19+
schmutz = random.random()
20+
if schmutz <= bad_good_ratio:
21+
status_code = 500
22+
else:
23+
status_code = 200
24+
25+
try:
26+
breaker.get(HTTP_BIN_URL + str(status_code))
27+
is_circuit_closed = True
28+
except CircuitOpenException:
29+
is_circuit_closed = False
30+
31+
print_buckets(breaker, is_circuit_closed)
32+
33+
34+
def print_buckets(breaker, is_circuit_closed):
35+
"""
36+
Prints bucket information.
37+
38+
Example output:
39+
40+
current time: Thu Jan 5 16:25:19 2017
41+
current window: 1483629914 - 1483629919
42+
circuit state: closed
43+
44+
BUCKET_TS ERROR_COUNT WITHIN_WINDOW
45+
0 0 no
46+
1483629916 1 yes
47+
0 0 no
48+
1483629918 1 yes
49+
1483629919 1 yes
50+
"""
51+
os.system('clear')
52+
53+
# Assign preamble and header values
54+
current_time = int(time.time())
55+
window_lower_bound = current_time - breaker.error_window
56+
headers = ['BUCKET_TS', 'ERROR_COUNT', 'WITHIN_WINDOW']
57+
58+
# Print the preamble and table headers
59+
def circuit_state_label():
60+
if is_circuit_closed is True:
61+
return CONSOLE_GREEN + 'closed' + CONSOLE_WHITE
62+
else:
63+
return CONSOLE_RED + 'open' + CONSOLE_WHITE
64+
65+
print 'current time: %s' % time.ctime(current_time)
66+
print 'current window: %s - %s' % (window_lower_bound, current_time)
67+
print 'circuit state: %s' % circuit_state_label()
68+
print
69+
print '{:<15} {:<15} {:<15}'.format(*headers)
70+
71+
def relative_time_label(bucket):
72+
if bucket.ts == 0 or current_time - bucket.ts == 0:
73+
return '0'
74+
else:
75+
return '-%d' % (current_time - bucket.ts)
76+
77+
def within_window_prefix(within_window):
78+
if within_window is True:
79+
return '\033[33m'
80+
else:
81+
return '\033[39m'
82+
83+
# Print the table rows
84+
for bucket in breaker.buckets:
85+
relative_time = relative_time_label(bucket)
86+
within_window = current_time - bucket.ts <= breaker.error_window
87+
within_window_label = 'yes' if within_window is True else 'no'
88+
format_args = [within_window_prefix(within_window), relative_time,
89+
bucket.count, within_window_label]
90+
print '{!s}{:<15} {:<15} {:<15}\033[39m'.format(*format_args)
91+
92+
print
93+
94+
95+
if __name__ == '__main__':
96+
main()

0 commit comments

Comments
 (0)