Skip to content

Commit

Permalink
Supports maximum keepalive limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rmqtt committed Aug 22, 2023
1 parent 323829d commit 0c636ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
13 changes: 10 additions & 3 deletions rmqtt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ listener.tcp.external.backlog = 1024
listener.tcp.external.idle_timeout = "20s"
#Whether anonymous login is allowed. Default: true
listener.tcp.external.allow_anonymous = true
#Minimum allowable keepalive value for mqtt connection,
#less than this value will reject the connection, default: 0, unit: seconds
listener.tcp.external.min_keepalive = 0
#A value of zero indicates disabling the keep-alive feature, where the server
#doesn't need to disconnect due to client inactivity, default: true
listener.tcp.external.allow_zero_keepalive = true
#Minimum allowable keepalive value for mqtt connection,
#less than this value will reject the connection(MQTT V3),
#less than this value will set keepalive to this value in CONNACK (MQTT V5),
#default: 0, unit: seconds
listener.tcp.external.min_keepalive = 0
#Maximum allowable keepalive value for mqtt connection,
#greater than this value will reject the connection(MQTT V3),
#greater than this value will set keepalive to this value in CONNACK (MQTT V5),
#default value: 65535, unit: seconds
listener.tcp.external.max_keepalive = 65535
# > 0.5, Keepalive * backoff * 2
listener.tcp.external.keepalive_backoff = 0.75
#Flight window size. The flight window is used to store the unanswered QoS 1 and QoS 2 messages
Expand Down
10 changes: 8 additions & 2 deletions rmqtt/src/broker/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,13 +1187,14 @@ impl Fitter for DefaultFitter {
if self.client.protocol() == MQTT_LEVEL_5 {
if *keep_alive == 0 {
return if self.listen_cfg.allow_zero_keepalive {
//*keep_alive = 10;
Ok(0)
} else {
Err(MqttError::from("Keepalive must be greater than 0"))
};
} else if *keep_alive < self.listen_cfg.min_keepalive {
*keep_alive = self.listen_cfg.min_keepalive;
} else if *keep_alive > self.listen_cfg.max_keepalive {
*keep_alive = self.listen_cfg.max_keepalive;
}
} else if *keep_alive == 0 {
return if self.listen_cfg.allow_zero_keepalive {
Expand All @@ -1203,9 +1204,14 @@ impl Fitter for DefaultFitter {
};
} else if *keep_alive < self.listen_cfg.min_keepalive {
return Err(MqttError::from(format!(
"Keepalive is too small, cannot be less than {}",
"Keepalive is too small and cannot be less than {}",
self.listen_cfg.min_keepalive
)));
} else if *keep_alive > self.listen_cfg.max_keepalive {
return Err(MqttError::from(format!(
"Keepalive is too large and cannot be greater than {}",
self.listen_cfg.max_keepalive
)));
}

if *keep_alive < 6 {
Expand Down
12 changes: 8 additions & 4 deletions rmqtt/src/settings/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ pub struct ListenerInner {
pub idle_timeout: Duration,
#[serde(default = "ListenerInner::allow_anonymous_default")]
pub allow_anonymous: bool,
#[serde(
default = "ListenerInner::min_keepalive_default",
//deserialize_with = "deserialize_duration"
)]
#[serde(default = "ListenerInner::min_keepalive_default")]
pub min_keepalive: u16,
#[serde(default = "ListenerInner::max_keepalive_default")]
pub max_keepalive: u16,
#[serde(default = "ListenerInner::allow_zero_keepalive_default")]
pub allow_zero_keepalive: bool,
#[serde(default = "ListenerInner::keepalive_backoff_default")]
Expand Down Expand Up @@ -251,6 +250,7 @@ impl Default for ListenerInner {
idle_timeout: ListenerInner::idle_timeout_default(),
allow_anonymous: ListenerInner::allow_anonymous_default(),
min_keepalive: ListenerInner::min_keepalive_default(),
max_keepalive: ListenerInner::max_keepalive_default(),
allow_zero_keepalive: ListenerInner::allow_zero_keepalive_default(),
keepalive_backoff: ListenerInner::keepalive_backoff_default(),
max_inflight: ListenerInner::max_inflight_default(),
Expand Down Expand Up @@ -324,6 +324,10 @@ impl ListenerInner {
0
}
#[inline]
fn max_keepalive_default() -> u16 {
u16::MAX
}
#[inline]
fn allow_zero_keepalive_default() -> bool {
true
}
Expand Down

0 comments on commit 0c636ce

Please sign in to comment.