Batch requests
This commit is contained in:
parent
d828a4b0f0
commit
ae107978f7
39
src/fetch.rs
39
src/fetch.rs
@ -2,10 +2,10 @@ use std::collections::VecDeque;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use nostr::{Event, EventId, Filter, Kind, Url};
|
|
||||||
use nostr::prelude::Nip19;
|
use nostr::prelude::Nip19;
|
||||||
|
use nostr::{Event, EventId, Filter, Kind, Url};
|
||||||
use nostr_sdk::{FilterOptions, RelayOptions, RelayPool};
|
use nostr_sdk::{FilterOptions, RelayOptions, RelayPool};
|
||||||
use tokio::sync::{Mutex, oneshot};
|
use tokio::sync::{oneshot, Mutex};
|
||||||
|
|
||||||
struct QueueItem {
|
struct QueueItem {
|
||||||
pub handler: oneshot::Sender<Option<Event>>,
|
pub handler: oneshot::Sender<Option<Event>>,
|
||||||
@ -30,8 +30,7 @@ impl FetchQueue {
|
|||||||
let pool_lock = self.pool.lock().await;
|
let pool_lock = self.pool.lock().await;
|
||||||
pool_lock
|
pool_lock
|
||||||
.add_relay(relay.clone(), RelayOptions::default())
|
.add_relay(relay.clone(), RelayOptions::default())
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
if let Err(e) = pool_lock.connect_relay(relay, None).await {
|
if let Err(e) = pool_lock.connect_relay(relay, None).await {
|
||||||
Err(anyhow::Error::new(e))
|
Err(anyhow::Error::new(e))
|
||||||
} else {
|
} else {
|
||||||
@ -52,21 +51,23 @@ impl FetchQueue {
|
|||||||
|
|
||||||
pub async fn process_queue(&self) {
|
pub async fn process_queue(&self) {
|
||||||
let mut q_lock = self.queue.lock().await;
|
let mut q_lock = self.queue.lock().await;
|
||||||
if let Some(q) = q_lock.pop_front() {
|
let mut batch = Vec::new();
|
||||||
let pool_lock = self.pool.lock().await;
|
while let Some(q) = q_lock.pop_front() {
|
||||||
let filters = vec![Self::nip19_to_filter(&q.request).unwrap()];
|
batch.push(q);
|
||||||
//info!("Sending filters: {:?}", filters);
|
}
|
||||||
if let Ok(evs) = pool_lock
|
let filters: Vec<Filter> =
|
||||||
.get_events_of(filters, Duration::from_secs(5), FilterOptions::ExitOnEOSE)
|
batch.iter().map(move |x| Self::nip19_to_filter(&x.request).unwrap()).collect();
|
||||||
.await
|
|
||||||
{
|
let pool_lock = self.pool.lock().await;
|
||||||
if let Some(e) = evs.first() {
|
info!("Sending filters: {:?}", &filters);
|
||||||
q.handler.send(Some(e.clone())).unwrap();
|
if let Ok(evs) = pool_lock
|
||||||
} else {
|
.get_events_of(filters, Duration::from_secs(2), FilterOptions::ExitOnEOSE)
|
||||||
q.handler.send(None).unwrap();
|
.await
|
||||||
}
|
{
|
||||||
} else {
|
for b in batch {
|
||||||
q.handler.send(None).unwrap();
|
let f = Self::nip19_to_filter(&b.request).unwrap();
|
||||||
|
let ev = evs.iter().find(|e| f.match_event(e));
|
||||||
|
b.handler.send(ev.cloned()).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user