Class: ELock
- Inherits:
-
Object
- Object
- ELock
- Defined in:
- lib/elock-client.rb
Overview
A client for elock.
Instance Method Summary collapse
-
#close ⇒ Object
Close this connction.
-
#initialize(host, port = 11400) ⇒ ELock
constructor
Construct a new ELock client pointed at the given server.
-
#lock(name, timeout = nil) ⇒ Object
Acquire a lock.
-
#stats ⇒ Object
Get server stats.
-
#unlock(name) ⇒ Object
Release a lock from elock.
-
#unlock_all ⇒ Object
Unlock all locks obtained by this client.
-
#with_lock(name, timeout = nil) ⇒ Object
Run a block while holding the named lock.
Constructor Details
#initialize(host, port = 11400) ⇒ ELock
Construct a new ELock client pointed at the given server.
12 13 14 |
# File 'lib/elock-client.rb', line 12 def initialize(host, port=11400) connect host, port end |
Instance Method Details
#close ⇒ Object
Close this connction
61 62 63 |
# File 'lib/elock-client.rb', line 61 def close @socket = @socket.close end |
#lock(name, timeout = nil) ⇒ Object
Acquire a lock. This method should return very quickly except in the case where a timeout is requested and the lock is unavailable.
18 19 20 |
# File 'lib/elock-client.rb', line 18 def lock(name, timeout=nil) do_cmd(timeout.nil? ? "lock #{name}" : "lock #{name} #{timeout}") end |
#stats ⇒ Object
Get server stats
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/elock-client.rb', line 33 def stats @socket.write("stats\r\n") line = @socket.gets("\r\n") rv={} if line.to_i == 200 line = @socket.gets("\r\n").strip while line != 'END' s, name, val=line.split rv[name]=val line = @socket.gets("\r\n").strip end end rv end |
#unlock(name) ⇒ Object
Release a lock from elock.
23 24 25 |
# File 'lib/elock-client.rb', line 23 def unlock(name) do_cmd("unlock #{name}") end |
#unlock_all ⇒ Object
Unlock all locks obtained by this client.
28 29 30 |
# File 'lib/elock-client.rb', line 28 def unlock_all do_cmd("unlock_all") end |
#with_lock(name, timeout = nil) ⇒ Object
Run a block while holding the named lock. raises Locked if the lock could not be acquired.
50 51 52 53 54 55 56 57 58 |
# File 'lib/elock-client.rb', line 50 def with_lock(name, timeout=nil) if lock(name, timeout).first == 200 yield else raise Locked end ensure unlock name end |