@@ -17,13 +17,14 @@ var logger = logging.GetLogger()
1717
1818type Dispatcher struct {
1919 concurrent.MetaConcurrentStructure [Dispatcher ]
20- ctx context.Context
21- ctxCancel context.CancelFunc
22- forwarders * structure.IndexMap [* arch.ForwarderWithValues ]
23- totalWeights map [int ]int // priority -> totalWeight
24- currentIndex int
25- senderPacket * channel.SafeSender [packet.IPacket ]
26- connsMap * concurrent.ConcurrentMap [string , string ]
20+ ctx context.Context
21+ ctxCancel context.CancelFunc
22+ forwarders * structure.IndexMap [* arch.ForwarderWithValues ]
23+ totalWeights map [uint32 ]uint32 // priority -> totalWeight
24+ currentIndex int
25+ senderPacket * channel.SafeSender [packet.IPacket ]
26+ connsMap * concurrent.ConcurrentMap [string , string ] // conn -> forwarder
27+ connsMapBackward * concurrent.ConcurrentMap [string , * structure.HashSet [string ]] // forwarder -> conns
2728}
2829
2930func NewDispatcher (parentCtx context.Context ) * Dispatcher {
@@ -32,11 +33,12 @@ func NewDispatcher(parentCtx context.Context) *Dispatcher {
3233 ctx : ctx ,
3334 ctxCancel : cancel ,
3435 forwarders : structure .NewIndexMap [* arch.ForwarderWithValues ](),
35- totalWeights : make (map [int ] int ),
36+ totalWeights : make (map [uint32 ] uint32 ),
3637 currentIndex : 0 ,
3738 MetaConcurrentStructure : * concurrent .NewMetaSyncStructure [Dispatcher ](),
3839 senderPacket : channel .NewSafeSenderWithParentCtxAndSize [packet.IPacket ](ctx , 16 ),
3940 connsMap : concurrent .NewSyncMap [string , string ](),
41+ connsMapBackward : concurrent .NewSyncMap [string , * structure.HashSet [string ]](),
4042 }
4143
4244 return dispatcher
@@ -57,23 +59,12 @@ func (dispatcher *Dispatcher) Close() error {
5759
5860// ---------------------------------------------------------------------
5961
60- func (dispatcher * Dispatcher ) packetHandlerMiddleware (forwarderUuid string , f func (packet.IPacket ) bool ) func (packet.IPacket ) bool {
61- return func (pkt packet.IPacket ) bool {
62- switch pkt := pkt .(type ) {
63- case * packet.PacketProxyData :
64- dispatcher .connsMap .LoadOrStore (pkt .Uuid , forwarderUuid )
65- }
66-
67- return f (pkt )
68- }
69- }
70-
7162func (dispatcher * Dispatcher ) AddForwarder (fwv * arch.ForwarderWithValues ) (uuid string ) {
7263 dispatcher .Lock .Lock ()
7364 defer dispatcher .Lock .Unlock ()
7465
7566 uuid = dispatcher .forwarders .Store (fwv )
76- dispatcher .totalWeights [fwv .Priority ] += fwv .Weight
67+ dispatcher .totalWeights [fwv .InitPacket . Priority ] += fwv . InitPacket .Weight
7768
7869 go channel .ConsumeWithCtx (dispatcher .GetCtx (), fwv .GetChanSendPacket (), dispatcher .senderPacket .Push )
7970
@@ -88,12 +79,14 @@ func (dispatcher *Dispatcher) RemoveForwarder(uuid string) {
8879 if ! ok {
8980 return
9081 }
91- dispatcher .totalWeights [conn .Priority ] -= conn .Weight
92- if dispatcher .totalWeights [conn .Priority ] == 0 {
93- delete (dispatcher .totalWeights , conn .Priority )
82+ dispatcher .totalWeights [conn .InitPacket . Priority ] -= conn . InitPacket .Weight
83+ if dispatcher .totalWeights [conn .InitPacket . Priority ] == 0 {
84+ delete (dispatcher .totalWeights , conn .InitPacket . Priority )
9485 }
9586 dispatcher .forwarders .Delete (uuid )
96- dispatcher .connsMap .Delete (uuid )
87+ if conns , ok := dispatcher .connsMapBackward .LoadAndDelete (uuid ); ok {
88+ conns .Stream ().ForEach (func (t string ) { dispatcher .connsMap .Delete (t ) })
89+ }
9790}
9891
9992func (dispatcher * Dispatcher ) Next () (uuid string , forwarder arch.IForwarder , ok bool ) {
@@ -104,21 +97,21 @@ func (dispatcher *Dispatcher) Next() (uuid string, forwarder arch.IForwarder, ok
10497 return
10598 }
10699
107- totalWeight , _ok := hof .NewStreamWithMap (dispatcher .totalWeights ).Max (func (bigger container.Entry [int , int ], smaller container.Entry [int , int ]) bool {
100+ totalWeight , _ok := hof .NewStreamWithMap (dispatcher .totalWeights ).Max (func (bigger container.Entry [uint32 , uint32 ], smaller container.Entry [uint32 , uint32 ]) bool {
108101 return bigger .GetKey () > smaller .GetKey ()
109102 })
110103
111104 if ! _ok || totalWeight .GetValue () == 0 {
112105 return
113106 }
114107
115- dispatcher .currentIndex = (dispatcher .currentIndex + 1 ) % totalWeight .GetValue ()
108+ dispatcher .currentIndex = (dispatcher .currentIndex + 1 ) % int ( totalWeight .GetValue () )
116109 i := dispatcher .currentIndex
117110
118111 dispatcher .forwarders .Stream ().Filter (func (t container.Entry [string , * arch.ForwarderWithValues ]) bool {
119- return t .GetValue ().Priority == totalWeight .GetKey ()
112+ return t .GetValue ().InitPacket . Priority == totalWeight .GetKey ()
120113 }).Range (func (t container.Entry [string , * arch.ForwarderWithValues ]) bool {
121- i -= t .GetValue ().Weight
114+ i -= int ( t .GetValue ().InitPacket . Weight )
122115 if i < 0 {
123116 uuid , forwarder , ok = t .GetKey (), t .GetValue (), true
124117 return false
@@ -152,16 +145,18 @@ func (dispatcher *Dispatcher) HandlePacket(pkt packet.IPacket) bool {
152145 switch pkt := pkt .(type ) {
153146 case packet.IPacketForConn :
154147 if uuid , ok := dispatcher .connsMap .Load (pkt .GetUuid ()); ok {
155- if v , ok := dispatcher .forwarders .Load (uuid ); ok {
156- v .HandlePacket (pkt )
157- return true
148+ if forwarder , ok := dispatcher .forwarders .Load (uuid ); ok {
149+ forwarder .HandlePacket (pkt )
150+ }
151+ } else {
152+ if uuid , forwarder , ok := dispatcher .Next (); ok {
153+ actual , _ := dispatcher .connsMap .LoadOrStore (pkt .GetUuid (), uuid )
154+ actual2 , _ := dispatcher .connsMapBackward .LoadOrStore (actual , structure .NewHashSet [string ]())
155+ actual2 .Store (pkt .GetUuid ())
156+ forwarder .HandlePacket (pkt )
158157 }
159158 }
160159 }
161160
162- if uuid , forwarder , ok := dispatcher .Next (); ok {
163- dispatcher .packetHandlerMiddleware (uuid , forwarder .HandlePacket )(pkt )
164- }
165-
166161 return true
167162}
0 commit comments