Skip to content
Browse files

Merge pull request #2 from tobert/master

Relocate ZMQ::LINGER setting to ReadSocket
  • Loading branch information...
2 parents 4697d1e + 31a18b2 commit 9cf80729402b34f55acea1a5a0b8155c44411491 @tarcieri tarcieri committed Apr 1, 2012
Showing with 21 additions and 5 deletions.
  1. +21 −5 lib/celluloid/zmq/sockets.rb
View
26 lib/celluloid/zmq/sockets.rb
@@ -4,6 +4,7 @@ class Socket
# Create a new socket
def initialize(type)
@socket = Celluloid::ZMQ.context.socket ::ZMQ.const_get(type.to_s.upcase)
+ @options = { ::ZMQ::LINGER => 0 }
end
# Connect to the given 0MQ address
@@ -15,14 +16,18 @@ def connect(addr)
true
end
- # Bind to the given 0MQ address
- # Address should be in the form: tcp://1.2.3.4:5678/
- def bind(addr)
- unless ::ZMQ::Util.resultcode_ok? @socket.setsockopt(::ZMQ::LINGER, 0)
+ def setsockopt(option, value)
+ @options[option] = value
+
+ unless ::ZMQ::Util.resultcode_ok? @socket.setsockopt(option, value)
@socket.close
- raise IOError, "couldn't set ZMQ::LINGER: #{::ZMQ::Util.error_string}"
+ raise IOError, "couldn't set ZMQ option #{option}: #{::ZMQ::Util.error_string}"
end
+ end
+ # Bind to the given 0MQ address
+ # Address should be in the form: tcp://1.2.3.4:5678/
+ def bind(addr)
unless ::ZMQ::Util.resultcode_ok? @socket.bind(addr)
raise IOError, "couldn't bind to #{addr}: #{::ZMQ::Util.error_string}"
end
@@ -48,6 +53,17 @@ def evented?
# Readable 0MQ sockets have a read method
module ReadableSocket
+ # always set LINGER on readable sockets
+ def bind(addr)
+ setsockopt(::ZMQ::LINGER, @options[::ZMQ::LINGER])
+ super(addr)
+ end
+
+ def connect(addr)
+ setsockopt(::ZMQ::LINGER, @options[::ZMQ::LINGER])
+ super(addr)
+ end
+
# Read a message from the socket
def read(buffer = '')
Celluloid.current_actor.wait_readable(@socket) if evented?

0 comments on commit 9cf8072

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