Skip to content

Commit fa10508

Browse files
committed
Remove thread-local map and socket map (complexity creep).
1 parent 177fad3 commit fa10508

11 files changed

+141
-297
lines changed

lib/mongo/connection.rb

-66
Original file line numberDiff line numberDiff line change
@@ -481,68 +481,6 @@ def max_bson_size
481481
@max_bson_size
482482
end
483483

484-
def get_local_reader
485-
self.connections ||= {}
486-
if !connected? && self.connections[self.object_id]
487-
self.connections[self.object_id]
488-
else
489-
self.connections[self.object_id] = {}
490-
end
491-
self.connections[self.object_id][:reader] ||= checkout_reader
492-
end
493-
494-
def get_local_writer
495-
self.connections ||= {}
496-
if !connected? && self.connections[self.object_id]
497-
self.connections[self.object_id]
498-
else
499-
self.connections[self.object_id] = {}
500-
end
501-
self.connections[self.object_id][:writer] ||= checkout_writer
502-
end
503-
504-
# Allow the current thread’s connection to return to the pool.
505-
#
506-
# Calling this method allows the socket that has been reserved
507-
# for this thread to be returned to the pool. Other threads will
508-
# then be able to re-use that socket. If your application uses many
509-
# threads, or has long-running threads that infrequently perform MongoDB
510-
# operations, then judicious use of this method can lead to performance gains.
511-
# Care should be taken, however, to make sure that end_request is not called
512-
# in the middle of a sequence of operations in which ordering is important. This
513-
# could lead to unexpected results.
514-
#
515-
# One important case is when a thread is dying permanently. It is best to call
516-
# end_request when you know a thread is finished, as otherwise its socket will
517-
# not be reclaimed.
518-
def end_request
519-
if socket = self.connections[self.object_id][:reader]
520-
checkin(socket)
521-
end
522-
523-
if socket = self.connections[self.object_id][:writer]
524-
checkin(socket)
525-
end
526-
end
527-
528-
# Used to close, check in, or refresh sockets held
529-
# in thread-local variables.
530-
def local_socket_done(socket)
531-
if self.connections[self.object_id][:reader] == socket
532-
if self.read_pool.sockets_low?
533-
checkin(socket)
534-
self.connections[self.object_id][:reader] = nil
535-
end
536-
end
537-
538-
if self.connections[self.object_id][:writer] == socket
539-
if self.primary_pool && self.primary_pool.sockets_low?
540-
checkin(socket)
541-
self.connections[self.object_id][:writer] = nil
542-
end
543-
end
544-
end
545-
546484
# Checkout a socket for reading (i.e., a secondary node).
547485
# Note: this is overridden in ReplSetConnection.
548486
def checkout_reader
@@ -560,16 +498,12 @@ def checkout_writer
560498
# Checkin a socket used for reading.
561499
# Note: this is overridden in ReplSetConnection.
562500
def checkin_reader(socket)
563-
warn "Connection#checkin_writer is not deprecated and will be removed " +
564-
"in driver v2.0. Use Connection#checkin instead."
565501
checkin(socket)
566502
end
567503

568504
# Checkin a socket used for writing.
569505
# Note: this is overridden in ReplSetConnection.
570506
def checkin_writer(socket)
571-
warn "Connection#checkin_writer is not deprecated and will be removed " +
572-
"in driver v2.0. Use Connection#checkin instead."
573507
checkin(socket)
574508
end
575509

lib/mongo/cursor.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,10 @@ def send_get_more
520520
def checkout_socket_from_connection
521521
@checkin_connection = true
522522
if @command || @read_preference == :primary
523-
@connection.get_local_writer
523+
@connection.checkout_writer
524524
else
525525
@read_pool = @connection.read_pool
526-
@connection.get_local_reader
526+
@connection.checkout_reader
527527
end
528528
end
529529

