-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move websocket to separate worker
- Loading branch information
Showing
4 changed files
with
47 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::Args; | ||
use flume::Receiver; | ||
use futures_util::StreamExt; | ||
use log::{info, warn}; | ||
use reqwest::Url; | ||
use std::time::Duration; | ||
use tokio_tungstenite::{connect_async, tungstenite::Message}; | ||
|
||
pub async fn websocket_worker(args: Args, rx: Receiver<Message>) -> anyhow::Result<()> { | ||
// wss://hostname/api/ws/worker/:hostname | ||
let hostname = gethostname::gethostname().to_string_lossy().to_string(); | ||
let ws = Url::parse(&args.server.replace("http", "ws"))? | ||
.join("api/")? | ||
.join("ws/")? | ||
.join("worker/")? | ||
.join(&hostname)?; | ||
|
||
loop { | ||
info!("Starting websocket connect to {:?}", ws); | ||
match connect_async(ws.as_str()).await { | ||
Ok((ws_stream, _)) => { | ||
let (write, _) = ws_stream.split(); | ||
let rx = rx.clone().into_stream(); | ||
if let Err(e) = rx.map(Ok).forward(write).await { | ||
warn!("Failed to forward message to websocket: {e}"); | ||
} | ||
} | ||
Err(err) => { | ||
warn!("Got error connecting to websocket: {}", err); | ||
} | ||
} | ||
|
||
tokio::time::sleep(Duration::from_secs(5)).await; | ||
} | ||
} |