Fix issue with adding multiple instances of chatserver.

This commit is contained in:
Dessalines 2020-01-15 22:01:14 -05:00
parent 9845fda05c
commit 81c6d276d5
5 changed files with 35 additions and 59 deletions

View file

@ -1 +1,2 @@
tab_spaces = 2 tab_spaces = 2
edition="2018"

View file

@ -2,11 +2,13 @@ extern crate lemmy_server;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
use actix::prelude::*;
use actix_web::*; use actix_web::*;
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection; use diesel::PgConnection;
use lemmy_server::routes::{federation, feeds, index, nodeinfo, webfinger, websocket}; use lemmy_server::routes::{federation, feeds, index, nodeinfo, webfinger, websocket};
use lemmy_server::settings::Settings; use lemmy_server::settings::Settings;
use lemmy_server::websocket::server::*;
use std::io; use std::io;
embed_migrations!(); embed_migrations!();
@ -27,6 +29,9 @@ async fn main() -> io::Result<()> {
let conn = pool.get().unwrap(); let conn = pool.get().unwrap();
embedded_migrations::run(&conn).unwrap(); embedded_migrations::run(&conn).unwrap();
// Set up websocket server
let server = ChatServer::startup(pool.clone()).start();
println!( println!(
"Starting http server at {}:{}", "Starting http server at {}:{}",
settings.bind, settings.port settings.bind, settings.port
@ -37,6 +42,7 @@ async fn main() -> io::Result<()> {
App::new() App::new()
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.data(pool.clone()) .data(pool.clone())
.data(server.clone())
// The routes // The routes
.configure(federation::config) .configure(federation::config)
.configure(feeds::config) .configure(feeds::config)

View file

@ -1,27 +1,12 @@
use crate::websocket::server::*; use crate::websocket::server::*;
use crate::Settings;
use actix::prelude::*; use actix::prelude::*;
use actix_web::web; use actix_web::web;
use actix_web::*; use actix_web::*;
use actix_web_actors::ws; use actix_web_actors::ws;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub fn config(cfg: &mut web::ServiceConfig) { pub fn config(cfg: &mut web::ServiceConfig) {
// TODO couldn't figure out how to get this method to recieve the other pool cfg.service(web::resource("/api/v1/ws").to(chat_route));
let settings = Settings::get();
let manager = ConnectionManager::<PgConnection>::new(&settings.get_database_url());
let pool = Pool::builder()
.max_size(settings.database.pool_size)
.build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", settings.get_database_url()));
// Start chat server actor in separate thread
let server = ChatServer::startup(pool).start();
cfg
.data(server)
.service(web::resource("/api/v1/ws").to(chat_route));
} }
/// How often heartbeat pings are sent /// How often heartbeat pings are sent
@ -38,8 +23,7 @@ async fn chat_route(
// TODO not sure if the blocking should be here or not // TODO not sure if the blocking should be here or not
ws::start( ws::start(
WSSession { WSSession {
// db: db.get_ref().clone(), cs_addr: chat_server.get_ref().to_owned(),
cs_addr: chat_server.get_ref().clone(),
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
ip: req ip: req

View file

@ -87,21 +87,6 @@ pub struct ChatServer {
db: Pool<ConnectionManager<PgConnection>>, db: Pool<ConnectionManager<PgConnection>>,
} }
// impl Default for ChatServer {
// fn default(nah: String) -> ChatServer {
// // default room
// let rooms = HashMap::new();
// ChatServer {
// sessions: HashMap::new(),
// rate_limits: HashMap::new(),
// rooms,
// rng: rand::thread_rng(),
// nah: nah,
// }
// }
// }
impl ChatServer { impl ChatServer {
pub fn startup(db: Pool<ConnectionManager<PgConnection>>) -> ChatServer { pub fn startup(db: Pool<ConnectionManager<PgConnection>>) -> ChatServer {
// default room // default room