@@ -347,23 +347,42 @@ find_best_routes([First | Rest]) ->
347347select_better_route ({LeftScore , _ } = Left , {RightScore , _ } = Right ) ->
348348 LeftPin = LeftScore # domain_PaymentRouteScores .route_pin ,
349349 RightPin = RightScore # domain_PaymentRouteScores .route_pin ,
350- case {LeftPin , RightPin } of
351- _ when LeftPin /= ? ZERO , RightPin /= ? ZERO ->
352- select_better_pinned_route (Left , Right );
353- _ ->
354- select_better_regular_route (Left , Right )
355- end .
350+ Res =
351+ case {LeftPin , RightPin } of
352+ _ when LeftPin /= ? ZERO , RightPin /= ? ZERO , LeftPin == RightPin ->
353+ select_better_pinned_route (Left , Right );
354+ _ ->
355+ select_better_regular_route (Left , Right )
356+ end ,
357+ Res .
356358
357- select_better_pinned_route ({LeftScore , _Route1 } = Left , {RightScore , _Route2 } = Right ) ->
358- case max (LeftScore , RightScore ) of
359- LeftScore ->
359+ select_better_pinned_route ({LeftScore0 , _Route1 } = Left , {RightScore0 , _Route2 } = Right ) ->
360+ LeftScore1 = LeftScore0 # domain_PaymentRouteScores {
361+ random_condition = 0
362+ },
363+ RightScore1 = RightScore0 # domain_PaymentRouteScores {
364+ random_condition = 0
365+ },
366+ case max (LeftScore1 , RightScore1 ) of
367+ LeftScore1 ->
360368 Left ;
361- RightScore ->
369+ RightScore1 ->
362370 Right
363371 end .
364372
365- select_better_regular_route (Left , Right ) ->
366- max (Left , Right ).
373+ select_better_regular_route ({LeftScore0 , LRoute }, {RightScore0 , RRoute }) ->
374+ LeftScore1 = LeftScore0 # domain_PaymentRouteScores {
375+ route_pin = 0
376+ },
377+ RightScore1 = RightScore0 # domain_PaymentRouteScores {
378+ route_pin = 0
379+ },
380+ case max ({LeftScore1 , LRoute }, {RightScore1 , RRoute }) of
381+ {LeftScore1 , LRoute } ->
382+ {LeftScore0 , LRoute };
383+ {RightScore1 , RRoute } ->
384+ {RightScore0 , RRoute }
385+ end .
367386
368387select_better_route_ideal (Left , Right ) ->
369388 IdealLeft = set_ideal_score (Left ),
@@ -525,20 +544,18 @@ score_route_ext({Route, ProviderStatus}) ->
525544score_route (Route ) ->
526545 PriorityRate = hg_route :priority (Route ),
527546 Pin = hg_route :pin (Route ),
528- {RandomCondition , PinHash } =
529- case Pin of
530- #{} when map_size (Pin ) == 0 ->
531- {hg_route :weight (Route ), ? ZERO };
532- _ ->
533- {? ZERO , erlang :phash2 (Pin )}
534- end ,
535547 # domain_PaymentRouteScores {
536548 terminal_priority_rating = PriorityRate ,
537- route_pin = PinHash ,
538- random_condition = RandomCondition ,
549+ route_pin = get_pin_hash ( Pin ) ,
550+ random_condition = hg_route : weight ( Route ) ,
539551 blacklist_condition = 0
540552 }.
541553
554+ get_pin_hash (Pin ) when map_size (Pin ) == 0 ->
555+ ? ZERO ;
556+ get_pin_hash (Pin ) ->
557+ erlang :phash2 (Pin ).
558+
542559get_availability_score ({alive , FailRate }) -> {1 , 1.0 - FailRate };
543560get_availability_score ({dead , FailRate }) -> {0 , 1.0 - FailRate }.
544561
@@ -868,6 +885,42 @@ pin_random_test() ->
868885 lists :seq (0 , 1000 )
869886 ).
870887
888+ - spec pin_weight_test () -> _ .
889+ pin_weight_test () ->
890+ Pin0 = #{
891+ email => <<" example@mail.com" >>
892+ },
893+ Pin1 = #{
894+ email => <<" example2@mail.com" >>
895+ },
896+ Scores = {{alive , 0.0 }, {normal , 0.0 }},
897+ Route1 = {hg_route :new (? prv (1 ), ? trm (1 ), 50 , 1 , Pin0 ), Scores },
898+ Route2 = {hg_route :new (? prv (2 ), ? trm (2 ), 50 , 1 , Pin1 ), Scores },
899+ {_ , DiffTimes } = lists :foldl (
900+ fun (_I , {Acc , Iter }) ->
901+ BalancedRoutes = balance_routes ([Route1 , Route2 ]),
902+ ScoredRoutes = score_routes (BalancedRoutes ),
903+ {{_ , ChosenScoredRoute }, _IdealRoute } = find_best_routes (ScoredRoutes ),
904+ case Acc of
905+ undefined ->
906+ {ChosenScoredRoute , Iter };
907+ _ ->
908+ ChosenTerminal = hg_route :terminal_ref (ChosenScoredRoute ),
909+ case hg_route :terminal_ref (Acc ) of
910+ ChosenTerminal ->
911+ {Acc , Iter };
912+ _ ->
913+ {Acc , Iter + 1 }
914+ end
915+ end
916+ end ,
917+ {undefined , 0 },
918+ lists :seq (0 , 1000 )
919+ ),
920+ ? assertNotEqual (0 , DiffTimes ),
921+ ? assertEqual (true , DiffTimes > 300 ),
922+ ? assertEqual (true , DiffTimes < 700 ).
923+
871924- spec balance_routes_test_ () -> [testcase ()].
872925balance_routes_test_ () ->
873926 Status = {{alive , 0.0 }, {normal , 0.0 }},
0 commit comments