Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Wrap per connection mutex around integrations with socket to make thr…
Browse files Browse the repository at this point in the history
…ead safe
  • Loading branch information
mloughran committed Oct 27, 2009
1 parent 722ce00 commit 0d7b71f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/beanstalk-client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'fcntl'
require 'yaml'
require 'set'
require 'thread'
require 'beanstalk-client/errors'
require 'beanstalk-client/job'

Expand All @@ -27,6 +28,7 @@ class Connection
attr_reader :addr

def initialize(addr, default_tube=nil)
@mutex = Mutex.new
@waiting = false
@addr = addr
connect
Expand Down Expand Up @@ -80,6 +82,7 @@ def peek_buried()

def reserve(timeout=nil)
raise WaitingForJobError if @waiting
@mutex.lock
if timeout.nil?
@socket.write("reserve\r\n")
else
Expand All @@ -97,6 +100,8 @@ def reserve(timeout=nil)
end

Job.new(self, *read_job('RESERVED'))
ensure
@mutex.unlock
end

def delete(id)
Expand Down Expand Up @@ -173,10 +178,13 @@ def list_tubes_watched(cached=false)

def interact(cmd, rfmt)
raise WaitingForJobError if @waiting
@mutex.lock
@socket.write(cmd)
return read_yaml('OK') if rfmt == :yaml
return found_job if rfmt == :job
check_resp(*rfmt)
ensure
@mutex.unlock
end

def get_resp()
Expand Down

0 comments on commit 0d7b71f

Please sign in to comment.