Permalink
Browse files

Adds an optional :outbound_limit setting

If :outbound_limit is set, the server closes the connection if we have more
than that value of in-flight data in the sending buffer.
  • Loading branch information...
1 parent 54dafbf commit a0b3db4e1f955e5e9f728cb658215d74bc32a1e2 @zimbatm zimbatm committed Apr 25, 2014
Showing with 38 additions and 0 deletions.
  1. +11 −0 lib/em-websocket/connection.rb
  2. +27 −0 spec/integration/common_spec.rb
@@ -45,6 +45,7 @@ def initialize(options)
@secure_proxy = options[:secure_proxy] || false
@tls_options = options[:tls_options] || {}
@close_timeout = options[:close_timeout]
+ @outbound_limit = options[:outbound_limit] || 0
@handler = nil
@@ -88,6 +89,16 @@ def receive_data(data)
trigger_on_error(e) || raise(e)
end
+ def send_data(data)
+ if @outbound_limit > 0 &&
+ get_outbound_data_size + data.bytesize > @outbound_limit
+ abort(:outbound_limit_reached)
+ return 0
+ end
+
+ super(data)
+ end
+
def unbind
debug [:unbind, :connection]
@@ -108,4 +108,31 @@
end
}
end
+
+ context "outbound limit set" do
+ it "should close the connection if the limit is reached" do
+ em {
+ start_server(:outbound_limit => 150) do |ws|
+ # Increase the message size by one on each loop
+ ws.onmessage{|msg| ws.send(msg + "x") }
+ ws.onclose{|status|
+ status[:code].should == 1006 # Unclean
+ status[:was_clean].should be_false
+ }
+ end
+
+ EM.add_timer(0.1) do
+ ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/')
+ ws.callback { ws.send_msg "hello" }
+ ws.disconnect { done } # Server closed the connection
+ ws.stream { |msg|
+ # minus frame size ? (getting 146 max here)
+ msg.data.size.should <= 150
+ # Return back the message
+ ws.send_msg(msg.data)
+ }
+ end
+ }
+ end
+ end
end

0 comments on commit a0b3db4

Please sign in to comment.