Skip to content

Commit

Permalink
don't go around allocating empty strings willy-nilly
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelser committed May 7, 2015
1 parent 323caae commit a23282d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1

- Slight memory leak fix.

## 0.3.0

- Dramatically simplify the interface by forcing clients to specify the key & resources at lock initialization instead of every method call.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
# The resources argument is the number of resources the semaphore will allow to lock (defaulting to one - a mutex)
suo = Suo::Client::Memcached.new("bar_resource", client: some_dalli_client, resources: 2)

Thread.new { suo.lock{ puts "One"; sleep 2 } }
Thread.new { suo.lock { puts "One"; sleep 2 } }
Thread.new { suo.lock { puts "Two"; sleep 2 } }
Thread.new { suo.lock { puts "Three" } }

Expand All @@ -46,7 +46,7 @@ suo = Suo::Client::Memcached.new("protected_key", client: some_dalli_client, acq
# manually locking/unlocking
# the return value from lock without a block is a unique token valid only for the current lock
# which must be unlocked manually
token = suo
token = suo.lock
foo.baz!
suo.unlock(token)

Expand Down Expand Up @@ -77,7 +77,7 @@ end

## History

View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md)
View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md).

## Contributing

Expand Down
8 changes: 6 additions & 2 deletions lib/suo/client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ class Base
resources: 1
}.freeze

BLANK_STR = "".freeze

attr_accessor :client, :key, :resources, :options

include MonitorMixin

def initialize(key, options = {})
fail "Client required" unless options[:client]

@options = DEFAULT_OPTIONS.merge(options)
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
@client = @options[:client]
@resources = @options[:resources].to_i
@key = key

super() # initialize Monitor mixin for thread safety
end

Expand Down Expand Up @@ -124,7 +128,7 @@ def set(newval, cas) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError
end

def initial_set(val = "") # rubocop:disable Lint/UnusedMethodArgument
def initial_set(val = BLANK_STR) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError
end

Expand Down Expand Up @@ -158,7 +162,7 @@ def deserialize_and_clear_locks(val)
end

def deserialize_locks(val)
unpacked = (val.nil? || val == "") ? [] : MessagePack.unpack(val)
unpacked = (val.nil? || val == BLANK_STR) ? [] : MessagePack.unpack(val)

unpacked.map do |time, token|
[Time.at(time), token]
Expand Down
2 changes: 1 addition & 1 deletion lib/suo/client/memcached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def set(newval, cas)
@client.set_cas(@key, newval, cas)
end

def initial_set(val = "")
def initial_set(val = BLANK_STR)
@client.set(@key, val)
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/suo/client/redis.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Suo
module Client
class Redis < Base
OK_STR = "OK".freeze

def initialize(key, options = {})
options[:client] ||= ::Redis.new(options[:connection] || {})
super
Expand All @@ -21,7 +23,7 @@ def set(newval, _)
multi.set(@key, newval)
end

ret && ret[0] == "OK"
ret && ret[0] == OK_STR
end

def synchronize
Expand All @@ -32,7 +34,7 @@ def synchronize
@client.unwatch
end

def initial_set(val = "")
def initial_set(val = BLANK_STR)
@client.set(@key, val)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/suo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Suo
VERSION = "0.3.0"
VERSION = "0.3.1"
end

0 comments on commit a23282d

Please sign in to comment.