Repository: libsql/libsql
File: libsql-server/src/namespace/mod.rs (Rust)

Code before:
    async fn checkpoint(&self) -> anyhow::Result<()> {
        let conn = self.db.connection_maker().create().await?;
        conn.vacuum_if_needed().await?;
        conn.checkpoint().await?;
        Ok(())
    }

    async fn shutdown(mut self, should_checkpoint: bool) -> anyhow::Result<()> {
        self.tasks.shutdown().await;
        if should_checkpoint {
            self.checkpoint().await?;
        }
        self.db.shutdown().await?;
        Ok(())
    }

    pub fn jwt_key(&self) -> crate::Result<Option<jsonwebtoken::DecodingKey>> {
        let config = self.db_config_store.get();
        if let Some(jwt_key) = config.jwt_key.as_deref() {
            Ok(Some(
                parse_jwt_key(jwt_key).context("Could not parse JWT decoding key")?,
            ))
        } else {
            Ok(None)
        }
    }
}

pub struct ReplicaNamespaceConfig {
    pub base_path: Arc<Path>,
    pub max_response_size: u64,
    pub max_total_response_size: u64,
    /// grpc channel
    pub channel: Channel,
    /// grpc uri
    pub uri: Uri,
    /// Extensions to load for the database connection
    pub extensions: Arc<[PathBuf]>,
    /// Stats monitor
    pub stats_sender: StatsSender,
}

impl Namespace<ReplicaDatabase> {
    #[tracing::instrument(skip(config, reset, meta_store_handle))]
    async fn new_replica(
        config: &ReplicaNamespaceConfig,
        name: NamespaceName,
        reset: ResetCb,
        meta_store_handle: MetaStoreHandle,
    ) -> crate::Result<Self> {
        tracing::debug!("creating replica namespace");

Review comment (performance):
It would be best if you decoded the key only once, when the namespace is loaded. As per jsonwebtoken Documentation:
> All the different kind of keys we can use to decode a JWT. This key can be re-used so make sure you only initialize it once if you can for better performance.

Also, consider wrapping it in an `Arc`: cloning the key is not cheap.



Code after:

    async fn checkpoint(&self) -> anyhow::Result<()> {
        let conn = self.db.connection_maker().create().await?;
        conn.vacuum_if_needed().await?;
        conn.checkpoint().await?;
        Ok(())
    }

    async fn shutdown(mut self, should_checkpoint: bool) -> anyhow::Result<()> {
        self.tasks.shutdown().await;
        if should_checkpoint {
            self.checkpoint().await?;
        }
        self.db.shutdown().await?;
        Ok(())
    }

    pub fn config(&self) -> Arc<DatabaseConfig> {
        self.db_config_store.get()
    }

    pub fn jwt_key(&self) -> crate::Result<Option<jsonwebtoken::DecodingKey>> {
        let config = self.db_config_store.get();
        if let Some(jwt_key) = config.jwt_key.as_deref() {
            Ok(Some(
                parse_jwt_key(jwt_key).context("Could not parse JWT decoding key")?,
            ))
        } else {
            Ok(None)
        }
    }
}

pub struct ReplicaNamespaceConfig {
    pub base_path: Arc<Path>,
    pub max_response_size: u64,
    pub max_total_response_size: u64,
    /// grpc channel
    pub channel: Channel,
    /// grpc uri
    pub uri: Uri,
    /// Extensions to load for the database connection
    pub extensions: Arc<[PathBuf]>,
    /// Stats monitor
    pub stats_sender: StatsSender,
}

impl Namespace<ReplicaDatabase> {
    #[tracing::instrument(skip(config, reset, meta_store_handle))]
    async fn new_replica(
        config: &ReplicaNamespaceConfig,