Three ways to close a client socket are unnecessary

Three ways to close a client socket are unnecessary

From 381437f9a395079791fb8b6a2ee79db8e6bc1a36 Mon Sep 17 00:00:00 2001
From: Ben Langfeld <blangfeld@powerhrg.com>
Date: Tue, 27 Nov 2018 18:31:34 -0200
Subject: [PATCH] Three ways to close a client socket are unnecessary


diff --git a/lib/message_bus/client.rb b/lib/message_bus/client.rb
index 29126aa..1ddea39 100644
--- a/lib/message_bus/client.rb
+++ b/lib/message_bus/client.rb
@@ -49,34 +49,6 @@ class MessageBus::Client
     end
   end
 
-  def ensure_closed!
-    return unless in_async?
-
-    if use_chunked
-      write_chunk("[]")
-      if @io
-        @io.write("0\r\n\r\n")
-        @io.close
-        @io = nil
-      end
-      if @async_response
-        @async_response << ("0\r\n\r\n")
-        @async_response.done
-        @async_response = nil
-      end
-    else
-      write_and_close "[]"
-    end
-  rescue
-    # we may have a dead socket, just nil the @io
-    @io = nil
-    @async_response = nil
-  end
-
-  def close
-    ensure_closed!
-  end
-
   def closed?
     !@async_response && !@io
   end
@@ -242,6 +214,30 @@ class MessageBus::Client
     end
   end
 
+  def ensure_closed!
+    return unless in_async?
+
+    if use_chunked
+      write_chunk("[]")
+      if @io
+        @io.write("0\r\n\r\n")
+        @io.close
+        @io = nil
+      end
+      if @async_response
+        @async_response << ("0\r\n\r\n")
+        @async_response.done
+        @async_response = nil
+      end
+    else
+      write_and_close "[]"
+    end
+  rescue
+    # we may have a dead socket, just nil the @io
+    @io = nil
+    @async_response = nil
+  end
+
   def messages_to_json(msgs)
     MessageBus::Rack::Middleware.backlog_to_json(msgs)
   end
diff --git a/lib/message_bus/rack/middleware.rb b/lib/message_bus/rack/middleware.rb
index d493537..ef06b21 100644
--- a/lib/message_bus/rack/middleware.rb
+++ b/lib/message_bus/rack/middleware.rb
@@ -207,8 +207,7 @@ class MessageBus::Rack::Middleware
 
     client.cleanup_timer = @bus.timer.queue(@bus.long_polling_interval.to_f / 1000) {
       begin
-        client.cleanup_timer = nil
-        client.ensure_closed!
+        client.cancel
         @connection_manager.remove_client(client)
       rescue
         @bus.logger.warn "Failed to clean up client properly: #{$!} #{$!.backtrace}"
diff --git a/spec/lib/message_bus/client_spec.rb b/spec/lib/message_bus/client_spec.rb
index 1bf86d9..ca8a30d 100644
--- a/spec/lib/message_bus/client_spec.rb
+++ b/spec/lib/message_bus/client_spec.rb
@@ -77,7 +77,7 @@ describe MessageBus::Client do
       chunk2.first["data"].must_equal "a|\r\n|\r\n|b"
 
       @client << MessageBus::Message.new(3, 3, '/test', 'test3')
-      @client.close
+      @client.cancel
 
       data = r.read

GitHub