Skip to content

Commit 4666938

Browse files
committed
Add events listener mechanism
First attempt at fixing #443
1 parent 24d8a20 commit 4666938

6 files changed

Lines changed: 138 additions & 25 deletions

File tree

examples/connection.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use futures_lite::stream::StreamExt;
12
use lapin::{
23
BasicProperties, Connection, ConnectionProperties, message::DeliveryResult, options::*,
34
publisher_confirm::Confirmation, types::FieldTable,
@@ -18,11 +19,21 @@ fn main() {
1819
.await
1920
.expect("connection error");
2021

22+
let mut events_listener = conn.events_listener();
23+
24+
async_global_executor::spawn(async move {
25+
while let Some(event) = events_listener.next().await {
26+
info!(?event, "GOT EVENT");
27+
}
28+
})
29+
.detach();
30+
2131
info!("CONNECTED");
2232

2333
{
2434
//send channel
2535
let channel_a = conn.create_channel().await.expect("create_channel");
36+
2637
//receive channel
2738
let channel_b = conn.create_channel().await.expect("create_channel");
2839
info!(state=?conn.status().state());

src/channel.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
consumer::Consumer,
1313
consumers::Consumers,
1414
error_handler::ErrorHandler,
15+
events::EventsSender,
1516
frames::{ExpectedReply, Frames},
1617
internal_rpc::InternalRPCHandle,
1718
message::{BasicGetMessage, BasicReturnMessage, Delivery},
@@ -54,9 +55,10 @@ pub struct Channel {
5455
internal_rpc: InternalRPCHandle,
5556
frames: Frames,
5657
error_handler: ErrorHandler,
58+
events_sender: EventsSender,
5759
executor: Arc<dyn FullExecutor + Send + Sync>,
5860
channel_closer: Option<Arc<ChannelCloser>>,
59-
connection_closer: Option<Arc<ConnectionCloser>>,
61+
_connection_closer: Option<Arc<ConnectionCloser>>,
6062
recovery_config: RecoveryConfig,
6163
}
6264

@@ -94,6 +96,7 @@ impl Channel {
9496
executor: Arc<dyn FullExecutor + Send + Sync>,
9597
connection_closer: Option<Arc<ConnectionCloser>>,
9698
recovery_config: RecoveryConfig,
99+
events_sender: EventsSender,
97100
) -> Channel {
98101
let returned_messages = ReturnedMessages::default();
99102
let status = ChannelStatus::new(channel_id, internal_rpc.clone());
@@ -120,9 +123,10 @@ impl Channel {
120123
internal_rpc,
121124
frames,
122125
error_handler: ErrorHandler::default(),
126+
events_sender,
123127
executor,
124128
channel_closer,
125-
connection_closer,
129+
_connection_closer: connection_closer,
126130
recovery_config,
127131
}
128132
}
@@ -193,25 +197,9 @@ impl Channel {
193197
}
194198

195199
pub(crate) fn clone_internal(&self) -> Self {
196-
Self {
197-
id: self.id,
198-
configuration: self.configuration.clone(),
199-
status: self.status.clone(),
200-
connection_status: self.connection_status.clone(),
201-
local_registry: self.local_registry.clone(),
202-
acknowledgements: self.acknowledgements.clone(),
203-
consumers: self.consumers.clone(),
204-
basic_get_delivery: self.basic_get_delivery.clone(),
205-
returned_messages: self.returned_messages.clone(),
206-
waker: self.waker.clone(),
207-
internal_rpc: self.internal_rpc.clone(),
208-
frames: self.frames.clone(),
209-
error_handler: self.error_handler.clone(),
210-
executor: self.executor.clone(),
211-
channel_closer: None,
212-
connection_closer: self.connection_closer.clone(),
213-
recovery_config: self.recovery_config.clone(),
214-
}
200+
let mut this = self.clone();
201+
this.channel_closer = None;
202+
this
215203
}
216204

217205
fn wake(&self) {
@@ -658,6 +646,7 @@ impl Channel {
658646
if !recover {
659647
self.set_closed(err);
660648
if let Some(error) = error {
649+
self.events_sender.error(error.clone());
661650
self.error_handler.on_error(error);
662651
}
663652
}
@@ -883,6 +872,7 @@ impl Channel {
883872
{
884873
self.connection_status.set_state(ConnectionState::Connected);
885874
resolver.resolve(*connection);
875+
self.events_sender.connected();
886876
Ok(())
887877
} else {
888878
error!(?state, ?step, "Invalid state");
@@ -919,6 +909,7 @@ impl Channel {
919909

920910
fn on_connection_blocked_received(&self, _method: protocol::connection::Blocked) -> Result<()> {
921911
self.connection_status.block();
912+
self.events_sender.connection_blocked();
922913
Ok(())
923914
}
924915

@@ -927,6 +918,7 @@ impl Channel {
927918
_method: protocol::connection::Unblocked,
928919
) -> Result<()> {
929920
self.connection_status.unblock();
921+
self.events_sender.connection_unblocked();
930922
self.wake();
931923
Ok(())
932924
}

src/channels.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
ConnectionState, ConnectionStatus, Error, ErrorKind, Promise, Result,
44
connection_closer::ConnectionCloser,
55
error_handler::ErrorHandler,
6+
events::{Events, EventsSender},
67
frames::Frames,
78
heartbeat::Heartbeat,
89
id_sequence::IdSequence,
@@ -36,6 +37,7 @@ pub(crate) struct Channels {
3637
heartbeat: Heartbeat,
3738
connection_killswitch: KillSwitch,
3839
error_handler: ErrorHandler,
40+
events: Events,
3941
uri: AMQPUri,
4042
options: ConnectionProperties,
4143
recovery_config: RecoveryConfig,
@@ -52,6 +54,7 @@ impl Channels {
5254
executor: Arc<dyn FullExecutor + Send + Sync>,
5355
uri: AMQPUri,
5456
options: ConnectionProperties,
57+
events: Events,
5558
) -> Self {
5659
let recovery_config = options.recovery_config.clone().unwrap_or_default();
5760
let mut inner = Inner::new(configuration.clone(), waker, recovery_config.clone());
@@ -61,6 +64,7 @@ impl Channels {
6164
internal_rpc.clone(),
6265
frames.clone(),
6366
executor.clone(),
67+
events.sender(),
6468
None,
6569
);
6670
channel0.set_state(ChannelState::Connected);
@@ -76,6 +80,7 @@ impl Channels {
7680
heartbeat,
7781
connection_killswitch: KillSwitch::default(),
7882
error_handler: ErrorHandler::default(),
83+
events,
7984
uri,
8085
options,
8186
recovery_config,
@@ -88,6 +93,7 @@ impl Channels {
8893
self.internal_rpc.clone(),
8994
self.frames.clone(),
9095
self.executor.clone(),
96+
self.events.sender(),
9197
connection_closer,
9298
)
9399
}
@@ -190,6 +196,7 @@ impl Channels {
190196
}
191197

192198
self.frames.drop_pending(error.clone());
199+
self.events.sender().error(error.clone());
193200
self.error_handler.on_error(error.clone());
194201
for (id, channel) in self.lock_inner().channels.iter() {
195202
self.frames.clear_expected_replies(*id, error.clone());
@@ -352,6 +359,7 @@ impl Channels {
352359
self.connection_status.clone(),
353360
self.clone(),
354361
self.internal_rpc.clone(),
362+
self.events.clone(),
355363
)
356364
.start(self.uri.clone(), self.options.clone())
357365
.await?;
@@ -431,6 +439,7 @@ impl Inner {
431439
internal_rpc: InternalRPCHandle,
432440
frames: Frames,
433441
executor: Arc<dyn FullExecutor + Send + Sync>,
442+
events_sender: EventsSender,
434443
connection_closer: Option<Arc<ConnectionCloser>>,
435444
) -> Channel {
436445
debug!(%id, "create channel");
@@ -444,6 +453,7 @@ impl Inner {
444453
executor,
445454
connection_closer,
446455
self.recovery_config.clone(),
456+
events_sender,
447457
)
448458
}
449459

@@ -453,6 +463,7 @@ impl Inner {
453463
internal_rpc: InternalRPCHandle,
454464
frames: Frames,
455465
executor: Arc<dyn FullExecutor + Send + Sync>,
466+
events_sender: EventsSender,
456467
connection_closer: Arc<ConnectionCloser>,
457468
) -> Result<Channel> {
458469
debug!("create channel");
@@ -474,6 +485,7 @@ impl Inner {
474485
internal_rpc,
475486
frames,
476487
executor,
488+
events_sender,
477489
Some(connection_closer),
478490
);
479491
self.channels.insert(id, channel.clone_internal());

src/connection.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{
2-
ConnectionProperties, Error, ErrorKind, Promise, Result,
2+
ConnectionProperties, Error, ErrorKind, Event, Promise, Result,
33
channel::Channel,
44
channels::Channels,
55
configuration::Configuration,
66
connection_closer::ConnectionCloser,
77
connection_status::ConnectionStatus,
8+
events::Events,
89
frames::Frames,
910
heartbeat::Heartbeat,
1011
internal_rpc::{InternalRPC, InternalRPCHandle},
@@ -17,6 +18,7 @@ use crate::{
1718
};
1819
use amq_protocol::frame::{AMQPFrame, ProtocolVersion};
1920
use async_trait::async_trait;
21+
use futures_core::Stream;
2022
use std::{fmt, io, sync::Arc};
2123
use tracing::{Level, level_enabled, trace};
2224

@@ -35,6 +37,7 @@ pub struct Connection {
3537
configuration: Configuration,
3638
status: ConnectionStatus,
3739
channels: Channels,
40+
events: Events,
3841
io_loop: ThreadHandle,
3942
closer: Arc<ConnectionCloser>,
4043
}
@@ -45,12 +48,14 @@ impl Connection {
4548
status: ConnectionStatus,
4649
channels: Channels,
4750
internal_rpc: InternalRPCHandle,
51+
events: Events,
4852
) -> Self {
4953
let closer = Arc::new(ConnectionCloser::new(status.clone(), internal_rpc));
5054
Self {
5155
configuration,
5256
status,
5357
channels,
58+
events,
5459
io_loop: ThreadHandle::default(),
5560
closer,
5661
}
@@ -61,8 +66,9 @@ impl Connection {
6166
status: ConnectionStatus,
6267
channels: Channels,
6368
internal_rpc: InternalRPCHandle,
69+
events: Events,
6470
) -> Self {
65-
let conn = Self::new(configuration, status, channels, internal_rpc);
71+
let conn = Self::new(configuration, status, channels, internal_rpc, events);
6672
conn.closer.noop();
6773
conn
6874
}
@@ -120,6 +126,11 @@ impl Connection {
120126
channel.clone().channel_open(channel).await
121127
}
122128

129+
/// Get a Stream of connection Events
130+
pub fn events_listener(&self) -> impl Stream<Item = Event> + Send + 'static {
131+
self.events.listener()
132+
}
133+
123134
/// Block current thread while the connection is still active.
124135
/// This is useful when you only have a consumer and nothing else keeping your application
125136
/// "alive".
@@ -190,6 +201,7 @@ impl Connection {
190201
let socket_state = SocketState::default();
191202
let internal_rpc = InternalRPC::new(executor.clone(), socket_state.handle());
192203
let heartbeat = Heartbeat::new(status.clone(), executor.clone(), reactor.clone());
204+
let events = Events::new();
193205
let channels = Channels::new(
194206
configuration.clone(),
195207
status.clone(),
@@ -200,8 +212,15 @@ impl Connection {
200212
executor,
201213
uri.clone(),
202214
options.clone(),
215+
events.clone(),
216+
);
217+
let conn = Connection::new(
218+
configuration,
219+
status,
220+
channels,
221+
internal_rpc.handle(),
222+
events,
203223
);
204-
let conn = Connection::new(configuration, status, channels, internal_rpc.handle());
205224
let io_loop = IoLoop::new(
206225
conn.status.clone(),
207226
conn.configuration.clone(),
@@ -328,6 +347,7 @@ mod tests {
328347
let socket_state = SocketState::default();
329348
let internal_rpc = InternalRPC::new(executor.clone(), socket_state.handle());
330349
let heartbeat = Heartbeat::new(status.clone(), executor.clone(), reactor);
350+
let events = Events::new();
331351
let channels = Channels::new(
332352
configuration.clone(),
333353
status.clone(),
@@ -338,8 +358,15 @@ mod tests {
338358
executor,
339359
uri.clone(),
340360
ConnectionProperties::default(),
361+
events.clone(),
362+
);
363+
let conn = Connection::new(
364+
configuration,
365+
status,
366+
channels,
367+
internal_rpc.handle(),
368+
events,
341369
);
342-
let conn = Connection::new(configuration, status, channels, internal_rpc.handle());
343370
conn.status.set_state(ConnectionState::Connected);
344371
conn
345372
}

0 commit comments

Comments
 (0)