-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·48 lines (40 loc) · 1.4 KB
/
app.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
import os
import socket
from flask import Flask
from redis import Redis
from pybreaker import CircuitBreaker, CircuitBreakerError
db = Redis(host="redis", db=0, socket_timeout=2, protocol=3)
db_breaker = CircuitBreaker(fail_max=1, reset_timeout=5)
app = Flask(__name__)
# If the dabatase is not available, then the circuit will become open.
# Subsequent calls will immediatelly fail instead of being redirected
# toward a database. The circuit will periodically try to close according
# to the configuration parameters (see above). A close state means
# that the annotated function again executes without problems.
@db_breaker
def num_visits():
return db.incr('visits')
@app.route("/")
def hello():
try:
visits = num_visits()
except CircuitBreakerError:
visits = "<em>The counter is temporarily disabled!</em>"
response = \
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Fault Tolerance Demo</title>
</head>
<body>
<h1>Hello {name}!</h1>
<strong>Hostname:</strong> {hostname}<br/>
<strong>Visits:</strong> {visits}
</body>
</html>
"""
return response.format(name=os.getenv("NAME"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)