actix_settings/settings/
max_connections.rs

1use std::fmt;
2
3use serde::de;
4
5use crate::{AsResult, Error, Parse};
6
7/// The maximum per-worker number of concurrent connections.
8///
9/// All socket listeners will stop accepting connections when this limit is reached for each worker.
10/// By default max connections is set to a 25k. Takes a string value: Either "default", or an
11/// integer N > 0 e.g. "6".
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub enum MaxConnections {
14    /// The default number of connections. See struct docs.
15    Default,
16
17    /// A specific number of connections.
18    Manual(usize),
19}
20
21impl Parse for MaxConnections {
22    fn parse(string: &str) -> AsResult<Self> {
23        match string {
24            "default" => Ok(MaxConnections::Default),
25            string => match string.parse::<usize>() {
26                Ok(val) => Ok(MaxConnections::Manual(val)),
27                Err(_) => Err(InvalidValue! {
28                    expected: "an integer > 0",
29                    got: string,
30                }),
31            },
32        }
33    }
34}
35
36impl<'de> de::Deserialize<'de> for MaxConnections {
37    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
38    where
39        D: de::Deserializer<'de>,
40    {
41        struct MaxConnectionsVisitor;
42
43        impl de::Visitor<'_> for MaxConnectionsVisitor {
44            type Value = MaxConnections;
45
46            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47                let msg = "Either \"default\" or a string containing an integer > 0";
48                f.write_str(msg)
49            }
50
51            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
52            where
53                E: de::Error,
54            {
55                match MaxConnections::parse(value) {
56                    Ok(max_connections) => Ok(max_connections),
57                    Err(Error::InvalidValue { expected, got, .. }) => Err(
58                        de::Error::invalid_value(de::Unexpected::Str(&got), &expected),
59                    ),
60                    Err(_) => unreachable!(),
61                }
62            }
63        }
64
65        deserializer.deserialize_string(MaxConnectionsVisitor)
66    }
67}