Skip to content
Browse files

Wrap per connection mutex around integrations with socket to make thr…

…ead safe
  • Loading branch information...
1 parent 722ce00 commit 0d7b71f463f213b1b4cbcf0fdc214882521e88c6 @mloughran mloughran committed Oct 27, 2009
Showing with 8 additions and 0 deletions.
  1. +8 −0 lib/beanstalk-client/connection.rb
View
8 lib/beanstalk-client/connection.rb
@@ -19,6 +19,7 @@
require 'fcntl'
require 'yaml'
require 'set'
+require 'thread'
require 'beanstalk-client/errors'
require 'beanstalk-client/job'
@@ -27,6 +28,7 @@ class Connection
attr_reader :addr
def initialize(addr, default_tube=nil)
+ @mutex = Mutex.new
@waiting = false
@addr = addr
connect
@@ -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
@@ -97,6 +100,8 @@ def reserve(timeout=nil)
end
Job.new(self, *read_job('RESERVED'))
+ ensure
+ @mutex.unlock
end
def delete(id)
@@ -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()

0 comments on commit 0d7b71f

Please sign in to comment.
Something went wrong with that request. Please try again.