redlock 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0db5130e2d2570b96aa0594e0e950275900120be
4
- data.tar.gz: a153f0211c73fe2fd20a019702db96531c446650
3
+ metadata.gz: e8e7d408cb9639c785875cbc975cd1fbb7166737
4
+ data.tar.gz: 4f067eef13f678f00b85d55edab568471e37b23e
5
5
  SHA512:
6
- metadata.gz: 307248d5f5609f4829a2bc06529740912180aa4767e559b224135ace81f8414d6931ecf72bf3ada258794fa4c8d56509cc86b69b6a11c26547b9bfce6c11615e
7
- data.tar.gz: 1bcaae57325bd013deee9e67cb21df550f589ae380d1f95ea66fbcc9a0a8faf1e04ec9aad6c99902fb3a2e274b880336679907702539765fb66c801c3cbf236c
6
+ metadata.gz: 96f84d612fdd0ee0eb68b3d357d58e4f042077f10b5988d65fb95e01e2bb4b2f2758fb81671a002c29c76a361956e7207d36455ced2e37d25c0d14bbdf78501f
7
+ data.tar.gz: 7283eac7e5222740298f5cb0b9849397fdaeb196ed2f2a218311cd127544da93a2ac7fe7063d010500762c224a4d811ff88e7c48e05b859c697665d305af9ead
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redlock (0.0.4)
4
+ redlock (0.1.0)
5
5
  redis (~> 3, >= 3.0.5)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,9 +1,15 @@
