Skip to content

Commit 115d425

Browse files
committed
More flume purge
put in a horrible unsafe hack to figure out if 2 senders are the same channel. To be replaced by a PR on async_channel.
1 parent c4d002a commit 115d425

File tree

6 files changed

+87
-58
lines changed

6 files changed

+87
-58
lines changed

iroh-blobs/src/util/progress.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,19 @@ impl<T> Clone for FlumeProgressSender<T> {
471471
}
472472
}
473473

474+
fn same_channel<T>(a: &async_channel::Sender<T>, b: &async_channel::Sender<T>) -> bool {
475+
assert!(std::mem::size_of::<async_channel::Sender<T>>() == std::mem::size_of::<usize>());
476+
fn get_arc_reference<T>(x: &async_channel::Sender<T>) -> &Arc<()> {
477+
unsafe {
478+
// Transmute the reference to MyNewType to a reference to Arc<()>
479+
std::mem::transmute::<_, &Arc<()>>(x)
480+
}
481+
}
482+
let a = get_arc_reference(a);
483+
let b = get_arc_reference(b);
484+
Arc::ptr_eq(a, b)
485+
}
486+
474487
impl<T> FlumeProgressSender<T> {
475488
/// Create a new progress sender from a flume sender.
476489
pub fn new(sender: async_channel::Sender<T>) -> Self {
@@ -482,8 +495,7 @@ impl<T> FlumeProgressSender<T> {
482495

483496
/// Returns true if `other` sends on the same `flume` channel as `self`.
484497
pub fn same_channel(&self, other: &FlumeProgressSender<T>) -> bool {
485-
self.id.load(std::sync::atomic::Ordering::SeqCst)
486-
== other.id.load(std::sync::atomic::Ordering::SeqCst)
498+
same_channel(&self.sender, &other.sender)
487499
}
488500
}
489501

iroh-docs/src/actor.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ enum Action {
6161
#[display("ListAuthors")]
6262
ListAuthors {
6363
#[debug("reply")]
64-
reply: flume::Sender<Result<AuthorId>>,
64+
reply: async_channel::Sender<Result<AuthorId>>,
6565
},
6666
#[display("ListReplicas")]
6767
ListReplicas {
6868
#[debug("reply")]
69-
reply: flume::Sender<Result<(NamespaceId, CapabilityKind)>>,
69+
reply: async_channel::Sender<Result<(NamespaceId, CapabilityKind)>>,
7070
},
7171
#[display("ContentHashes")]
7272
ContentHashes {
@@ -108,12 +108,12 @@ enum ReplicaAction {
108108
reply: oneshot::Sender<Result<()>>,
109109
},
110110
Subscribe {
111-
sender: flume::Sender<Event>,
111+
sender: async_channel::Sender<Event>,
112112
#[debug("reply")]
113113
reply: oneshot::Sender<Result<()>>,
114114
},
115115
Unsubscribe {
116-
sender: flume::Sender<Event>,
116+
sender: async_channel::Sender<Event>,
117117
#[debug("reply")]
118118
reply: oneshot::Sender<Result<()>>,
119119
},
@@ -166,7 +166,7 @@ enum ReplicaAction {
166166
},
167167
GetMany {
168168
query: Query,
169-
reply: flume::Sender<Result<SignedEntry>>,
169+
reply: async_channel::Sender<Result<SignedEntry>>,
170170
},
171171
DropReplica {
172172
reply: oneshot::Sender<Result<()>>,
@@ -222,7 +222,7 @@ struct OpenReplica {
222222
/// [`SyncHandle::drop`] will not block.
223223
#[derive(Debug, Clone)]
224224
pub struct SyncHandle {
225-
tx: flume::Sender<Action>,
225+
tx: async_channel::Sender<Action>,
226226
join_handle: Arc<Option<JoinHandle<()>>>,
227227
}
228228

@@ -232,7 +232,7 @@ pub struct OpenOpts {
232232
/// Set to true to set sync state to true.
233233
pub sync: bool,
234234
/// Optionally subscribe to replica events.
235-
pub subscribe: Option<flume::Sender<Event>>,
235+
pub subscribe: Option<async_channel::Sender<Event>>,
236236
}
237237
impl OpenOpts {
238238
/// Set sync state to true.
@@ -241,7 +241,7 @@ impl OpenOpts {
241241
self
242242
}
243243
/// Subscribe to replica events.
244-
pub fn subscribe(mut self, subscribe: flume::Sender<Event>) -> Self {
244+
pub fn subscribe(mut self, subscribe: async_channel::Sender<Event>) -> Self {
245245
self.subscribe = Some(subscribe);
246246
self
247247
}
@@ -255,7 +255,7 @@ impl SyncHandle {
255255
content_status_callback: Option<ContentStatusCallback>,
256256
me: String,
257257
) -> SyncHandle {
258-
let (action_tx, action_rx) = flume::bounded(ACTION_CAP);
258+
let (action_tx, action_rx) = async_channel::bounded(ACTION_CAP);
259259
let actor = Actor {
260260
store,
261261
states: Default::default(),
@@ -298,7 +298,7 @@ impl SyncHandle {
298298
pub async fn subscribe(
299299
&self,
300300
namespace: NamespaceId,
301-
sender: flume::Sender<Event>,
301+
sender: async_channel::Sender<Event>,
302302
) -> Result<()> {
303303
let (reply, rx) = oneshot::channel();
304304
self.send_replica(namespace, ReplicaAction::Subscribe { sender, reply })
@@ -309,7 +309,7 @@ impl SyncHandle {
309309
pub async fn unsubscribe(
310310
&self,
311311
namespace: NamespaceId,
312-
sender: flume::Sender<Event>,
312+
sender: async_channel::Sender<Event>,
313313
) -> Result<()> {
314314
let (reply, rx) = oneshot::channel();
315315
self.send_replica(namespace, ReplicaAction::Unsubscribe { sender, reply })
@@ -435,7 +435,7 @@ impl SyncHandle {
435435
&self,
436436
namespace: NamespaceId,
437437
query: Query,
438-
reply: flume::Sender<Result<SignedEntry>>,
438+
reply: async_channel::Sender<Result<SignedEntry>>,
439439
) -> Result<()> {
440440
let action = ReplicaAction::GetMany { query, reply };
441441
self.send_replica(namespace, action).await?;
@@ -489,13 +489,13 @@ impl SyncHandle {
489489
Ok(store)
490490
}
491491

492-
pub async fn list_authors(&self, reply: flume::Sender<Result<AuthorId>>) -> Result<()> {
492+
pub async fn list_authors(&self, reply: async_channel::Sender<Result<AuthorId>>) -> Result<()> {
493493
self.send(Action::ListAuthors { reply }).await
494494
}
495495

496496
pub async fn list_replicas(
497497
&self,
498-
reply: flume::Sender<Result<(NamespaceId, CapabilityKind)>>,
498+
reply: async_channel::Sender<Result<(NamespaceId, CapabilityKind)>>,
499499
) -> Result<()> {
500500
self.send(Action::ListReplicas { reply }).await
501501
}
@@ -566,7 +566,7 @@ impl SyncHandle {
566566

567567
async fn send(&self, action: Action) -> Result<()> {
568568
self.tx
569-
.send_async(action)
569+
.send(action)
570570
.await
571571
.context("sending to iroh_docs actor failed")?;
572572
Ok(())
@@ -581,7 +581,7 @@ impl Drop for SyncHandle {
581581
fn drop(&mut self) {
582582
// this means we're dropping the last reference
583583
if let Some(handle) = Arc::get_mut(&mut self.join_handle) {
584-
self.tx.send(Action::Shutdown { reply: None }).ok();
584+
self.tx.send_blocking(Action::Shutdown { reply: None }).ok();
585585
let handle = handle.take().expect("this can only run once");
586586
if let Err(err) = handle.join() {
587587
warn!(?err, "Failed to join sync actor");
@@ -593,7 +593,7 @@ impl Drop for SyncHandle {
593593
struct Actor {
594594
store: Store,
595595
states: OpenReplicas,
596-
action_rx: flume::Receiver<Action>,
596+
action_rx: async_channel::Receiver<Action>,
597597
content_status_callback: Option<ContentStatusCallback>,
598598
tasks: JoinSet<()>,
599599
}
@@ -619,10 +619,10 @@ impl Actor {
619619
}
620620
continue;
621621
}
622-
action = self.action_rx.recv_async() => {
622+
action = self.action_rx.recv() => {
623623
match action {
624624
Ok(action) => action,
625-
Err(flume::RecvError::Disconnected) => {
625+
Err(async_channel::RecvError) => {
626626
debug!("action channel disconnected");
627627
break None;
628628
}
@@ -979,17 +979,14 @@ impl OpenReplicas {
979979
}
980980

981981
async fn iter_to_channel_async<T: Send + 'static>(
982-
channel: flume::Sender<Result<T>>,
982+
channel: async_channel::Sender<Result<T>>,
983983
iter: Result<impl Iterator<Item = Result<T>>>,
984984
) -> Result<(), SendReplyError> {
985985
match iter {
986-
Err(err) => channel
987-
.send_async(Err(err))
988-
.await
989-
.map_err(send_reply_error)?,
986+
Err(err) => channel.send(Err(err)).await.map_err(send_reply_error)?,
990987
Ok(iter) => {
991988
for item in iter {
992-
channel.send_async(item).await.map_err(send_reply_error)?;
989+
channel.send(item).await.map_err(send_reply_error)?;
993990
}
994991
}
995992
}
@@ -1032,10 +1029,10 @@ mod tests {
10321029
let id = namespace.id();
10331030
sync.import_namespace(namespace.into()).await?;
10341031
sync.open(id, Default::default()).await?;
1035-
let (tx, rx) = flume::bounded(10);
1032+
let (tx, rx) = async_channel::bounded(10);
10361033
sync.subscribe(id, tx).await?;
10371034
sync.close(id).await?;
1038-
assert!(rx.recv_async().await.is_err());
1035+
assert!(rx.recv().await.is_err());
10391036
Ok(())
10401037
}
10411038
}

iroh-docs/src/engine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ impl Engine {
170170

171171
// Subscribe to insert events from the replica.
172172
let a = {
173-
let (s, r) = flume::bounded(SUBSCRIBE_CHANNEL_CAP);
173+
let (s, r) = async_channel::bounded(SUBSCRIBE_CHANNEL_CAP);
174174
this.sync.subscribe(namespace, s).await?;
175-
r.into_stream()
176-
.map(move |ev| LiveEvent::from_replica_event(ev, &content_status_cb))
175+
Box::pin(r).map(move |ev| LiveEvent::from_replica_event(ev, &content_status_cb))
177176
};
178177

179178
// Subscribe to events from the [`live::Actor`].

iroh-docs/src/engine/live.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ pub struct LiveActor<B: iroh_blobs::store::Store> {
153153
gossip: Gossip,
154154
bao_store: B,
155155
downloader: Downloader,
156-
replica_events_tx: flume::Sender<crate::Event>,
157-
replica_events_rx: flume::Receiver<crate::Event>,
156+
replica_events_tx: async_channel::Sender<crate::Event>,
157+
replica_events_rx: async_channel::Receiver<crate::Event>,
158158

159159
/// Send messages to self.
160160
/// Note: Must not be used in methods called from `Self::run` directly to prevent deadlocks.
@@ -192,7 +192,7 @@ impl<B: iroh_blobs::store::Store> LiveActor<B> {
192192
sync_actor_tx: mpsc::Sender<ToLiveActor>,
193193
gossip_actor_tx: mpsc::Sender<ToGossipActor>,
194194
) -> Self {
195-
let (replica_events_tx, replica_events_rx) = flume::bounded(1024);
195+
let (replica_events_tx, replica_events_rx) = async_channel::bounded(1024);
196196
Self {
197197
inbox,
198198
sync,
@@ -262,7 +262,7 @@ impl<B: iroh_blobs::store::Store> LiveActor<B> {
262262
}
263263
}
264264
}
265-
event = self.replica_events_rx.recv_async() => {
265+
event = self.replica_events_rx.recv() => {
266266
trace!(?i, "tick: replica_event");
267267
inc!(Metrics, doc_live_tick_replica_event);
268268
let event = event.context("replica_events closed")?;

iroh-docs/src/sync.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,31 @@ pub struct SyncOutcome {
108108
pub num_sent: usize,
109109
}
110110

111+
fn same_channel<T>(a: &async_channel::Sender<T>, b: &async_channel::Sender<T>) -> bool {
112+
assert!(std::mem::size_of::<async_channel::Sender<T>>() == std::mem::size_of::<usize>());
113+
fn get_arc_reference<T>(x: &async_channel::Sender<T>) -> &Arc<()> {
114+
unsafe {
115+
// Transmute the reference to MyNewType to a reference to Arc<()>
116+
std::mem::transmute::<_, &Arc<()>>(x)
117+
}
118+
}
119+
let a = get_arc_reference(a);
120+
let b = get_arc_reference(b);
121+
Arc::ptr_eq(a, b)
122+
}
123+
111124
#[derive(Debug, Default)]
112-
struct Subscribers(Vec<flume::Sender<Event>>);
125+
struct Subscribers(Vec<async_channel::Sender<Event>>);
113126
impl Subscribers {
114-
pub fn subscribe(&mut self, sender: flume::Sender<Event>) {
127+
pub fn subscribe(&mut self, sender: async_channel::Sender<Event>) {
115128
self.0.push(sender)
116129
}
117-
pub fn unsubscribe(&mut self, sender: &flume::Sender<Event>) {
118-
self.0.retain(|s| !s.same_channel(sender));
130+
pub fn unsubscribe(&mut self, sender: &async_channel::Sender<Event>) {
131+
self.0.retain(|s| !same_channel(s, &sender));
119132
}
120133
pub fn send(&mut self, event: Event) {
121-
self.0.retain(|sender| sender.send(event.clone()).is_ok())
134+
self.0
135+
.retain(|sender| sender.send_blocking(event.clone()).is_ok())
122136
}
123137
pub fn len(&self) -> usize {
124138
self.0.len()
@@ -263,10 +277,10 @@ impl ReplicaInfo {
263277

264278
/// Subscribe to insert events.
265279
///
266-
/// When subscribing to a replica, you must ensure that the corresponding [`flume::Receiver`] is
280+
/// When subscribing to a replica, you must ensure that the corresponding [`async_channel::Receiver`] is
267281
/// received from in a loop. If not receiving, local and remote inserts will hang waiting for
268282
/// the receiver to be received from.
269-
pub fn subscribe(&mut self, sender: flume::Sender<Event>) {
283+
pub fn subscribe(&mut self, sender: async_channel::Sender<Event>) {
270284
self.subscribers.subscribe(sender)
271285
}
272286

@@ -275,7 +289,7 @@ impl ReplicaInfo {
275289
/// Simply dropping the receiver is fine too. If you cloned a single sender to subscribe to
276290
/// multiple replicas, you can use this method to explicitly unsubscribe the sender from
277291
/// this replica without having to drop the receiver.
278-
pub fn unsubscribe(&mut self, sender: &flume::Sender<Event>) {
292+
pub fn unsubscribe(&mut self, sender: &async_channel::Sender<Event>) {
279293
self.subscribers.unsubscribe(sender)
280294
}
281295

@@ -2156,6 +2170,14 @@ mod tests {
21562170
Ok(())
21572171
}
21582172

2173+
fn drain(events: async_channel::Receiver<Event>) -> Vec<Event> {
2174+
let mut res = vec![];
2175+
while let Ok(ev) = events.try_recv() {
2176+
res.push(ev);
2177+
}
2178+
res
2179+
}
2180+
21592181
/// This tests that no events are emitted for entries received during sync which are obsolete
21602182
/// (too old) by the time they are actually inserted in the store.
21612183
#[test]
@@ -2173,8 +2195,8 @@ mod tests {
21732195
let mut replica1 = store1.new_replica(namespace.clone())?;
21742196
let mut replica2 = store2.new_replica(namespace.clone())?;
21752197

2176-
let (events1_sender, events1) = flume::bounded(32);
2177-
let (events2_sender, events2) = flume::bounded(32);
2198+
let (events1_sender, events1) = async_channel::bounded(32);
2199+
let (events2_sender, events2) = async_channel::bounded(32);
21782200

21792201
replica1.info.subscribe(events1_sender);
21802202
replica2.info.subscribe(events2_sender);
@@ -2198,8 +2220,8 @@ mod tests {
21982220
.sync_process_message(from1, peer1, &mut state2)
21992221
.unwrap();
22002222
assert!(from2.is_none());
2201-
let events1 = events1.drain().collect::<Vec<_>>();
2202-
let events2 = events2.drain().collect::<Vec<_>>();
2223+
let events1 = drain(events1);
2224+
let events2 = drain(events2);
22032225
assert_eq!(events1.len(), 1);
22042226
assert_eq!(events2.len(), 1);
22052227
assert!(matches!(events1[0], Event::LocalInsert { .. }));

0 commit comments

Comments
 (0)