1use std::{net::Ipv4Addr, time::Duration};
2
3use jsonrpsee::core::TEN_MB_SIZE_BYTES;
4use serde::{Deserialize, Serialize};
5use serde_with::serde_as;
6
7use crate::{Port, PortDefaults};
8
9pub enum GrpcService {}
10impl PortDefaults for GrpcService {
11 const DEFAULT: u16 = 9089;
12 const ENV_VAR: Option<&str> = Some("AGGLAYER_GRPC_PORT");
13}
14
15pub enum ReadRpcService {}
16impl PortDefaults for ReadRpcService {
17 const DEFAULT: u16 = 9090;
18 const ENV_VAR: Option<&str> = Some("AGGLAYER_READRPC_PORT");
19}
20
21pub enum AdminService {}
22impl PortDefaults for AdminService {
23 const DEFAULT: u16 = 9091;
24 const ENV_VAR: Option<&str> = Some("AGGLAYER_ADMIN_PORT");
25}
26
27#[serde_as]
29#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
30#[serde(rename_all = "kebab-case")]
31pub struct RpcConfig {
32 #[serde(default)]
36 pub grpc_port: Port<GrpcService>,
37
38 #[serde(default)]
42 pub readrpc_port: Port<ReadRpcService>,
43
44 #[serde(default)]
48 pub admin_port: Port<AdminService>,
49
50 #[serde(default = "default_host")]
51 pub host: Ipv4Addr,
52
53 #[serde(
55 skip_serializing_if = "same_as_default_body_size",
56 default = "default_body_size"
57 )]
58 pub max_request_body_size: u32,
59
60 #[serde(
62 skip_serializing_if = "same_as_default_body_size",
63 default = "default_body_size"
64 )]
65 pub max_response_body_size: u32,
66
67 #[serde(
69 skip_serializing_if = "same_as_default_max_connections",
70 default = "default_max_connections"
71 )]
72 pub max_connections: u32,
73
74 #[serde(skip_serializing_if = "crate::is_default")]
77 pub batch_request_limit: Option<u32>,
78
79 #[serde(skip)]
81 #[serde_as(as = "Option<crate::with::HumanDuration>")]
82 pub ping_interval: Option<Duration>,
83
84 #[serde_as(as = "crate::with::HumanDuration")]
86 #[serde(default = "default_request_timeout")]
87 pub request_timeout: Duration,
88}
89
90impl Default for RpcConfig {
91 fn default() -> Self {
92 Self {
93 grpc_port: Default::default(),
94 readrpc_port: Default::default(),
95 admin_port: Default::default(),
96 host: default_host(),
97 max_request_body_size: default_body_size(),
98 max_response_body_size: default_body_size(),
99 max_connections: default_max_connections(),
100 batch_request_limit: None,
101 ping_interval: None,
102 request_timeout: default_request_timeout(),
103 }
104 }
105}
106
107fn default_max_connections() -> u32 {
109 100
110}
111
112fn default_body_size() -> u32 {
114 TEN_MB_SIZE_BYTES
115}
116
117const fn default_host() -> Ipv4Addr {
119 Ipv4Addr::new(0, 0, 0, 0)
120}
121
122const fn default_request_timeout() -> Duration {
124 Duration::from_secs(180)
125}
126
127fn same_as_default_body_size(size: &u32) -> bool {
128 *size == default_body_size()
129}
130
131fn same_as_default_max_connections(max_connections: &u32) -> bool {
132 *max_connections == default_max_connections()
133}