You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: app.rs
    path of file: ./repos/crates.io/src
    
    the code of the file until where you have to start completion: //! Application-wide components in a struct accessible from each request

use crate::config;
use crate::db::{connection_url, ConnectionConfig, DieselPool, DieselPooledConn, PoolError};
use std::ops::Deref;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

use crate::downloads_counter::DownloadsCounter;
use crate::email::Emails;
use crate::metrics::{InstanceMetrics, ServiceMetrics};
use crate::rate_limiter::RateLimiter;
use crate::storage::Storage;
use axum::extract::{FromRef, FromRequestParts, State};
use crates_io_github::GitHubClient;
use deadpool_diesel::postgres::{Manager as DeadpoolManager, Pool as DeadpoolPool};
use deadpool_diesel::Runtime;
use diesel::r2d2;
use moka::future::{Cache, CacheBuilder};
use oauth2::basic::BasicClient;
use scheduled_thread_pool::ScheduledThreadPool;

type DeadpoolResult = Result<deadpool_diesel::postgres::Connection, deadpool_diesel::PoolError>;

/// The `App` struct holds the main components of the application like
/// the database connection pool and configurations
pub struct App {
    /// The primary database connection pool
    pub primary_database: DieselPool,

    /// Async database connection pool based on `deadpool` connected
    /// to the primary database
    pub deadpool_primary: DeadpoolPool,

    /// The read-only replica database connection pool
    pub read_only_replica_database: Option<DieselPool>,

    /// Async database connection pool based on `deadpool` connected
    /// to the read-only replica database
    pub deadpool_replica: Option<DeadpoolPool>,

    /// GitHub API client
    pub github: Box<dyn GitHubClient>,

    /// The GitHub OAuth2 configuration
    pub github_oauth: BasicClient,

    /// The server configuration
    pub config: Arc<config::Server>,

    /// Cache the `version_id` of a `canonical_crate_name:semver` pair
    ///
    /// This is used by the download endpoint to reduce the number of database queries. The
    /// `version_id` is only cached under the canonical spelling of the crate name.
    pub(crate) version_id_cacher: Cache<(String, String), i32>,

    /// Count downloads and periodically persist them in the database
    pub downloads_counter: DownloadsCounter,

    /// Backend used to send emails
    pub emails: Emails,

    /// Storage backend for crate files and other large objects.
    pub storage: Arc<Storage>,

    /// Metrics related to the service as a whole
    pub service_metrics: ServiceMetrics,

    /// Metrics related to this specific instance of the service
    pub instance_metrics: InstanceMetrics,

    /// In-flight request counters for the `balance_capacity` middleware.
    pub balance_capacity: BalanceCapacityState,

    /// Rate limit select actions.
    pub rate_limiter: RateLimiter,
}

