1
1
const gateway = require ( '../index' )
2
- const PORT = process . env . PORT || 8080
3
2
const onEnd = require ( 'on-http-end' )
4
3
const CircuitBreaker = require ( 'opossum' )
5
4
6
- const REQUEST_TIMEOUT = 1.5 * 1000
7
-
8
5
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
10
7
errorThresholdPercentage : 50 , // When 50% of requests fail, trip the circuit
11
8
resetTimeout : 30 * 1000 // After 30 seconds, try again.
12
9
}
@@ -23,14 +20,13 @@ const breaker = new CircuitBreaker(([req, res, url, proxy, proxyOpts]) => {
23
20
breaker . fallback ( ( [ req , res ] , err ) => {
24
21
if ( err . code === 'EOPENBREAKER' ) {
25
22
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!'
27
24
} , 503 )
28
25
}
29
26
} )
30
27
31
28
gateway ( {
32
29
routes : [ {
33
- timeout : REQUEST_TIMEOUT ,
34
30
proxyHandler : ( ...params ) => breaker . fire ( params ) ,
35
31
prefix : '/public' ,
36
32
target : 'http://localhost:3000' ,
@@ -40,20 +36,10 @@ gateway({
40
36
type : 'swagger'
41
37
}
42
38
} ]
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!' ) )
46
40
47
41
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