Repository: tikv/tikv
File: src/server/config.rs (Rust)

Code before:
use engine_traits::{PerfLevel, perf_level_serde};
use grpcio::{CompressionAlgorithms, ResourceQuota};
use lazy_static::lazy_static;
use online_config::{ConfigChange, ConfigManager, OnlineConfig};
pub use raftstore::store::Config as RaftStoreConfig;
use raftstore::store::config::DEFAULT_SNAP_MAX_BYTES_PER_SEC;
use regex::Regex;
use tikv_util::{
    config::{self, ReadableDuration, ReadableSize, VersionTrack},
    sys::SysQuota,
    worker::Scheduler,
};

use super::{Result, snap::Task as SnapTask};
pub use crate::storage::config::Config as StorageConfig;

pub const DEFAULT_CLUSTER_ID: u64 = 0;
pub const DEFAULT_LISTENING_ADDR: &str = "127.0.0.1:20160";
const DEFAULT_ADVERTISE_LISTENING_ADDR: &str = "";
const DEFAULT_STATUS_ADDR: &str = "127.0.0.1:20180";

// Default settings related to gRPC
lazy_static! {
     static ref DEFAULT_GRPC_RAFT_CONN_NUM: usize = {
        // Use 8c as the default quota unit.
        let base_num = SysQuota::cpu_cores_quota().div(8.0).floor() as usize;
        // Max count for the default setting of raft connection num is limited
        // to 4.
        base_num.clamp(1, 4)
    };

    static ref DEFAULT_GRPC_CONCURRENCY: usize = {
        DEFAULT_GRPC_RAFT_CONN_NUM.mul(3_usize) + 2_usize
    };
}
const DEFAULT_GRPC_CONCURRENT_STREAM: i32 = 1024;
const DEFAULT_GRPC_MEMORY_POOL_QUOTA: u64 = isize::MAX as u64;
const DEFAULT_GRPC_STREAM_INITIAL_WINDOW_SIZE: u64 = 2 * 1024 * 1024;
const DEFAULT_GRPC_GZIP_COMPRESSION_LEVEL: usize = 2;
const DEFAULT_GRPC_MIN_MESSAGE_SIZE_TO_COMPRESS: usize = 4096;

// Number of rows in each chunk.
const DEFAULT_ENDPOINT_BATCH_ROW_LIMIT: usize = 64;

// If a request has been handled for more than 60 seconds, the client should
// be timeout already, so it can be safely aborted.
const DEFAULT_ENDPOINT_REQUEST_MAX_HANDLE_SECS: u64 = 60;

// Number of rows in each chunk for streaming coprocessor.
const DEFAULT_ENDPOINT_STREAM_BATCH_ROW_LIMIT: usize = 128;


Review comment (refactor):
The magic number 8.0 should be extracted as a named constant to improve code maintainability and make the rationale clearer.
```suggestion
const DEFAULT_GRPC_QUOTA_UNIT: f64 = 8.0; // Default quota unit for gRPC calculations

// Default settings related to gRPC
lazy_static! {
     static ref DEFAULT_GRPC_RAFT_CONN_NUM: usize = {
        // Use DEFAULT_GRPC_QUOTA_UNIT as the default quota unit.
        let base_num = SysQuota::cpu_cores_quota().div(DEFAULT_GRPC_QUOTA_UNIT).floor() as usize;
```

Code after:
use engine_traits::{PerfLevel, perf_level_serde};
use grpcio::{CompressionAlgorithms, ResourceQuota};
use lazy_static::lazy_static;
use online_config::{ConfigChange, ConfigManager, OnlineConfig};
pub use raftstore::store::Config as RaftStoreConfig;
use raftstore::store::config::DEFAULT_SNAP_MAX_BYTES_PER_SEC;
use regex::Regex;
use tikv_util::{
    config::{self, ReadableDuration, ReadableSize, VersionTrack},
    sys::SysQuota,
    worker::Scheduler,
};

use super::{Result, snap::Task as SnapTask};
pub use crate::storage::config::Config as StorageConfig;

pub const DEFAULT_CLUSTER_ID: u64 = 0;
pub const DEFAULT_LISTENING_ADDR: &str = "127.0.0.1:20160";
const DEFAULT_ADVERTISE_LISTENING_ADDR: &str = "";
const DEFAULT_STATUS_ADDR: &str = "127.0.0.1:20180";

fn calculate_cpu_quota_base_num() -> usize {
    // Use 8c as the default quota unit for the limit calculations
    const DEFAULT_CPU_QUOTA_UNIT: f64 = 8.0;

    SysQuota::cpu_cores_quota()
        .div(DEFAULT_CPU_QUOTA_UNIT)
        .floor() as usize
}

fn calculate_grpc_concurrency() -> usize {
    // Multiplier applied to the number of raft connections to determine base
    // concurrency.
    const GRPC_RAFT_CONN_MULTIPLIER: usize = 3;

    // Additional offset added to the calculated concurrency to ensure sufficient
    // threads for handling gRPC requests and system overhead.
    const GRPC_CONCURRENCY_OFFSET: usize = 2;

    DEFAULT_GRPC_RAFT_CONN_NUM.mul(GRPC_RAFT_CONN_MULTIPLIER) + GRPC_CONCURRENCY_OFFSET
}

// Default settings related to gRPC
lazy_static! {
     static ref DEFAULT_GRPC_RAFT_CONN_NUM: usize = {
        // Max count for the default setting of raft connection num is limited
        // to 4.
        calculate_cpu_quota_base_num().clamp(1, 4)
    };

    static ref DEFAULT_GRPC_CONCURRENCY: usize = calculate_grpc_concurrency();