Skip to content

Commit

Permalink
Message passing solution
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 21, 2024
1 parent 4b9ae61 commit eacb841
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use miniserve::{http::StatusCode, Content, Request, Response};
use serde::{Deserialize, Serialize};
use tokio::join;
use tokio::{
join,
sync::{mpsc, oneshot},
};

async fn index(_req: Request) -> Response {
let content = include_str!("../index.html").to_string();
Expand All @@ -14,6 +17,25 @@ struct Messages {
messages: Vec<String>,
}

async fn query_chat(messages: &Arc<Vec<String>>) -> Vec<String> {
type Payload = (Arc<Vec<String>>, oneshot::Sender<Vec<String>>);
static SENDER: LazyLock<mpsc::Sender<Payload>> = LazyLock::new(|| {
let (tx, mut rx) = mpsc::channel::<Payload>(1024);
tokio::spawn(async move {
let mut chatbot = chatbot::Chatbot::new(vec![":-)".into(), "^^".into()]);
while let Some((messages, responder)) = rx.recv().await {
let response = chatbot.query_chat(&messages).await;
responder.send(response).unwrap();
}
});
tx
});

let (tx, rx) = oneshot::channel();
SENDER.send((Arc::clone(messages), tx)).await.unwrap();
rx.await.unwrap()
}

async fn chat(req: Request) -> Response {
let Request::Post(body) = req else {
return Err(StatusCode::METHOD_NOT_ALLOWED);
Expand All @@ -23,12 +45,7 @@ async fn chat(req: Request) -> Response {
};

let messages = Arc::new(data.messages);
let messages_ref = Arc::clone(&messages);
let (i, responses) = join!(
chatbot::gen_random_number(),
tokio::spawn(async move { chatbot::query_chat(&messages_ref).await })
);
let mut responses = responses.unwrap();
let (i, mut responses) = join!(chatbot::gen_random_number(), query_chat(&messages));

let response = responses.remove(i % responses.len());
data.messages = Arc::into_inner(messages).unwrap();
Expand Down

0 comments on commit eacb841

Please sign in to comment.