(request, info)
| 765 | if (b.state === 'OPEN') { |
| 766 | if (now - b.lastFailureTime >= CIRCUIT_BREAKER.TIMEOUT) { |
| 767 | b.state = 'HALF_OPEN'; |
| 768 | b.halfOpenAttempts = 0; |
| 769 | return 'HALF_OPEN'; |
| 770 | } |
| 771 | return 'OPEN'; |
| 772 | } |
| 773 | |
| 774 | if (b.state === 'HALF_OPEN') { |
| 775 | if (b.halfOpenAttempts >= CIRCUIT_BREAKER.HALF_OPEN_MAX_CALLS) return 'OPEN'; |
| 776 | b.halfOpenAttempts++; |
| 777 | } |
| 778 | |
| 779 | return b.state; |
| 780 | } |
| 781 | |
| 782 | function updateCircuitBreaker(clientIP, success) { |
| 783 | let b = circuitBreakers.get(clientIP); |
| 784 | if (!b) { |
| 785 | if (circuitBreakers.size >= CACHE_MAX_SIZE) { |
| 786 | circuitBreakers.delete(circuitBreakers.keys().next().value); |
| 787 | } |
| 788 | b = { state: 'CLOSED', failureCount: 0, lastFailureTime: 0, halfOpenAttempts: 0 }; |
| 789 | circuitBreakers.set(clientIP, b); |
| 790 | } |
| 791 | |
| 792 | if (success) { |
| 793 | if (b.state === 'HALF_OPEN') { b.state = 'CLOSED'; b.failureCount = 0; } |
| 794 | else if (b.state === 'CLOSED') b.failureCount = Math.max(0, b.failureCount - 1); |
| 795 | } else { |
| 796 | b.failureCount++; |
| 797 | b.lastFailureTime = Date.now(); |
no test coverage detected