Skip to content

Commit bb91cc9

Browse files
committed
source code refactoring
1 parent 0734b64 commit bb91cc9

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

demos/circuitbreaker.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
const gateway = require('../index')
2-
const PORT = process.env.PORT || 8080
32
const onEnd = require('on-http-end')
43
const CircuitBreaker = require('opossum')
54

6-
const REQUEST_TIMEOUT = 1.5 * 1000
7-
85
const options = {
9-
timeout: REQUEST_TIMEOUT - 200, // If our function takes longer than "timeout", trigger a failure
6+
timeout: 1500, // If our function takes longer than "timeout", trigger a failure
107
errorThresholdPercentage: 50, // When 50% of requests fail, trip the circuit
118
resetTimeout: 30 * 1000 // After 30 seconds, try again.
129
}
@@ -23,14 +20,13 @@ const breaker = new CircuitBreaker(([req, res, url, proxy, proxyOpts]) => {
2320
breaker.fallback(([req, res], err) => {
2421
if (err.code === 'EOPENBREAKER') {
2522
res.send({
26-
message: 'Upps, looks like "public" service is down. Please try again in 30 seconds!'
23+
message: 'Upps, looks like we are under heavy load. Please try again in 30 seconds!'
2724
}, 503)
2825
}
2926
})
3027

3128
gateway({
3229
routes: [{
33-
timeout: REQUEST_TIMEOUT,
3430
proxyHandler: (...params) => breaker.fire(params),
3531
prefix: '/public',
3632
target: 'http://localhost:3000',
@@ -40,20 +36,10 @@ gateway({
4036
type: 'swagger'
4137
}
4238
}]
43-
}).start(PORT).then(() => {
44-
console.log(`API Gateway listening on ${PORT} port!`)
45-
})
39+
}).start(8080).then(() => console.log('API Gateway listening on 8080 port!'))
4640

4741
const service = require('restana')({})
48-
service.get('/longop', (req, res) => {
49-
setTimeout(() => {
50-
res.send('This operation will trigger the breaker failure counter...')
51-
}, 2000)
52-
})
53-
service.get('/hi', (req, res) => {
54-
res.send('Hello World!')
55-
})
56-
57-
service.start(3000).then(() => {
58-
console.log('Public service listening on 3000 port!')
59-
})
42+
service
43+
.get('/longop', (req, res) => setTimeout(() => res.send('This operation will trigger the breaker failure counter...'), 2000))
44+
.get('/hi', (req, res) => res.send('Hello World!'))
45+
.start(3000).then(() => console.log('Public service listening on 3000 port!'))

0 commit comments

Comments
 (0)