@@ -556,7 +556,11 @@ def checkin_socket(sock)
556556
@read_pool.checkin(sock)
557557
@checkin_read_pool = false
558558
elsif @checkin_connection
559-
@connection.local_socket_done(sock)
559+
if @command || @read_preference == :primary
560+
@connection.checkin_writer(sock)
561+
else
562+
@connection.checkin_reader(sock)
563+
end
560564
@checkin_connection = false
561565
end
562566
end
@@ -566,7 +570,11 @@ def force_checkin_socket(sock)
566570
@read_pool.checkin(sock)
567571
@checkin_read_pool = false
568572
else
569-
@connection.checkin(sock)
573+
if @command || @read_preference == :primary
574+
@connection.checkin_writer(sock)
575+
else
576+
@connection.checkin_reader(sock)
577+
end
570578
@checkin_connection = false
571579
end
572580
end

lib/mongo/networking.rb

+40-34
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ def send_message(operation, message, opts={})
2525

2626
connection = opts.fetch(:connection, :writer)
2727

28-
begin
29-
add_message_headers(message, operation)
30-
packed_message = message.to_s
28+
add_message_headers(message, operation)
29+
packed_message = message.to_s
3130

31+
if connection == :writer
32+
sock = checkout_writer
33+
else
34+
sock = checkout_reader
35+
end
36+
37+
begin
38+
send_message_on_socket(packed_message, sock)
39+
ensure
3240
if connection == :writer
33-
sock = get_local_writer
41+
checkin_writer(sock)
3442
else
35-
sock = get_local_reader
43+
checkin_reader(sock)
3644
end
37-
38-
send_message_on_socket(packed_message, sock)
39-
local_socket_done(sock)
40-
rescue ConnectionFailure, OperationFailure, OperationTimeout => ex
41-
checkin(sock)
42-
raise ex
4345
end
4446
end
4547

@@ -64,13 +66,13 @@ def send_message_with_safe_check(operation, message, db_name, log_message=nil, l
6466
last_error_id = add_message_headers(last_error_message, Mongo::Constants::OP_QUERY)
6567

6668
packed_message = message.append!(last_error_message).to_s
69+
sock = checkout_writer
6770
begin
68-
sock = get_local_writer
6971
send_message_on_socket(packed_message, sock)
7072
docs, num_received, cursor_id = receive(sock, last_error_id)
71-
local_socket_done(sock)
73+
checkin_writer(sock)
7274
rescue ConnectionFailure, OperationFailure, OperationTimeout => ex
73-
checkin(sock)
75+
checkin_writer(sock)
7476
raise ex
7577
end
7678

@@ -101,30 +103,34 @@ def receive_message(operation, message, log_message=nil, socket=nil, command=fal
101103
read=:primary, exhaust=false)
102104
request_id = add_message_headers(message, operation)
103105
packed_message = message.to_s
104-
begin
105-
if socket
106-
sock = socket
107-
should_checkin = false
106+
if socket
107+
sock = socket
108+
should_checkin = false
109+
else
110+
if command || read == :primary
111+
sock = checkout_writer
112+
elsif read == :secondary
113+
sock = checkout_reader
108114
else
109-
if command
110-
sock = get_local_writer
111-
elsif read == :primary
112-
sock = get_local_writer
113-
elsif read == :secondary
114-
sock = get_local_reader
115-
else
116-
sock = checkout_tagged(read)
117-
end
118-
should_checkin = true
115+
sock = checkout_tagged(read)
119116
end
117+
should_checkin = true
118+
end
120119

121-
result = ''
120+
result = ''
121+
begin
122122
send_message_on_socket(packed_message, sock)
123123
result = receive(sock, request_id, exhaust)
124-
local_socket_done(sock) if should_checkin
125-
rescue ConnectionFailure, OperationFailure, OperationTimeout => ex
126-
checkin(sock) if should_checkin
127-
raise ex
124+
ensure
125+
if should_checkin
126+
if command || read == :primary
127+
checkin_writer(sock)
128+
elsif read == :secondary
129+
checkin_reader(sock)
130+
else
131+
# TODO: sock = checkout_tagged(read)
132+
end
133+
end
128134
end
129135
result
130136
end
@@ -281,7 +287,7 @@ def send_message_on_socket(packed_message, socket)
281287
total_bytes_sent
282288
rescue => ex
283289
close
284-
raise ConnectionFailure, "Operation failed with the following exception: #{ex}"
290+
raise ConnectionFailure, "Operation failed with the following exception: #{ex}:#{ex.message}"
285291
end
286292
end
287293

0 commit comments

Comments
 (0)