@@ -164,9 +164,15 @@ impl Connection {
164
164
let ( published_sender, published_receiver) = mpsc:: unbounded ( ) ;
165
165
let ( error_sender, error_receiver) = mpsc:: unbounded ( ) ;
166
166
let ( pub_done_sender, pub_done_receiver) = oneshot:: channel ( ) ;
167
+ let subscriptions = Arc :: new ( Mutex :: new ( config. subscriptions . clone ( ) ) ) ;
167
168
168
- let ( mqtt_client, event_loop) =
169
- Connection :: open ( config, received_sender. clone ( ) , error_sender. clone ( ) ) . await ?;
169
+ let ( mqtt_client, event_loop) = Connection :: open (
170
+ config,
171
+ received_sender. clone ( ) ,
172
+ error_sender. clone ( ) ,
173
+ subscriptions. clone ( ) ,
174
+ )
175
+ . await ?;
170
176
let permits = Arc :: new ( Semaphore :: new ( 1 ) ) ;
171
177
let permit = permits. clone ( ) . acquire_owned ( ) . await . unwrap ( ) ;
172
178
let pub_count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
@@ -179,6 +185,7 @@ impl Connection {
179
185
pub_done_sender,
180
186
permits,
181
187
pub_count. clone ( ) ,
188
+ subscriptions. clone ( ) ,
182
189
) ) ;
183
190
tokio:: spawn ( Connection :: sender_loop (
184
191
mqtt_client. clone ( ) ,
@@ -194,7 +201,7 @@ impl Connection {
194
201
published : published_sender,
195
202
errors : error_receiver,
196
203
pub_done : pub_done_receiver,
197
- subscriptions : SubscriberHandle :: new ( mqtt_client, config . subscriptions . clone ( ) ) ,
204
+ subscriptions : SubscriberHandle :: new ( mqtt_client, subscriptions) ,
198
205
} )
199
206
}
200
207
@@ -207,6 +214,7 @@ impl Connection {
207
214
config : & Config ,
208
215
mut message_sender : mpsc:: UnboundedSender < MqttMessage > ,
209
216
mut error_sender : mpsc:: UnboundedSender < MqttError > ,
217
+ subscriptions : Arc < Mutex < TopicFilter > > ,
210
218
) -> Result < ( AsyncClient , EventLoop ) , MqttError > {
211
219
const INSECURE_MQTT_PORT : u16 = 1883 ;
212
220
const SECURE_MQTT_PORT : u16 = 8883 ;
@@ -234,7 +242,7 @@ impl Connection {
234
242
} ;
235
243
info ! ( target: "MQTT" , "Connection established" ) ;
236
244
237
- let subscriptions = config . subscriptions . lock ( ) . unwrap ( ) . filters ( ) ;
245
+ let subscriptions = subscriptions. lock ( ) . unwrap ( ) . filters ( ) ;
238
246
239
247
// Need check here otherwise it will hang waiting for a SubAck, and none will come when there is no subscription.
240
248
if subscriptions. is_empty ( ) {
@@ -291,6 +299,7 @@ impl Connection {
291
299
done : oneshot:: Sender < ( ) > ,
292
300
permits : Arc < Semaphore > ,
293
301
pub_count : Arc < AtomicUsize > ,
302
+ subscriptions : Arc < Mutex < TopicFilter > > ,
294
303
) -> Result < ( ) , MqttError > {
295
304
let mut triggered_disconnect = false ;
296
305
let mut disconnect_permit = None ;
@@ -363,7 +372,7 @@ impl Connection {
363
372
// If session_name is not provided or if the broker session persistence
364
373
// is not enabled or working, then re-subscribe
365
374
366
- let subscriptions = config . subscriptions . lock ( ) . unwrap ( ) . filters ( ) ;
375
+ let subscriptions = subscriptions. lock ( ) . unwrap ( ) . filters ( ) ;
367
376
// Need check here otherwise it will hang waiting for a SubAck, and none will come when there is no subscription.
368
377
if subscriptions. is_empty ( ) {
369
378
break ;
0 commit comments