|
@@ -21,7 +21,7 @@ use url::Url;
|
|
|
#[derive(Debug)]
|
|
|
pub struct Relayer {
|
|
|
/// URL of the relayer
|
|
|
- pub url: String,
|
|
|
+ pub url: Url,
|
|
|
/// Sender to the relayer. This can be used to send a Requests to this
|
|
|
/// relayer
|
|
|
pub send_to_socket: mpsc::Sender<Request>,
|
|
@@ -42,12 +42,12 @@ impl Drop for Relayer {
|
|
|
impl Relayer {
|
|
|
/// Creates a new relayer
|
|
|
pub fn new<F>(
|
|
|
- broadcast_to_listeners: mpsc::Sender<(Event, String)>,
|
|
|
- url: &str,
|
|
|
+ broadcast_to_listeners: mpsc::Sender<(Event, Url)>,
|
|
|
+ url: Url,
|
|
|
on_connection: Option<F>,
|
|
|
- ) -> Result<Self, Error>
|
|
|
+ ) -> Self
|
|
|
where
|
|
|
- F: (Fn(&str, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ F: (Fn(&Url, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ Send
|
|
|
+ Sync
|
|
|
+ 'static,
|
|
@@ -58,45 +58,42 @@ impl Relayer {
|
|
|
broadcast_to_listeners,
|
|
|
sender_to_socket.clone(),
|
|
|
send_to_socket,
|
|
|
- url,
|
|
|
+ url.clone(),
|
|
|
is_connected.clone(),
|
|
|
on_connection,
|
|
|
- )?;
|
|
|
+ );
|
|
|
|
|
|
- Ok(Self {
|
|
|
- url: url.to_owned(),
|
|
|
+ Self {
|
|
|
+ url,
|
|
|
is_connected,
|
|
|
send_to_socket: sender_to_socket,
|
|
|
worker,
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
fn spawn_background_client<F>(
|
|
|
- broadcast_to_listeners: mpsc::Sender<(Event, String)>,
|
|
|
+ broadcast_to_listeners: mpsc::Sender<(Event, Url)>,
|
|
|
sender_to_socket: mpsc::Sender<Request>,
|
|
|
mut send_to_socket: mpsc::Receiver<Request>,
|
|
|
- url_str: &str,
|
|
|
+ url: Url,
|
|
|
is_connected: Arc<AtomicBool>,
|
|
|
on_connection: Option<F>,
|
|
|
- ) -> Result<JoinHandle<()>, Error>
|
|
|
+ ) -> JoinHandle<()>
|
|
|
where
|
|
|
- F: (Fn(&str, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ F: (Fn(&Url, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ Send
|
|
|
+ Sync
|
|
|
+ 'static,
|
|
|
{
|
|
|
- let url = url_str.to_owned();
|
|
|
- let url_parsed = Url::parse(&url)?;
|
|
|
-
|
|
|
is_connected.store(false, Relaxed);
|
|
|
|
|
|
- Ok(tokio::spawn(async move {
|
|
|
+ tokio::spawn(async move {
|
|
|
let mut connection_attempts = 0;
|
|
|
|
|
|
loop {
|
|
|
log::warn!("{}: Connect attempt {}", url, connection_attempts);
|
|
|
connection_attempts += 1;
|
|
|
- let mut socket = match connect_async(url_parsed.clone()).await {
|
|
|
+ let mut socket = match connect_async(url.clone()).await {
|
|
|
Ok(x) => x.0,
|
|
|
Err(err) => {
|
|
|
log::warn!("{}: Failed to connect: {}", url, err);
|
|
@@ -155,7 +152,7 @@ impl Relayer {
|
|
|
let msg: Result<Response, _> = serde_json::from_str(&msg);
|
|
|
|
|
|
if let Ok(msg) = msg {
|
|
|
- if let Err(error) = broadcast_to_listeners.try_send((Event::Response(msg.into()), url.to_owned())) {
|
|
|
+ if let Err(error) = broadcast_to_listeners.try_send((Event::Response(msg.into()), url.clone())) {
|
|
|
log::error!("{}: Reconnecting client because of {}", url, error);
|
|
|
break;
|
|
|
}
|
|
@@ -172,7 +169,7 @@ impl Relayer {
|
|
|
// Throttle down to not spam the server with reconnections
|
|
|
sleep(Duration::from_millis(500)).await;
|
|
|
}
|
|
|
- }))
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/// Checks if the relayer is connected. It is guaranteed that the relayer is
|