1
+ [![Stories in Ready](https://badge.waffle.io/leandromoreira/redlock-rb.png?label=ready&title=Ready)](https://waffle.io/leandromoreira/redlock-rb)
1
2
  [![Build Status](https://travis-ci.org/leandromoreira/redlock-rb.svg?branch=master)](https://travis-ci.org/leandromoreira/redlock-rb)
2
3
  [![Coverage Status](https://coveralls.io/repos/leandromoreira/redlock-rb/badge.svg?branch=master)](https://coveralls.io/r/leandromoreira/redlock-rb?branch=master)
3
4
  [![Code Climate](https://codeclimate.com/github/leandromoreira/redlock-rb/badges/gpa.svg)](https://codeclimate.com/github/leandromoreira/redlock-rb)
4
5
  [![Dependency Status](https://gemnasium.com/leandromoreira/redlock-rb.svg)](https://gemnasium.com/leandromoreira/redlock-rb)
5
6
  [![Gem Version](https://badge.fury.io/rb/redlock.svg)](http://badge.fury.io/rb/redlock)
6
7
  [![security](https://hakiri.io/github/leandromoreira/redlock-rb/master.svg)](https://hakiri.io/github/leandromoreira/redlock-rb/master)
8
+ [![Inline docs](http://inch-ci.org/github/leandromoreira/redlock-rb.svg?branch=master)](http://inch-ci.org/github/leandromoreira/redlock-rb)
9
+ [![Join the chat at https://gitter.im/leandromoreira/redlock-rb](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/leandromoreira/redlock-rb?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
10
+
11
+ [![Codeship](https://codeship.com/projects/901ff180-c1ad-0132-1a88-3eb2295b72b3/status?branch=master)](https://codeship.com/projects/901ff180-c1ad-0132-1a88-3eb2295b72b3/status?branch=master)
12
+
7
13
 
8
14
  # Redlock - A ruby distributed lock using redis.
9
15
 
@@ -54,6 +60,13 @@ Or install it yourself as:
54
60
  p second_try_lock_info
55
61
  ```
56
62
 
63
+ Redlock works seamlessly with [redis sentinel](http://redis.io/topics/sentinel), which is supported in redis 3.2+. It also allows clients to set any other arbitrary options on the Redis connection, e.g. password, driver, and more.
64
+
65
+ ```ruby
66
+ servers = [ 'redis://localhost:6379', Redis.new(:url => 'redis://someotherhost:6379') ]
67
+ redlock = Redlock::Client.new(servers)
68
+ ```
69
+
57
70
  There's also a block version that automatically unlocks the lock:
58
71
 
59
72
  ```ruby
@@ -11,15 +11,21 @@ module Redlock
11
11
 
12
12
  # Create a distributed lock manager implementing redlock algorithm.
13
13
  # Params:
14
- # +server_urls+:: the array of redis hosts.
14
+ # +servers+:: The array of redis connection URLs or Redis connection instances. Or a mix of both.
15
15
  # +options+:: You can override the default value for `retry_count` and `retry_delay`.
16
16
  # * `retry_count` being how many times it'll try to lock a resource (default: 3)
17
17
  # * `retry_delay` being how many ms to sleep before try to lock again (default: 200)
18
18
  # * `redis_timeout` being how the Redis timeout will be set in seconds (default: 0.1)
19
- def initialize(server_urls = DEFAULT_REDIS_URLS, options = {})
19
+ def initialize(servers = DEFAULT_REDIS_URLS, options = {})
20
20
  redis_timeout = options[:redis_timeout] || DEFAULT_REDIS_TIMEOUT
21
- @servers = server_urls.map { |url| RedisInstance.new(url, redis_timeout) }
22
- @quorum = server_urls.length / 2 + 1
21
+ @servers = servers.map do |server|
22
+ if server.is_a?(String)
23
+ RedisInstance.new(url: server, timeout: redis_timeout)
24
+ else
25
+ RedisInstance.new(server)
26
+ end
27
+ end
28
+ @quorum = servers.length / 2 + 1
23
29
  @retry_count = options[:retry_count] || DEFAULT_RETRY_COUNT
24
30
  @retry_delay = options[:retry_delay] || DEFAULT_RETRY_DELAY
25
31
  end
@@ -62,16 +68,24 @@ module Redlock
62
68
  end
63
69
  eos
64
70
 
65
- def initialize(url, timeout)
66
- @redis = Redis.new(url: url, timeout: timeout)
71
+ def initialize(connection)
72
+ if connection.respond_to?(:client)
73
+ @redis = connection
74
+ else
75
+ @redis = Redis.new(connection)
76
+ end
77
+ end
78
+
79
+ def redis
80
+ @redis
67
81
  end
68
82
 
69
83
  def lock(resource, val, ttl)
70
- @redis.client.call([:set, resource, val, :nx, :px, ttl])
84
+ redis.client.call([:set, resource, val, 'NX', 'PX', ttl])
71
85
  end
72
86
 
73
87
  def unlock(resource, val)
74
- @redis.client.call([:eval, UNLOCK_SCRIPT, 1, resource, val])
88
+ redis.client.call([:eval, UNLOCK_SCRIPT, 1, resource, val])
75
89
  rescue
76
90
  # Nothing to do, unlocking is just a best-effort attempt.
77
91
  end
@@ -1,3 +1,3 @@
1
1
  module Redlock
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -7,6 +7,18 @@ RSpec.describe Redlock::Client do
7
7
  let(:resource_key) { SecureRandom.hex(3) }
8
8
  let(:ttl) { 1000 }
9
9
 
10
+ describe 'initialize' do
11
+ it 'accepts both redis URLs and Redis objects' do
12
+ servers = [ 'redis://localhost:6379', Redis.new(url: 'redis://someotherhost:6379') ]
13
+ redlock = Redlock::Client.new(servers)
14
+
15
+ redlock_servers = redlock.instance_variable_get(:@servers)
16
+
17
+ expect(redlock_servers.one? { |s| s.redis.client.host == 'localhost' })
18
+ expect(redlock_servers.one? { |s| s.redis.client.port == 'someotherhost' })
19
+ end
20
+ end
21
+
10
22
  describe 'lock' do
11
23
  context 'when lock is available' do
12
24
  after(:each) { lock_manager.unlock(@lock_info) if @lock_info }
metadata CHANGED
@@ -1,89 +1,89 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Moreira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-25 00:00:00.000000000 Z
11
+ date: 2015-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3'
20
- - - ">="
20
+ - - '>='
21
21
  - !ruby/object:Gem::Version
22
22
  version: 3.0.5
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3'
30
- - - ">="
30
+ - - '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.0.5
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ~>
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1.7'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - "~>"
44
+ - - ~>
45
45
  - !ruby/object:Gem::Version
46
46
  version: '1.7'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: coveralls
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
58
+ - - '>='
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - "~>"
65
+ - - ~>
66
66
  - !ruby/object:Gem::Version
67
67
  version: '10.0'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - "~>"
72
+ - - ~>
73
73
  - !ruby/object:Gem::Version
74
74
  version: '10.0'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rspec
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - "~>"
79
+ - - ~>
80
80
  - !ruby/object:Gem::Version
81
81
  version: '3.1'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - "~>"
86
+ - - ~>
87
87
  - !ruby/object:Gem::Version
88
88
  version: '3.1'
89
89
  description: Distributed lock using Redis written in Ruby. Highly inspired by https://github.com/antirez/redlock-rb.
@@ -93,9 +93,9 @@ executables: []
93
93
  extensions: []
94
94
  extra_rdoc_files: []
95
95
  files:
96
- - ".gitignore"
97
- - ".rspec"
98
- - ".travis.yml"
96
+ - .gitignore
97
+ - .rspec
98
+ - .travis.yml
99
99
  - CONTRIBUTORS
100
100
  - Gemfile
101
101
  - Gemfile.lock
@@ -118,17 +118,17 @@ require_paths:
118
118
  - lib
119
119
  required_ruby_version: !ruby/object:Gem::Requirement
120
120
  requirements:
121
- - - ">="
121
+ - - '>='
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0'
124
124
  required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  requirements:
126
- - - ">="
126
+ - - '>='
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
130
  rubyforge_project:
131
- rubygems_version: 2.2.2
131
+ rubygems_version: 2.4.6
132
132
  signing_key:
133
133
  specification_version: 4
134
134
  summary: Distributed lock using Redis written in Ruby.