2121import io .streamnative .pulsar .handlers .mqtt .common .systemtopic .ConnectEvent ;
2222import io .streamnative .pulsar .handlers .mqtt .common .systemtopic .EventListener ;
2323import io .streamnative .pulsar .handlers .mqtt .common .systemtopic .MqttEvent ;
24- import java .util .ArrayList ;
2524import java .util .Collection ;
25+ import java .util .LinkedHashSet ;
26+ import java .util .Set ;
2627import java .util .concurrent .ConcurrentHashMap ;
2728import java .util .concurrent .ConcurrentMap ;
2829import java .util .concurrent .TimeUnit ;
2930import java .util .function .Consumer ;
3031import lombok .Getter ;
3132import lombok .extern .slf4j .Slf4j ;
33+ import org .apache .pulsar .jetcd .shaded .io .vertx .core .impl .ConcurrentHashSet ;
3234
3335/**
3436 * Proxy connection manager.
@@ -38,7 +40,7 @@ public class MQTTConnectionManager {
3840
3941 private final ConcurrentMap <String , Connection > localConnections ;
4042
41- private final ConcurrentMap <String , Connection > eventConnections ;
43+ private final ConcurrentHashSet <String > eventClientIds ;
4244
4345 @ Getter
4446 private static final HashedWheelTimer sessionExpireInterval =
@@ -56,7 +58,7 @@ public class MQTTConnectionManager {
5658 public MQTTConnectionManager (String advertisedAddress ) {
5759 this .advertisedAddress = advertisedAddress ;
5860 this .localConnections = new ConcurrentHashMap <>(2048 );
59- this .eventConnections = new ConcurrentHashMap <>(2048 );
61+ this .eventClientIds = new ConcurrentHashSet <>(2048 );
6062 this .connectListener = new ConnectEventListener ();
6163 this .disconnectListener = new DisconnectEventListener ();
6264 }
@@ -102,11 +104,11 @@ public Collection<Connection> getLocalConnections() {
102104 return this .localConnections .values ();
103105 }
104106
105- public Collection <Connection > getAllConnections () {
106- Collection < Connection > connections = new ArrayList <>(this .localConnections .values ().size ()
107- + this .eventConnections . values () .size ());
108- connections .addAll (this .localConnections .values ());
109- connections .addAll (eventConnections . values () );
107+ public Collection <String > getAllConnectionsId () {
108+ Set < String > connections = new LinkedHashSet <>(this .localConnections .keySet ().size ()
109+ + this .eventClientIds .size ());
110+ connections .addAll (this .localConnections .keySet ());
111+ connections .addAll (eventClientIds );
110112 return connections ;
111113 }
112114
@@ -126,7 +128,7 @@ public void onChange(MqttEvent event) {
126128 log .warn ("[ConnectEvent] close existing connection : {}" , connection );
127129 connection .disconnect ();
128130 } else {
129- eventConnections . put (connectEvent .getClientId (), connection );
131+ eventClientIds . add (connectEvent .getClientId ());
130132 }
131133 }
132134 }
@@ -141,7 +143,7 @@ public void onChange(MqttEvent event) {
141143 if (event .getEventType () == DISCONNECT ) {
142144 ConnectEvent connectEvent = (ConnectEvent ) event .getSourceEvent ();
143145 if (!connectEvent .getAddress ().equals (advertisedAddress )) {
144- eventConnections .remove (connectEvent .getClientId ());
146+ eventClientIds .remove (connectEvent .getClientId ());
145147 }
146148 }
147149 }
0 commit comments