surrealdb_core/cnf/
mod.rs

1use std::sync::LazyLock;
2
3/// The characters which are supported in server record IDs.
4pub const ID_CHARS: [char; 36] = [
5	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
6	'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
7];
8
9/// The publicly visible name of the server
10pub const SERVER_NAME: &str = "SurrealDB";
11
12/// Specifies the names of parameters which can not be specified in a query.
13pub const PROTECTED_PARAM_NAMES: &[&str] = &["access", "auth", "token", "session"];
14
15/// Specifies how many concurrent jobs can be buffered in the worker channel.
16#[cfg(not(target_family = "wasm"))]
17pub static MAX_CONCURRENT_TASKS: LazyLock<usize> =
18	lazy_env_parse!("SURREAL_MAX_CONCURRENT_TASKS", usize, 64);
19
20/// Specifies how deep computation recursive call will go before en error is returned.
21pub static MAX_COMPUTATION_DEPTH: LazyLock<u32> =
22	lazy_env_parse!("SURREAL_MAX_COMPUTATION_DEPTH", u32, 120);
23
24/// Specifies how deep the parser will parse nested objects and arrays in a query.
25pub static MAX_OBJECT_PARSING_DEPTH: LazyLock<u32> =
26	lazy_env_parse!("SURREAL_MAX_OBJECT_PARSING_DEPTH", u32, 100);
27
28/// Specifies how deep the parser will parse recursive queries (queries within queries).
29pub static MAX_QUERY_PARSING_DEPTH: LazyLock<u32> =
30	lazy_env_parse!("SURREAL_MAX_QUERY_PARSING_DEPTH", u32, 20);
31
32/// Specifies the number of computed regexes which can be cached in the engine.
33pub static REGEX_CACHE_SIZE: LazyLock<usize> =
34	lazy_env_parse!("SURREAL_REGEX_CACHE_SIZE", usize, 1_000);
35
36/// Specifies the number of items which can be cached within a single transaction.
37pub static TRANSACTION_CACHE_SIZE: LazyLock<usize> =
38	lazy_env_parse!("SURREAL_TRANSACTION_CACHE_SIZE", usize, 10_000);
39
40/// Specifies the number of definitions which can be cached across transactions.
41pub static DATASTORE_CACHE_SIZE: LazyLock<usize> =
42	lazy_env_parse!("SURREAL_DATASTORE_CACHE_SIZE", usize, 1_000);
43
44/// The maximum number of keys that should be scanned at once in general queries.
45pub static NORMAL_FETCH_SIZE: LazyLock<u32> =
46	lazy_env_parse!("SURREAL_NORMAL_FETCH_SIZE", u32, 500);
47
48/// The maximum number of keys that should be scanned at once for export queries.
49pub static EXPORT_BATCH_SIZE: LazyLock<u32> =
50	lazy_env_parse!("SURREAL_EXPORT_BATCH_SIZE", u32, 1000);
51
52/// The maximum number of keys that should be scanned at once for count queries.
53pub static COUNT_BATCH_SIZE: LazyLock<u32> =
54	lazy_env_parse!("SURREAL_COUNT_BATCH_SIZE", u32, 10_000);
55
56/// The maximum size of the priority queue triggering usage of the priority queue for the result collector.
57pub static MAX_ORDER_LIMIT_PRIORITY_QUEUE_SIZE: LazyLock<u32> =
58	lazy_env_parse!("SURREAL_MAX_ORDER_LIMIT_PRIORITY_QUEUE_SIZE", u32, 1000);
59
60/// The maximum number of keys that should be scanned at once per concurrent indexing batch.
61pub static INDEXING_BATCH_SIZE: LazyLock<u32> =
62	lazy_env_parse!("SURREAL_INDEXING_BATCH_SIZE", u32, 250);
63
64/// The maximum stack size of the JavaScript function runtime (defaults to 256 KiB)
65pub static SCRIPTING_MAX_STACK_SIZE: LazyLock<usize> =
66	lazy_env_parse!("SURREAL_SCRIPTING_MAX_STACK_SIZE", usize, 256 * 1024);
67
68/// The maximum memory limit of the JavaScript function runtime (defaults to 2 MiB).
69pub static SCRIPTING_MAX_MEMORY_LIMIT: LazyLock<usize> =
70	lazy_env_parse!("SURREAL_SCRIPTING_MAX_MEMORY_LIMIT", usize, 2 << 20);
71
72/// Forward all signup/signin/authenticate query errors to a client performing authentication. Do not use in production.
73pub static INSECURE_FORWARD_ACCESS_ERRORS: LazyLock<bool> =
74	lazy_env_parse!("SURREAL_INSECURE_FORWARD_ACCESS_ERRORS", bool, false);
75
76#[cfg(storage)]
77/// Specifies the buffer limit for external sorting.
78pub static EXTERNAL_SORTING_BUFFER_LIMIT: LazyLock<usize> =
79	lazy_env_parse!("SURREAL_EXTERNAL_SORTING_BUFFER_LIMIT", usize, 50_000);
80
81/// Used to limit allocation for builtin functions
82pub static GENERATION_ALLOCATION_LIMIT: LazyLock<usize> = LazyLock::new(|| {
83	let n = std::env::var("SURREAL_GENERATION_ALLOCATION_LIMIT")
84		.map(|s| s.parse::<u32>().unwrap_or(20))
85		.unwrap_or(20);
86	2usize.pow(n)
87});
88
89/// Used to limit allocation for builtin functions
90pub static IDIOM_RECURSION_LIMIT: LazyLock<usize> = LazyLock::new(|| {
91	std::env::var("SURREAL_IDIOM_RECURSION_LIMIT")
92		.map(|s| s.parse::<usize>().unwrap_or(256))
93		.unwrap_or(256)
94});
95
96pub static MEMORY_THRESHOLD: LazyLock<usize> = std::sync::LazyLock::new(|| {
97	std::env::var("SURREAL_MEMORY_THRESHOLD")
98		.map(|input| {
99			// Trim the input of any spaces
100			let input = input.trim();
101			// Check if this contains a suffix
102			let split = input.find(|c: char| !c.is_ascii_digit());
103			// Split the value into number and suffix
104			let parts = match split {
105				Some(index) => input.split_at(index),
106				None => (input, ""),
107			};
108			// Parse the number as a positive number
109			let number = parts.0.parse::<usize>().unwrap_or_default();
110			// Parse the supplied suffix as a multiplier
111			let suffix = match parts.1.trim().to_lowercase().as_str() {
112				"" | "b" => 1,
113				"k" | "kb" | "kib" => 1024,
114				"m" | "mb" | "mib" => 1024 * 1024,
115				"g" | "gb" | "gib" => 1024 * 1024 * 1024,
116				_ => 1,
117			};
118			// Multiply the input by the suffix
119			let bytes = number.checked_mul(suffix).unwrap_or_default();
120			// Log the parsed memory threshold
121			debug!("Memory threshold guide: {input} ({bytes} bytes)");
122			// Return the total byte threshold
123			bytes
124		})
125		.unwrap_or(0)
126});