Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Mutex.new to constructor #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/carrot/amqp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(opts = {})
@ssl_verify = opts[:ssl_verify] || OpenSSL::SSL::VERIFY_PEER

@multithread = opts[:multithread]
@mutex = @multithread ? Mutex.new : nil
start_session
end

Expand Down Expand Up @@ -125,7 +126,7 @@ def socket

begin
# Attempt to connect.
mutex.lock if multithread?
@mutex&.lock
@socket = timeout(CONNECT_TIMEOUT) do
TCPSocket.new(host, port)
end
Expand All @@ -141,7 +142,7 @@ def socket
msg = e.message << " - #{@host}:#{@port}"
raise ServerDown, e.message
ensure
mutex.unlock if multithread?
@mutex&.unlock
end

@socket
Expand Down Expand Up @@ -194,16 +195,12 @@ def multithread?

def close_socket(reason=nil)
# Close the socket. The server is not considered dead.
mutex.lock if multithread?
@mutex&.lock
@socket.close if @socket and not @socket.closed?
@socket = nil
@status = "NOT CONNECTED"
ensure
mutex.unlock if multithread?
end

def mutex
@mutex ||= Mutex.new
@mutex&.unlock
end

def log(*args)
Expand Down