@@ -751,19 +751,35 @@ export async function tryTargetsRecursively(
751
751
] ;
752
752
// end: merge inherited config with current target config (preference given to current)
753
753
754
+ const circuitBreakerConfigId = c . get ( 'circuitBreakerConfigId' ) ;
755
+ if ( circuitBreakerConfigId ) {
756
+ const healthyTargets = ( currentTarget . targets || [ ] )
757
+ . map ( ( t : any , index : number ) => ( {
758
+ ...t ,
759
+ originalIndex : index ,
760
+ } ) )
761
+ . filter ( ( t : any ) => ! t . isClosed ) ;
762
+
763
+ if ( healthyTargets . length ) {
764
+ currentTarget . targets = healthyTargets ;
765
+ }
766
+ }
767
+
754
768
let response ;
755
769
756
770
switch ( strategyMode ) {
757
771
case StrategyModes . FALLBACK :
758
772
for ( const [ index , target ] of currentTarget . targets . entries ( ) ) {
773
+ const originalIndex = target . originalIndex || index ;
774
+ currentJsonPath = `${ currentJsonPath } .targets[${ originalIndex } ]` ;
759
775
response = await tryTargetsRecursively (
760
776
c ,
761
777
target ,
762
778
request ,
763
779
requestHeaders ,
764
780
fn ,
765
781
method ,
766
- ` ${ currentJsonPath } .targets[ ${ index } ]` ,
782
+ currentJsonPath ,
767
783
currentInheritedConfig
768
784
) ;
769
785
if ( response ?. headers . get ( 'x-portkey-gateway-exception' ) === 'true' ) {
@@ -791,8 +807,9 @@ export async function tryTargetsRecursively(
791
807
792
808
let randomWeight = Math . random ( ) * totalWeight ;
793
809
for ( const [ index , provider ] of currentTarget . targets . entries ( ) ) {
810
+ const originalIndex = provider . originalIndex || index ;
794
811
if ( randomWeight < provider . weight ) {
795
- currentJsonPath = currentJsonPath + `.targets[${ index } ]` ;
812
+ currentJsonPath = currentJsonPath + `.targets[${ originalIndex } ]` ;
796
813
response = await tryTargetsRecursively (
797
814
c ,
798
815
provider ,
@@ -836,28 +853,30 @@ export async function tryTargetsRecursively(
836
853
throw new RouterError ( conditionalRouter . message ) ;
837
854
}
838
855
856
+ const originalIndex = finalTarget . originalIndex || finalTarget . index ;
839
857
response = await tryTargetsRecursively (
840
858
c ,
841
859
finalTarget ,
842
860
request ,
843
861
requestHeaders ,
844
862
fn ,
845
863
method ,
846
- `${ currentJsonPath } .targets[${ finalTarget . index } ]` ,
864
+ `${ currentJsonPath } .targets[${ originalIndex } ]` ,
847
865
currentInheritedConfig
848
866
) ;
849
867
break ;
850
868
}
851
869
852
870
case StrategyModes . SINGLE :
871
+ const originalIndex = currentTarget . targets [ 0 ] . originalIndex || 0 ;
853
872
response = await tryTargetsRecursively (
854
873
c ,
855
874
currentTarget . targets [ 0 ] ,
856
875
request ,
857
876
requestHeaders ,
858
877
fn ,
859
878
method ,
860
- `${ currentJsonPath } .targets[0 ]` ,
879
+ `${ currentJsonPath } .targets[${ originalIndex } ]` ,
861
880
currentInheritedConfig
862
881
) ;
863
882
break ;
@@ -873,6 +892,14 @@ export async function tryTargetsRecursively(
873
892
currentJsonPath ,
874
893
method
875
894
) ;
895
+ if ( circuitBreakerConfigId ) {
896
+ await c . get ( 'handleCircuitBreakerResponse' ) (
897
+ response ,
898
+ circuitBreakerConfigId ,
899
+ currentJsonPath ,
900
+ c
901
+ ) ;
902
+ }
876
903
} catch ( error : any ) {
877
904
// tryPost always returns a Response.
878
905
// TypeError will check for all unhandled exceptions.
@@ -898,6 +925,13 @@ export async function tryTargetsRecursively(
898
925
) ;
899
926
} else {
900
927
response = error . response ;
928
+ if ( circuitBreakerConfigId ) {
929
+ await c . get ( 'recordCircuitBreakerFailure' ) (
930
+ env ( c ) ,
931
+ circuitBreakerConfigId ,
932
+ currentJsonPath
933
+ ) ;
934
+ }
901
935
}
902
936
}
903
937
break ;
0 commit comments