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

Code before:
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;

// By default, endpoint memory quota will be set to 12.5% of the available
// memory of TiKV.
//
// TPCC check test shows that:
// * The actual endpoint memory usage is about 3 times to memory quota.
// * Setting memory quota too low can lead to ServerIsBusy errors, which slow
//   down performance.

Review comment (refactor):
The magic numbers 3 and 2 should be extracted as named constants to improve code maintainability and document the relationship between raft connections and concurrency.
```suggestion
        DEFAULT_GRPC_RAFT_CONN_NUM.mul(GRPC_RAFT_CONN_MULTIPLIER) + GRPC_CONCURRENCY_OFFSET
```

Code after:
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();
}
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;