@@ -598,6 +598,7 @@ export async function tryTargetsRecursively(
598
598
599
599
// start: merge inherited config with current target config (preference given to current)
600
600
const currentInheritedConfig : Record < string , any > = {
601
+ id : inheritedConfig . id || currentTarget . id ,
601
602
overrideParams : {
602
603
...inheritedConfig . overrideParams ,
603
604
...currentTarget . overrideParams ,
@@ -751,19 +752,34 @@ export async function tryTargetsRecursively(
751
752
] ;
752
753
// end: merge inherited config with current target config (preference given to current)
753
754
755
+ const isHandlingCircuitBreaker = currentInheritedConfig . id ;
756
+ if ( isHandlingCircuitBreaker ) {
757
+ const healthyTargets = ( currentTarget . targets || [ ] )
758
+ . map ( ( t : any , index : number ) => ( {
759
+ ...t ,
760
+ originalIndex : index ,
761
+ } ) )
762
+ . filter ( ( t : any ) => ! t . isOpen ) ;
763
+
764
+ if ( healthyTargets . length ) {
765
+ currentTarget . targets = healthyTargets ;
766
+ }
767
+ }
768
+
754
769
let response ;
755
770
756
771
switch ( strategyMode ) {
757
772
case StrategyModes . FALLBACK :
758
773
for ( const [ index , target ] of currentTarget . targets . entries ( ) ) {
774
+ const originalIndex = target . originalIndex || index ;
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 } .targets[${ originalIndex } ]` ,
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,15 @@ export async function tryTargetsRecursively(
873
892
currentJsonPath ,
874
893
method
875
894
) ;
895
+ if ( isHandlingCircuitBreaker ) {
896
+ await c . get ( 'handleCircuitBreakerResponse' ) ?.(
897
+ response ,
898
+ currentInheritedConfig . id ,
899
+ currentTarget . cbConfig ,
900
+ currentJsonPath ,
901
+ c
902
+ ) ;
903
+ }
876
904
} catch ( error : any ) {
877
905
// tryPost always returns a Response.
878
906
// TypeError will check for all unhandled exceptions.
@@ -898,6 +926,15 @@ export async function tryTargetsRecursively(
898
926
) ;
899
927
} else {
900
928
response = error . response ;
929
+ if ( isHandlingCircuitBreaker ) {
930
+ await c . get ( 'recordCircuitBreakerFailure' ) ?.(
931
+ env ( c ) ,
932
+ currentInheritedConfig . id ,
933
+ currentTarget . cbConfig ,
934
+ currentJsonPath ,
935
+ response . status
936
+ ) ;
937
+ }
901
938
}
902
939
}
903
940
break ;
@@ -1207,6 +1244,7 @@ export function constructConfigFromRequestHeaders(
1207
1244
'output_guardrails' ,
1208
1245
'default_input_guardrails' ,
1209
1246
'default_output_guardrails' ,
1247
+ 'cb_config' ,
1210
1248
] ) as any ;
1211
1249
}
1212
1250
0 commit comments