@@ -744,12 +744,17 @@ export class ConatServer extends EventEmitter {
744
744
}
745
745
// send to exactly one in each queue group
746
746
for ( const queue in g ) {
747
- const target = this . loadBalance ( {
748
- pattern,
749
- subject,
750
- queue,
751
- targets : g [ queue ] ,
752
- } ) ;
747
+ let target ;
748
+ const targets = g [ queue ] ;
749
+ if ( queue == STICKY_QUEUE_GROUP ) {
750
+ target = await this . stickyLoadBalance ( {
751
+ pattern,
752
+ subject,
753
+ targets,
754
+ } ) ;
755
+ } else {
756
+ target = this . loadBalance ( targets ) ;
757
+ }
753
758
if ( target !== undefined ) {
754
759
this . io . to ( target ) . emit ( pattern , { subject, data } ) ;
755
760
if ( ! isSilentPattern ( pattern ) ) {
@@ -773,12 +778,17 @@ export class ConatServer extends EventEmitter {
773
778
for ( const pattern in clusterInterest ) {
774
779
const g = clusterInterest [ pattern ] ;
775
780
for ( const queue in g ) {
776
- const t = this . clusterLoadBalance ( {
777
- pattern,
778
- subject,
779
- queue,
780
- targets : g [ queue ] ,
781
- } ) ;
781
+ let t ;
782
+ const targets = g [ queue ] ;
783
+ if ( queue == STICKY_QUEUE_GROUP ) {
784
+ t = await this . stickyClusterLoadBalance ( {
785
+ pattern,
786
+ subject,
787
+ targets,
788
+ } ) ;
789
+ } else {
790
+ t = this . clusterLoadBalance ( targets ) ;
791
+ }
782
792
if ( t !== undefined ) {
783
793
const { id, target } = t ;
784
794
if ( id == this . id ) {
@@ -824,78 +834,74 @@ export class ConatServer extends EventEmitter {
824
834
link ?. client . conn . emit ( "publish" , data1 ) ;
825
835
}
826
836
827
- //
828
- // TODO: Supercluster routing. NOT IMPLEMENTED YET
829
- //
830
- // // if no matches in local cluster, try the supercluster (if there is one)
831
- // if (count == 0) {
832
- // // nothing in this cluster, so try other clusters
833
- // for (const clusterName in this.clusterLinks) {
834
- // if (clusterName == this.clusterName) continue;
835
- // const links = this.clusterLinks[clusterName];
836
- // for (const id in links) {
837
- // const link = links[id];
838
- // const count2 = link.publish({ subject, data, queueGroups });
839
- // if (count2 > 0) {
840
- // count += count2;
841
- // // once we publish to any other cluster, we are done.
842
- // break;
843
- // }
844
- // }
845
- // }
846
- // }
847
-
848
837
return count ;
849
838
} ;
850
839
851
840
///////////////////////////////////////
852
841
// WHO GETS PUBLISHED MESSAGE:
853
842
///////////////////////////////////////
854
- private loadBalance = ( {
843
+ private loadBalance = ( targets : Set < string > ) : string | undefined => {
844
+ if ( targets . size == 0 ) {
845
+ return undefined ;
846
+ }
847
+ return randomChoice ( targets ) ;
848
+ } ;
849
+
850
+ clusterLoadBalance = ( targets0 : {
851
+ [ id : string ] : Set < string > ;
852
+ } ) : { id : string ; target : string } | undefined => {
853
+ const targets = new Set < string > ( ) ;
854
+ for ( const id in targets0 ) {
855
+ for ( const target of targets0 [ id ] ) {
856
+ targets . add ( JSON . stringify ( { id, target } ) ) ;
857
+ }
858
+ }
859
+ const x = this . loadBalance ( targets ) ;
860
+ if ( ! x ) {
861
+ return undefined ;
862
+ }
863
+ return JSON . parse ( x ) ;
864
+ } ;
865
+
866
+ // sticky versions
867
+
868
+ private stickyLoadBalance = async ( {
855
869
pattern,
856
870
subject,
857
- queue,
858
871
targets,
859
872
} : {
860
873
pattern : string ;
861
874
subject : string ;
862
- queue : string ;
863
875
targets : Set < string > ;
864
- } ) : string | undefined => {
876
+ } ) : Promise < string | undefined > => {
865
877
if ( targets . size == 0 ) {
866
878
return undefined ;
867
879
}
868
- if ( queue == STICKY_QUEUE_GROUP ) {
869
- return stickyChoice ( {
870
- pattern,
871
- subject,
872
- targets,
873
- updateSticky : this . updateSticky ,
874
- getStickyTarget : this . getStickyTarget ,
875
- } ) ;
876
- } else {
877
- return randomChoice ( targets ) ;
878
- }
880
+ return stickyChoice ( {
881
+ pattern,
882
+ subject,
883
+ targets,
884
+ updateSticky : this . updateSticky ,
885
+ getStickyTarget : this . getStickyTarget ,
886
+ } ) ;
879
887
} ;
880
888
881
- clusterLoadBalance = ( {
889
+ stickyClusterLoadBalance = async ( {
882
890
pattern,
883
891
subject,
884
- queue,
885
892
targets : targets0 ,
886
893
} : {
887
894
pattern : string ;
888
895
subject : string ;
889
- queue : string ;
890
896
targets : { [ id : string ] : Set < string > } ;
891
- } ) : { id : string ; target : string } | undefined => {
897
+ } ) : Promise < { id : string ; target : string } | undefined > => {
892
898
const targets = new Set < string > ( ) ;
893
899
for ( const id in targets0 ) {
894
900
for ( const target of targets0 [ id ] ) {
895
901
targets . add ( JSON . stringify ( { id, target } ) ) ;
896
902
}
897
903
}
898
- const x = this . loadBalance ( { pattern, subject, queue , targets } ) ;
904
+ const x = await this . stickyLoadBalance ( { pattern, subject, targets } ) ;
899
905
if ( ! x ) {
900
906
return undefined ;
901
907
}
0 commit comments