chore (backend-rs): do not use Arc for internal actor caches at least for now

This commit is contained in:
naskya 2024-07-13 22:32:55 +09:00
parent 9b37933644
commit 7024dc67f4
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C

View file

@ -5,7 +5,7 @@
use super::*;
use crate::{database::db_conn, model::entity::user};
use sea_orm::prelude::*;
use std::sync::{Arc, Mutex};
use std::sync::Mutex;
#[derive(thiserror::Error, Debug)]
pub enum Error {
@ -17,17 +17,21 @@ pub enum Error {
InternalActorNotFound(InternalActor),
}
static INSTANCE_ACTOR: Mutex<Option<Arc<user::Model>>> = Mutex::new(None);
static RELAY_ACTOR: Mutex<Option<Arc<user::Model>>> = Mutex::new(None);
static INSTANCE_ACTOR: Mutex<Option<user::Model>> = Mutex::new(None);
static RELAY_ACTOR: Mutex<Option<user::Model>> = Mutex::new(None);
fn set_instance_actor(value: Arc<user::Model>) {
let _ = INSTANCE_ACTOR.lock().map(|mut cache| *cache = Some(value));
fn set_instance_actor(value: &user::Model) {
let _ = INSTANCE_ACTOR
.lock()
.map(|mut cache| *cache = Some(value.to_owned()));
}
fn set_relay_actor(value: Arc<user::Model>) {
let _ = RELAY_ACTOR.lock().map(|mut cache| *cache = Some(value));
fn set_relay_actor(value: &user::Model) {
let _ = RELAY_ACTOR
.lock()
.map(|mut cache| *cache = Some(value.to_owned()));
}
async fn cache_instance_actor() -> Result<Arc<user::Model>, Error> {
async fn cache_instance_actor() -> Result<user::Model, Error> {
let actor = user::Entity::find()
.filter(user::Column::Username.eq(INSTANCE_ACTOR_USERNAME))
.filter(user::Column::Host.is_null())
@ -35,14 +39,13 @@ async fn cache_instance_actor() -> Result<Arc<user::Model>, Error> {
.await?;
if let Some(actor) = actor {
let arc = Arc::new(actor);
set_instance_actor(arc.clone());
Ok(arc)
set_instance_actor(&actor);
Ok(actor)
} else {
Err(Error::InternalActorNotFound(InternalActor::Instance))
}
}
async fn cache_relay_actor() -> Result<Arc<user::Model>, Error> {
async fn cache_relay_actor() -> Result<user::Model, Error> {
let actor = user::Entity::find()
.filter(user::Column::Username.eq(RELAY_ACTOR_USERNAME))
.filter(user::Column::Host.is_null())
@ -50,9 +53,8 @@ async fn cache_relay_actor() -> Result<Arc<user::Model>, Error> {
.await?;
if let Some(actor) = actor {
let arc = Arc::new(actor);
set_relay_actor(arc.clone());
Ok(arc)
set_relay_actor(&actor);
Ok(actor)
} else {
Err(Error::InternalActorNotFound(InternalActor::Relay))
}
@ -63,7 +65,7 @@ async fn cache_relay_actor() -> Result<Arc<user::Model>, Error> {
type User = user::Model;
#[macros::export(js_name = "getInternalActor")]
pub async fn get(actor: InternalActor) -> Result<Arc<User>, Error> {
pub async fn get(actor: InternalActor) -> Result<User, Error> {
match actor {
InternalActor::Instance => {
if let Some(cache) = INSTANCE_ACTOR.lock().ok().and_then(|cache| cache.clone()) {