UNPKG

microservice-support-toolkit

Version:

A global module with commons utilities for microservice support

88 lines (81 loc) 4.49 kB
const { hystrixSSEStream: HystrixStream, commandFactory: CommandFactory } = require('hystrixjs'); const logger = require('./logger'); let defaults = { "hystrix.execution.timeoutInMilliseconds": 30000, "hystrix.force.circuit.open": false, "hystrix.force.circuit.closed": false, "hystrix.circuit.sleepWindowInMilliseconds": 3000, "hystrix.circuit.errorThresholdPercentage": 50, "hystrix.circuit.volumeThreshold": 10, "hystrix.metrics.statistical.window.timeInMilliseconds": 10000, "hystrix.metrics.statistical.window.bucketsNumber": 10, "hystrix.metrics.percentile.window.timeInMilliseconds": 10000, "hystrix.metrics.percentile.window.bucketsNumber": 10, "hystrix.request.volume.rejectionThreshold": 0, }; const stream = (request, response) => { logger.info('Starting hystrix stream...'); response.append('Content-Type', 'text/event-stream;charset=UTF-8'); response.append('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate'); response.append('Pragma', 'no-cache'); return HystrixStream.toObservable().subscribe( sseData => response.write('data: ' + sseData + '\n\n'), err => console.error(err), () => response.end() ); }; const commandBuilder = (makeRequest, fallbackTo, errorHandler, serviceName, commandKey, configs = {}) => { const TIMEOUT = configs.timeout || defaults["hystrix.execution.timeoutInMilliseconds"]; const CIRCUIT_VOLUME_THRESHOLD = configs.circuitVolumeThreshold || defaults["hystrix.circuit.volumeThreshold"]; const CIRCUIT_SLEEP_WINDOW_IN_MS = configs.circuitSleepWindowInMilliseconds || defaults["hystrix.circuit.sleepWindowInMilliseconds"]; const REQUEST_VOLUME_REJECTION_THRESHOLD = configs.requestVolumeRejectionThreshold || defaults["hystrix.request.volume.rejectionThreshold"]; const FORCE_CIRCUIT_OPEN = configs.forceCircuitOpen || defaults["hystrix.force.circuit.open"]; const FORCE_CIRCUIT_CLOSED = configs.forceCircuitClosed || defaults["hystrix.force.circuit.closed"]; const STATISTICAL_WINDOW_NUMBER_OF_BUCKETS = configs.statisticalWindowNumberOfBuckets || defaults["hystrix.metrics.statistical.window.bucketsNumber"]; const STATISTICAL_WINDOW_LENGTH = configs.statisticalWindowLength || defaults["hystrix.metrics.statistical.window.timeInMilliseconds"]; const PERCENTILE_WINDOW_NUMBER_OF_BUCKETS = configs.percentileWindowNumberOfBuckets || defaults["hystrix.metrics.percentile.window.bucketsNumber"]; const PERCENTILE_WINDOW_LENGTH = configs.percentileWindowLength || defaults["hystrix.metrics.percentile.window.timeInMilliseconds"]; const CIRCUIT_ERROR_THRESHOLD_PERCENTAGE = configs.errorThresholdPercentage || defaults["hystrix.circuit.errorThresholdPercentage"]; CommandFactory.resetCache(); if (errorHandler) { return CommandFactory.getOrCreate(commandKey) .timeout(TIMEOUT) .circuitBreakerSleepWindowInMilliseconds(CIRCUIT_SLEEP_WINDOW_IN_MS) .circuitBreakerRequestVolumeThreshold(CIRCUIT_VOLUME_THRESHOLD) .requestVolumeRejectionThreshold(REQUEST_VOLUME_REJECTION_THRESHOLD) .circuitBreakerForceOpened(FORCE_CIRCUIT_OPEN) .circuitBreakerForceClosed(FORCE_CIRCUIT_CLOSED) .statisticalWindowNumberOfBuckets(STATISTICAL_WINDOW_NUMBER_OF_BUCKETS) .statisticalWindowLength(STATISTICAL_WINDOW_LENGTH) .percentileWindowNumberOfBuckets(PERCENTILE_WINDOW_NUMBER_OF_BUCKETS) .percentileWindowLength(PERCENTILE_WINDOW_LENGTH) .circuitBreakerErrorThresholdPercentage(CIRCUIT_ERROR_THRESHOLD_PERCENTAGE) .run(makeRequest) .fallbackTo(fallbackTo) .errorHandler(errorHandler) .build(); } else { return CommandFactory.getOrCreate(commandKey) .timeout(TIMEOUT) .circuitBreakerSleepWindowInMilliseconds(CIRCUIT_SLEEP_WINDOW_IN_MS) .circuitBreakerRequestVolumeThreshold(CIRCUIT_VOLUME_THRESHOLD) .requestVolumeRejectionThreshold(REQUEST_VOLUME_REJECTION_THRESHOLD) .circuitBreakerForceOpened(FORCE_CIRCUIT_OPEN) .circuitBreakerForceClosed(FORCE_CIRCUIT_CLOSED) .statisticalWindowNumberOfBuckets(STATISTICAL_WINDOW_NUMBER_OF_BUCKETS) .statisticalWindowLength(STATISTICAL_WINDOW_LENGTH) .percentileWindowNumberOfBuckets(PERCENTILE_WINDOW_NUMBER_OF_BUCKETS) .percentileWindowLength(PERCENTILE_WINDOW_LENGTH) .circuitBreakerErrorThresholdPercentage(CIRCUIT_ERROR_THRESHOLD_PERCENTAGE) .run(makeRequest) .fallbackTo(fallbackTo) .build(); } }; module.exports = { commandBuilder, stream, };