|
| 1 | +import { OriginLogger } from "@multiversx/sdk-nestjs-common"; |
| 2 | +import { ElasticQuery, ElasticService } from "@multiversx/sdk-nestjs-elastic"; |
| 3 | +import { Injectable, ServiceUnavailableException } from "@nestjs/common"; |
| 4 | +import { ApiConfigService } from "../../../api-config/api.config.service"; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class EsCircuitBreakerProxy { |
| 8 | + private failureCount = 0; |
| 9 | + private lastFailureTime = 0; |
| 10 | + private isCircuitOpen = false; |
| 11 | + private readonly logger = new OriginLogger(EsCircuitBreakerProxy.name); |
| 12 | + private readonly enabled: boolean; |
| 13 | + private readonly config: { durationThresholdMs: number, failureCountThreshold: number, resetTimeoutMs: number }; |
| 14 | + |
| 15 | + constructor( |
| 16 | + readonly apiConfigService: ApiConfigService, |
| 17 | + private readonly elasticService: ElasticService, |
| 18 | + ) { |
| 19 | + this.enabled = apiConfigService.isElasticCircuitBreakerEnabled(); |
| 20 | + this.config = apiConfigService.getElasticCircuitBreakerConfig(); |
| 21 | + this.logger.log(`ES Circuit Breaker. Enabled: ${this.enabled}. Duration threshold: ${this.config.durationThresholdMs}ms. |
| 22 | + FailureCountThreshold: ${this.config.failureCountThreshold}ms. FailureCountThreshold: ${this.config.failureCountThreshold}`); |
| 23 | + } |
| 24 | + |
| 25 | + private async withCircuitBreaker<T>(operation: () => Promise<T>): Promise<T> { |
| 26 | + if (!this.enabled) { |
| 27 | + return operation(); |
| 28 | + } |
| 29 | + |
| 30 | + if (this.isCircuitOpen) { |
| 31 | + const now = Date.now(); |
| 32 | + if (now - this.lastFailureTime >= this.config.resetTimeoutMs) { |
| 33 | + this.logger.log('Circuit is half-open, attempting reset'); |
| 34 | + this.isCircuitOpen = false; |
| 35 | + this.failureCount = 0; |
| 36 | + } else { |
| 37 | + throw new ServiceUnavailableException(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + const timeoutPromise = new Promise<never>((_, reject) => { |
| 43 | + setTimeout(() => reject(new Error('Operation timed out')), this.config.durationThresholdMs); |
| 44 | + }); |
| 45 | + |
| 46 | + const result = await Promise.race([operation(), timeoutPromise]); |
| 47 | + this.failureCount = 0; |
| 48 | + return result; |
| 49 | + } catch (error) { |
| 50 | + this.failureCount++; |
| 51 | + this.lastFailureTime = Date.now(); |
| 52 | + |
| 53 | + if (this.failureCount >= this.config.failureCountThreshold) { |
| 54 | + if (!this.isCircuitOpen) { |
| 55 | + this.logger.log('Circuit breaker opened due to multiple failures'); |
| 56 | + } |
| 57 | + |
| 58 | + this.isCircuitOpen = true; |
| 59 | + } |
| 60 | + |
| 61 | + throw new ServiceUnavailableException(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // eslint-disable-next-line require-await |
| 66 | + async getCount(index: string, query: ElasticQuery): Promise<number> { |
| 67 | + return this.withCircuitBreaker(() => this.elasticService.getCount(index, query)); |
| 68 | + } |
| 69 | + |
| 70 | + // eslint-disable-next-line require-await |
| 71 | + async getList(index: string, id: string, query: ElasticQuery): Promise<any[]> { |
| 72 | + return this.withCircuitBreaker(() => this.elasticService.getList(index, id, query)); |
| 73 | + } |
| 74 | + |
| 75 | + // eslint-disable-next-line require-await |
| 76 | + async getItem(index: string, id: string, value: string): Promise<any> { |
| 77 | + return this.withCircuitBreaker(() => this.elasticService.getItem(index, id, value)); |
| 78 | + } |
| 79 | + |
| 80 | + // eslint-disable-next-line require-await |
| 81 | + async getCustomValue(index: string, id: string, key: string): Promise<any> { |
| 82 | + return this.withCircuitBreaker(() => this.elasticService.getCustomValue(index, id, key)); |
| 83 | + } |
| 84 | + |
| 85 | + // eslint-disable-next-line require-await |
| 86 | + async setCustomValue(index: string, id: string, key: string, value: any): Promise<void> { |
| 87 | + return this.withCircuitBreaker(() => this.elasticService.setCustomValue(index, id, key, value)); |
| 88 | + } |
| 89 | + |
| 90 | + // eslint-disable-next-line require-await |
| 91 | + async setCustomValues(index: string, id: string, values: Record<string, any>): Promise<void> { |
| 92 | + return this.withCircuitBreaker(() => this.elasticService.setCustomValues(index, id, values)); |
| 93 | + } |
| 94 | + |
| 95 | + // eslint-disable-next-line require-await |
| 96 | + async getScrollableList(index: string, id: string, query: ElasticQuery, action: (items: any[]) => Promise<void>): Promise<void> { |
| 97 | + return this.withCircuitBreaker(() => this.elasticService.getScrollableList(index, id, query, action)); |
| 98 | + } |
| 99 | + |
| 100 | + // eslint-disable-next-line require-await |
| 101 | + async get(url: string): Promise<any> { |
| 102 | + return this.withCircuitBreaker(() => this.elasticService.get(url)); |
| 103 | + } |
| 104 | + |
| 105 | + // eslint-disable-next-line require-await |
| 106 | + async post(url: string, data: any): Promise<any> { |
| 107 | + return this.withCircuitBreaker(() => this.elasticService.post(url, data)); |
| 108 | + } |
| 109 | +} |
0 commit comments