Skip to content

Commit 675ac9c

Browse files
author
Bulat Shakirzyanov
committed
[RUBY-90] add tcp nodelay option
1 parent 08e3263 commit 675ac9c

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Features:
44

5+
* [RUBY-90] Add support for disabling nagle algorithm (tcp nodelay), enabled by default.
56
* [RUBY-70] Add support for client-side timestamps, disabled by default.
67
* [RUBY-114] Add support for serial consistency in batch requests.
78

lib/cassandra.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module Cassandra
5959
#
6060
# @option options [Integer] :port (9042) cassandra native protocol port.
6161
#
62+
# @option options [Boolean] :nodelay (false) when set to `true`, disables
63+
# nagle algorithm.
64+
#
6265
# @option options [String] :datacenter (nil) name of current datacenter.
6366
# First datacenter found will be assumed current by default. Note that you
6467
# can skip this option if you specify only hosts from the local datacenter
@@ -435,6 +438,10 @@ def self.cluster_async(options = {})
435438
Util.assert_one_of(CONSISTENCIES, consistency) { ":consistency must be one of #{CONSISTENCIES.inspect}, #{consistency.inspect} given" }
436439
end
437440

441+
if options.has_key?(:nodelay)
442+
options[:nodelay] = !!options[:nodelay]
443+
end
444+
438445
if options.has_key?(:trace)
439446
options[:trace] = !!options[:trace]
440447
end

lib/cassandra/cluster/connector.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ def refresh_status(host)
114114
def do_connect(host)
115115
@reactor.connect(host.ip.to_s, @connection_options.port, {:timeout => @connection_options.connect_timeout, :ssl => @connection_options.ssl}) do |connection|
116116
raise Errors::ClientError, 'Not connected, reactor stopped' unless connection
117+
118+
if @connection_options.nodelay?
119+
connection.to_io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
120+
else
121+
connection.to_io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 0)
122+
end
123+
117124
Protocol::CqlProtocolHandler.new(connection, @reactor, @connection_options.protocol_version, @connection_options.compressor, @connection_options.heartbeat_interval, @connection_options.idle_timeout)
118125
end.flat_map do |connection|
119126
f = request_options(connection)

lib/cassandra/cluster/options.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Options
2525
:schema_refresh_delay, :schema_refresh_timeout
2626
attr_accessor :protocol_version
2727

28-
def initialize(protocol_version, credentials, auth_provider, compressor, port, connect_timeout, ssl, connections_per_local_node, connections_per_remote_node, heartbeat_interval, idle_timeout, synchronize_schema, schema_refresh_delay, schema_refresh_timeout, client_timestamps)
28+
def initialize(protocol_version, credentials, auth_provider, compressor, port, connect_timeout, ssl, connections_per_local_node, connections_per_remote_node, heartbeat_interval, idle_timeout, synchronize_schema, schema_refresh_delay, schema_refresh_timeout, client_timestamps, nodelay)
2929
@protocol_version = protocol_version
3030
@credentials = credentials
3131
@auth_provider = auth_provider
@@ -39,6 +39,7 @@ def initialize(protocol_version, credentials, auth_provider, compressor, port, c
3939
@schema_refresh_delay = schema_refresh_delay
4040
@schema_refresh_timeout = schema_refresh_timeout
4141
@client_timestamps = client_timestamps
42+
@nodelay = nodelay
4243

4344
@connections_per_local_node = connections_per_local_node
4445
@connections_per_remote_node = connections_per_remote_node
@@ -52,6 +53,10 @@ def client_timestamps?
5253
@client_timestamps
5354
end
5455

56+
def nodelay?
57+
@nodelay
58+
end
59+
5560
def compression
5661
@compressor && @compressor.algorithm
5762
end

lib/cassandra/driver.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def self.let(name, &block)
8787
synchronize_schema,
8888
schema_refresh_delay,
8989
schema_refresh_timeout,
90-
client_timestamps
90+
client_timestamps,
91+
nodelay
9192
)
9293
end
9394

@@ -116,6 +117,7 @@ def self.let(name, &block)
116117
let(:thread_pool_size) { 4 }
117118
let(:shuffle_replicas) { true }
118119
let(:client_timestamps) { false }
120+
let(:nodelay) { false }
119121

120122
let(:connections_per_local_node) { 2 }
121123
let(:connections_per_remote_node) { 1 }

spec/support/stub_io_reactor.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
#++
1818

1919
class StubIoReactor
20+
class NullObject
21+
def method_missing(method, *args, &block)
22+
self
23+
end
24+
25+
def nil?
26+
true
27+
end
28+
end
29+
2030
class Connection
2131
attr_reader :host
2232
attr_reader :port
@@ -30,6 +40,7 @@ def initialize(host, port, timeout, ssl)
3040
@ssl = ssl
3141
@blocked = false
3242
@connected = true
43+
@io = NullObject.new
3344

3445
@closed_listeners = []
3546
@incoming = ::Queue.new
@@ -46,6 +57,10 @@ def initialize(host, port, timeout, ssl)
4657
end
4758
end
4859

60+
def to_io
61+
@io
62+
end
63+
4964
def connected?
5065
@connected
5166
end

0 commit comments

Comments
 (0)