impl App {
    /// Creates a new `App` with a given `Config` and an optional HTTP `Client`
    ///
    /// Configures and sets up:
    ///
    /// - GitHub OAuth
    /// - Database connection pools
    /// - A `git2::Repository` instance from the index repo checkout (that server.rs ensures exists)
    pub fn new(config: config::Server, emails: Emails, github: Box<dyn GitHubClient>) -> App {
        use oauth2::{AuthUrl, TokenUrl};

        let instance_metrics =
            InstanceMetrics::new().expect("could not initialize instance metrics");

        let github_oauth = BasicClient::new(
            config.gh_client_id.clone(),
            Some(config.gh_client_secret.clone()),
            AuthUrl::new(String::from("https://github.com/login/oauth/authorize")).unwrap(),
            Some(
                TokenUrl::new(String::from("https://github.com/login/oauth/access_token")).unwrap(),
            ),
        );

        let thread_pool = Arc::new(ScheduledThreadPool::new(config.db.helper_threads));

        let primary_database = {
            let primary_db_connection_config = ConnectionConfig {
                statement_timeout: config.db.statement_timeout,
                read_only: config.db.primary.read_only_mode,
            };

            let primary_db_config = r2d2::Pool::builder()
                .max_size(config.db.primary.pool_size)
                .min_idle(config.db.primary.min_idle)
                .connection_timeout(config.db.connection_timeout)
                .connection_customizer(Box::new(primary_db_connection_config))
                .thread_pool(thread_pool
    
    pub fn new(config: config::Server, emails: Emails, github: Box<dyn GitHubClient>) -> App {
        use oauth2::{AuthUrl, TokenUrl};

        let instance_metrics =
            InstanceMetrics::new().expect("could not initialize instance metrics");

        let github_oauth = BasicClient::new(
            config.gh_client_id.clone(),
            Some(config.gh_client_secret.clone()),
            AuthUrl::new(String::from("https://github.com/login/oauth/authorize")).unwrap(),
            Some(
                TokenUrl::new(String::from("https://github.com/login/oauth/access_token")).unwrap(),
            ),
        );

        let thread_pool = Arc::new(ScheduledThreadPool::new(config.db.helper_threads));

        let primary_database = {
            let primary_db_connection_config = ConnectionConfig {
                statement_timeout: config.db.statement_timeout,
                read_only: config.db.primary.read_only_mode,
            };

            let primary_db_config = r2d2::Pool::builder()
                .max_size(config.db.primary.pool_size)
                .min_idle(config.db.primary.min_idle)
                .connection_timeout(config.db.connection_timeout)
                .connection_customizer(Box::new(primary_db_connection_config))
                .thread_pool(thread_pool.clone());

            DieselPool::new(
                &config.db.primary.url,
                &config.db,
                primary_db_config,
                instance_metrics
                    .database_time_to_obtain_connection
                    .with_label_values(&["primary"]),
            )
            .unwrap()
        };

        let primary_database_async = {
            use secrecy::ExposeSecret;

            let primary_db_connection_config = ConnectionConfig {
                statement_timeout: config.db.statement_timeout,
                read_only: config.db.primary.read_only_mode,
            };

            let url = connection_url(&config.db, config.db.primary.url.expose_secret());
            let manager = DeadpoolManager::new(url, Runtime::Tokio1);

            DeadpoolPool::builder(manager)
                .runtime(Runtime::Tokio1)
                .max_size(config.db.primary.async_pool_size)
                .wait_timeout(Some(config.db.connection_timeout))
                .post_create(primary_db_connection_config)
                .build()
                .unwrap()
        };

        let replica_database = if let Some(pool_config) = config.db.replica.as_ref() {
            let replica_db_connection_config = ConnectionConfig {
                statement_timeout: config.db.statement_timeout,
                read_only: pool_config.read_only_mode,
            };

            let replica_db_config = r2d2::Pool::builder()
                .max_size(pool_config.pool_size)
                .min_idle(pool_config.min_idle)
                .connection_timeout(config.db.connection_timeout)
                .connection_customizer(Box::new(replica_db_connection_config))
                .thread_pool(thread_pool);

            Some(
                DieselPool::new(
                    &pool_config.url,
                    &config.db,
                    replica_db_config,
                    instance_metrics
                        .database_time_to_obtain_connection
                        .with_label_values(&["follower"]),
                )
                .unwrap(),
            )
        } else {
            None
        };

        let replica_database_async = if let Some(pool_config) = config.db.replica.as_ref() {
            use secrecy::ExposeSecret;

            let replica_db_connection_config = ConnectionConfig {
                statement_timeout: config.db.statement_timeout,
                read_only: pool_config.read_only_mode,
            };

            let url = connection_url(&config.db, pool_config.url.expose_secret());
            let manager = DeadpoolManager::new(url, Runtime::Tokio1);

            let pool = DeadpoolPool::builder(manager)
                .runtime(Runtime::Tokio1)
                .max_size(pool_config.async_pool_size)
                .wait_timeout(Some(config.db.connection_timeout))
                .post_create(replica_db_connection_config)
                .build()
                .unwrap();

            Some(pool)
        } else {
            None
        };

        let version_id_cacher = CacheBuilder::new(config.version_id_cache_size)
            .time_to_live(config.version_id_cache_ttl)
            .build();

        App {
            primary_database,
            deadpool_primary: primary_database_async,
            read_only_replica_database: replica_database,
            deadpool_replica: replica_database_async,
            github,
            github_oauth,
            version_id_cacher,
            downloads_counter: DownloadsCounter::new(),
            emails,
            storage: Arc::new(Storage::from_config(&config.storage)),
            service_metrics: ServiceMetrics::new().expect("could not initialize service metrics"),
            instance_metrics,
            balance_capacity: Default::default(),
            rate_limiter: RateLimiter::new(config.rate_limiter.clone()),
            config: Arc::new(config),
        }
    }