Skip to content

Commit

Permalink
Replace OnceLock with LazyLock
Browse files Browse the repository at this point in the history
Resolve clippies
  • Loading branch information
hatchan committed Aug 2, 2024
1 parent 1d275a1 commit 4f0e7e1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
2 changes: 1 addition & 1 deletion fpx-lib/src/api/handlers/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fpx_macros::ApiError;
use http::StatusCode;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tracing::{error, info};
use tracing::error;

#[tracing::instrument(skip_all)]
pub async fn span_get_handler(
Expand Down
1 change: 1 addition & 0 deletions fpx-lib/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Result<T, E = DbError> = anyhow::Result<T, E>;

pub type BoxedStore = Arc<dyn Store>;

#[derive(Clone, Default, Debug)]
pub struct Transaction {}

impl Transaction {
Expand Down
11 changes: 1 addition & 10 deletions fpx-lib/src/data/fake_store.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
use super::{models, DbError, Result, Store, Transaction};
use async_trait::async_trait;
use std::sync::{Arc, RwLock};
use tracing::info;

/// A simple in-memory [`Store`] implementation. Currently only intended for
/// proof of concept.
///
/// This implementation does not provide any Transaction support, nor will it
/// work concurrently.
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct FakeStore {
/// Spans are stored in a [`RwLock`] so that we can always mutate the inner
/// [`Vec`], even with a reference to this [`FakeStore`].
spans: Arc<RwLock<Vec<models::Span>>>,
}

impl FakeStore {
pub fn new() -> Self {
Self {
spans: Arc::new(RwLock::new(vec![])),
}
}
}

#[async_trait]
impl Store for FakeStore {
async fn start_readonly_transaction(&self) -> Result<Transaction> {
Expand Down
11 changes: 3 additions & 8 deletions fpx-workers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use fpx_lib::data::fake_store::FakeStore;
use fpx_lib::events::ServerEvents;
use fpx_lib::{api, service};
use std::sync::{Arc, OnceLock};
use std::sync::{Arc, LazyLock};
use tower_service::Service;
use tracing::info;
use tracing_subscriber::fmt::format::Pretty;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::prelude::*;
use tracing_web::{performance_layer, MakeConsoleWriter};
use worker::*;

static FAKE_STORE: OnceLock<FakeStore> = OnceLock::new();
static FAKE_STORE: LazyLock<FakeStore> = LazyLock::new(FakeStore::default);

#[event(start)]
fn start() {
Expand All @@ -24,10 +23,6 @@ fn start() {
.with(fmt_layer)
.with(perf_layer)
.init();

FAKE_STORE
.set(FakeStore::new())
.expect("failed to set FakeStore");
}

#[event(fetch)]
Expand All @@ -38,7 +33,7 @@ async fn fetch(
) -> Result<axum::http::Response<axum::body::Body>> {
console_error_panic_hook::set_once();

let store = FAKE_STORE.get().unwrap().clone();
let store = FAKE_STORE.clone();
let boxed_store = Arc::new(store);
let events = ServerEvents::new();

Expand Down

0 comments on commit 4f0e7e1

Please sign in to comment.