@@ -142,7 +142,6 @@ process_request(?PUBACK,
142
142
{QMsgId , U } ->
143
143
case rabbit_queue_type :settle (QName , complete , ? CONSUMER_TAG , [QMsgId ], QStates0 ) of
144
144
{ok , QStates , Actions } ->
145
- % %TODO rabbit_channel:incr_queue_stats/3
146
145
message_acknowledged (QName , State ),
147
146
{ok , handle_queue_actions (Actions , State # state {unacked_server_pubs = U ,
148
147
queue_states = QStates })};
@@ -349,14 +348,14 @@ process_connect(#mqtt_packet{
349
348
PacketConnect , State0 ) of
350
349
{ok , SessionPresent0 , State1 } ->
351
350
{? CONNACK_ACCEPT , SessionPresent0 , State1 };
352
- {error , ReturnCode0 , State1 } ->
353
- {ReturnCode0 , false , State1 }
351
+ {error , ConnectionRefusedReturnCode , State1 } ->
352
+ {ConnectionRefusedReturnCode , false , State1 }
354
353
end ,
355
- ResponsePacket = # mqtt_packet {fixed = # mqtt_packet_fixed {type = ? CONNACK },
356
- variable = # mqtt_packet_connack {
357
- session_present = SessionPresent ,
358
- return_code = ReturnCode }},
359
- SendFun (ResponsePacket , State ),
354
+ Response = # mqtt_packet {fixed = # mqtt_packet_fixed {type = ? CONNACK },
355
+ variable = # mqtt_packet_connack {
356
+ session_present = SessionPresent ,
357
+ return_code = ReturnCode }},
358
+ SendFun (Response , State ),
360
359
return_connack (ReturnCode , State ).
361
360
362
361
check_protocol_version (# mqtt_packet_connect {proto_ver = ProtoVersion }) ->
@@ -368,7 +367,7 @@ check_protocol_version(#mqtt_packet_connect{proto_ver = ProtoVersion}) ->
368
367
end .
369
368
370
369
check_client_id (# mqtt_packet_connect {clean_sess = false ,
371
- client_id = [] }) ->
370
+ client_id = <<>> }) ->
372
371
{error , ? CONNACK_ID_REJECTED };
373
372
check_client_id (_ ) ->
374
373
ok .
@@ -401,12 +400,10 @@ login({UserBin, PassBin,
401
400
State0 ) ->
402
401
ClientId = ensure_client_id (ClientId0 ),
403
402
case process_login (UserBin , PassBin , ClientId , State0 ) of
404
- already_connected ->
405
- {ok , already_connected };
406
403
{ok , State } ->
407
404
{ok , Packet , State # state {clean_sess = CleanSess ,
408
405
client_id = ClientId }};
409
- {error , _Reason , _State } = Err ->
406
+ {error , _ConnectionRefusedReturnCode , _State } = Err ->
410
407
Err
411
408
end .
412
409
@@ -419,8 +416,6 @@ ensure_client_id(ClientId)
419
416
when is_binary (ClientId ) ->
420
417
ClientId .
421
418
422
- register_client (already_connected , _State ) ->
423
- ok ;
424
419
register_client (Packet = # mqtt_packet_connect {proto_ver = ProtoVersion },
425
420
State = # state {client_id = ClientId ,
426
421
socket = Socket ,
@@ -462,8 +457,6 @@ register_client(Packet = #mqtt_packet_connect{proto_ver = ProtoVersion},
462
457
{ok , NewProcState (undefined )}
463
458
end .
464
459
465
- notify_connection_created (already_connected ) ->
466
- ok ;
467
460
notify_connection_created (# mqtt_packet_connect {}) ->
468
461
rabbit_networking :register_non_amqp_connection (self ()),
469
462
self () ! connection_created ,
@@ -616,7 +609,7 @@ maybe_unbind(TopicName, TopicNames, QName, State0) ->
616
609
{ok , _Output , State } ->
617
610
{ok , State };
618
611
{error , Reason , _State } = Err ->
619
- rabbit_log :error (" Failed to unbind ~s with topic ~s : ~p " ,
612
+ rabbit_log :error (" Failed to unbind ~s with topic ' ~s ' : ~p " ,
620
613
[rabbit_misc :rs (QName ), TopicName , Reason ]),
621
614
Err
622
615
end
@@ -674,17 +667,18 @@ make_will_msg(#mqtt_packet_connect{will_retain = Retain,
674
667
dup = false ,
675
668
payload = Msg }.
676
669
677
- process_login (_UserBin , _PassBin , _ClientId ,
670
+ process_login (_UserBin , _PassBin , ClientId ,
678
671
# state {peer_addr = Addr ,
679
672
auth_state = # auth_state {username = Username ,
680
673
user = User ,
681
674
vhost = VHost
682
- }})
675
+ }} = State )
683
676
when Username =/= undefined , User =/= undefined , VHost =/= underfined ->
684
677
rabbit_core_metrics :auth_attempt_failed (list_to_binary (inet :ntoa (Addr )), Username , mqtt ),
685
- rabbit_log_connection :warning (" MQTT detected duplicate connect/login attempt for user ~ts , vhost ~ts " ,
686
- [Username , VHost ]),
687
- already_connected ;
678
+ rabbit_log_connection :error (
679
+ " MQTT detected duplicate connect attempt for client ID '~ts ', user '~ts ', vhost '~ts '" ,
680
+ [ClientId , Username , VHost ]),
681
+ {error , ? CONNACK_ID_REJECTED , State };
688
682
process_login (UserBin , PassBin , ClientId ,
689
683
# state {socket = Sock ,
690
684
ssl_login_name = SslLoginName ,
@@ -713,7 +707,7 @@ process_login(UserBin, PassBin, ClientId,
713
707
{ok , _Output , State } ->
714
708
rabbit_core_metrics :auth_attempt_succeeded (RemoteIpAddressBin , UsernameBin , mqtt ),
715
709
{ok , State };
716
- {error , _Reason , _State } = Err ->
710
+ {error , _ConnectionRefusedReturnCode , _State } = Err ->
717
711
rabbit_core_metrics :auth_attempt_failed (RemoteIpAddressBin , UsernameBin , mqtt ),
718
712
Err
719
713
end .
@@ -730,30 +724,30 @@ check_vhost_exists(#{vhost := VHost,
730
724
end .
731
725
732
726
check_vhost_connection_limit (#{vhost := VHost ,
733
- username_bin := UsernameBin }) ->
727
+ client_id := ClientId ,
728
+ username_bin := Username }) ->
734
729
case rabbit_vhost_limit :is_over_connection_limit (VHost ) of
735
730
false ->
736
731
ok ;
737
732
{true , Limit } ->
738
733
rabbit_log_connection :error (
739
- " Error on MQTT connection ~p~n "
740
- " access to vhost '~s ' refused for user '~s ': "
741
- " vhost connection limit (~p ) is reached" ,
742
- [self (), VHost , UsernameBin , Limit ]),
734
+ " Failed to create MQTT connection because vhost connection limit is reached; "
735
+ " vhost: '~s '; connection limit: ~p ; user: '~s '; client ID '~s '" ,
736
+ [VHost , Limit , Username , ClientId ]),
743
737
{error , ? CONNACK_NOT_AUTHORIZED }
744
738
end .
745
739
746
740
check_vhost_alive (#{vhost := VHost ,
741
+ client_id := ClientId ,
747
742
username_bin := UsernameBin }) ->
748
743
case rabbit_vhost_sup_sup :is_vhost_alive (VHost ) of
749
744
true ->
750
745
ok ;
751
746
false ->
752
747
rabbit_log_connection :error (
753
- " Error on MQTT connection ~p~n "
754
- " access refused for user '~s ': "
755
- " vhost is down" ,
756
- [self (), UsernameBin , VHost ]),
748
+ " Failed to create MQTT connection because vhost is down; "
749
+ " vhost: ~s ; user: ~s ; client ID: ~s " ,
750
+ [VHost , UsernameBin , ClientId ]),
757
751
{error , ? CONNACK_NOT_AUTHORIZED }
758
752
end .
759
753
@@ -798,16 +792,16 @@ notify_auth_result(AuthResult, Username, #state{conn_name = ConnName}) ->
798
792
{connection_name , ConnName },
799
793
{connection_type , network }]).
800
794
801
- check_user_connection_limit (#{user := # user {username = Username }}) ->
795
+ check_user_connection_limit (#{user := # user {username = Username },
796
+ client_id := ClientId }) ->
802
797
case rabbit_auth_backend_internal :is_over_connection_limit (Username ) of
803
798
false ->
804
799
ok ;
805
800
{true , Limit } ->
806
801
rabbit_log_connection :error (
807
- " Error on MQTT connection ~p~n "
808
- " access refused for user '~s ': "
809
- " user connection limit (~p ) is reached" ,
810
- [self (), Username , Limit ]),
802
+ " Failed to create MQTT connection because user connection limit is reached; "
803
+ " user: '~s '; connection limit: ~p ; client ID '~s '" ,
804
+ [Username , Limit , ClientId ]),
811
805
{error , ? CONNACK_NOT_AUTHORIZED }
812
806
end .
813
807
@@ -1230,8 +1224,6 @@ deliver_to_queues(Delivery,
1230
1224
RoutedToQNames ,
1231
1225
State0 = # state {queue_states = QStates0 ,
1232
1226
proto_ver = ProtoVer }) ->
1233
- % % TODO only lookup fields that are needed using ets:select / match?
1234
- % % TODO Use ETS continuations to be more space efficient
1235
1227
Qs0 = rabbit_amqqueue :lookup (RoutedToQNames ),
1236
1228
Qs = rabbit_amqqueue :prepend_extra_bcc (Qs0 ),
1237
1229
case rabbit_queue_type :deliver (Qs , Delivery , QStates0 ) of
@@ -1588,7 +1580,7 @@ maybe_publish_to_client(
1588
1580
routing_keys = [RoutingKey | _CcRoutes ],
1589
1581
content = # content {payload_fragments_rev = FragmentsRev }}},
1590
1582
QoS , State0 = # state {send_fun = SendFun }) ->
1591
- {PacketId , State } = queue_packet_id_to_packet_id (QMsgId , QoS , State0 ),
1583
+ {PacketId , State } = msg_id_to_packet_id (QMsgId , QoS , State0 ),
1592
1584
Packet =
1593
1585
# mqtt_packet {
1594
1586
fixed = # mqtt_packet_fixed {
@@ -1609,11 +1601,11 @@ maybe_publish_to_client(
1609
1601
message_delivered (QNameOrType , Redelivered , QoS , State ),
1610
1602
State .
1611
1603
1612
- queue_packet_id_to_packet_id (_ , ? QOS_0 , State ) ->
1604
+ msg_id_to_packet_id (_ , ? QOS_0 , State ) ->
1613
1605
% % "A PUBLISH packet MUST NOT contain a Packet Identifier if its QoS value is set to 0 [MQTT-2.2.1-2]."
1614
1606
{undefined , State };
1615
- queue_packet_id_to_packet_id (QMsgId , ? QOS_1 , # state {packet_id = PktId ,
1616
- unacked_server_pubs = U } = State ) ->
1607
+ msg_id_to_packet_id (QMsgId , ? QOS_1 , # state {packet_id = PktId ,
1608
+ unacked_server_pubs = U } = State ) ->
1617
1609
{PktId , State # state {packet_id = increment_packet_id (PktId ),
1618
1610
unacked_server_pubs = maps :put (PktId , QMsgId , U )}}.
1619
1611
@@ -1626,14 +1618,8 @@ increment_packet_id(Id) ->
1626
1618
1627
1619
maybe_auto_ack (_AckRequired = true , ? QOS_0 , QName , QMsgId ,
1628
1620
State = # state {queue_states = QStates0 }) ->
1629
- case rabbit_queue_type :settle (QName , complete , ? CONSUMER_TAG , [QMsgId ], QStates0 ) of
1630
- {ok , QStates , Actions } ->
1631
- % %TODO rabbit_channel:incr_queue_stats/3
1632
- handle_queue_actions (Actions , State # state {queue_states = QStates });
1633
- {protocol_error , _ErrorType , _Reason , _ReasonArgs } = Err ->
1634
- % %TODO handle error
1635
- throw (Err )
1636
- end ;
1621
+ {ok , QStates , Actions } = rabbit_queue_type :settle (QName , complete , ? CONSUMER_TAG , [QMsgId ], QStates0 ),
1622
+ handle_queue_actions (Actions , State # state {queue_states = QStates });
1637
1623
maybe_auto_ack (_ , _ , _ , _ , State ) ->
1638
1624
State .
1639
1625
0 commit comments