From 194a675268a35e3b3dfc3414cd2fd581cff01ad1 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 9 Dec 2020 15:46:05 +0900 Subject: [PATCH] Assume publish response as ping response This option should be needed due to high frequently traffic environment. When flood of messages are reached in this client, keep_alive! response may not reached until response timeout and causes MQTT::ProtocolException with "No Ping Response received for xxx seconds". Signed-off-by: Hiroshi Hatake --- lib/mqtt/client.rb | 7 ++++++- spec/mqtt_client_spec.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/mqtt/client.rb b/lib/mqtt/client.rb index 0f5d467..a844219 100644 --- a/lib/mqtt/client.rb +++ b/lib/mqtt/client.rb @@ -56,6 +56,9 @@ class Client # Last ping response time attr_reader :last_ping_response + # Assume publish response as ping response + attr_accessor :assume_publish_response_as_pingresp + # Timeout between select polls (in seconds) SELECT_TIMEOUT = 0.5 @@ -74,7 +77,8 @@ class Client :will_payload => nil, :will_qos => 0, :will_retain => false, - :ssl => false + :ssl => false, + :assume_publish_response_as_pingresp => false } # Create and connect a new MQTT Client @@ -486,6 +490,7 @@ def handle_packet(packet) if packet.class == MQTT::Packet::Publish # Add to queue @read_queue.push(packet) + @last_ping_response = Time.now if @assume_publish_response_as_pingresp elsif packet.class == MQTT::Packet::Pingresp @last_ping_response = Time.now elsif packet.class == MQTT::Packet::Puback diff --git a/spec/mqtt_client_spec.rb b/spec/mqtt_client_spec.rb index 97229c0..031bd42 100644 --- a/spec/mqtt_client_spec.rb +++ b/spec/mqtt_client_spec.rb @@ -30,6 +30,7 @@ expect(client.port).to eq(1883) expect(client.version).to eq('3.1.1') expect(client.keep_alive).to eq(15) + expect(client.assume_publish_response_as_pingresp).to be_falsey end it "with a single string argument, it should use it has the host" do @@ -81,6 +82,15 @@ expect(client.keep_alive).to eq(65) end + it "with a combination of a host name, port and a hash of settings with different pattern" do + client = MQTT::Client.new('localhost', 1888, :keep_alive => 65, + :assume_publish_response_as_pingresp => true) + expect(client.host).to eq('localhost') + expect(client.port).to eq(1888) + expect(client.keep_alive).to eq(65) + expect(client.assume_publish_response_as_pingresp).to be_truthy + end + it "with a mqtt:// URI containing just a hostname" do client = MQTT::Client.new(URI.parse('mqtt://mqtt.example.com')) expect(client.host).to eq('mqtt.example.com')