potlock 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +32 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +133 -0
- data/CONTRIBUTING.md +23 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +67 -0
- data/LICENSE +21 -0
- data/README.md +125 -0
- data/Rakefile +12 -0
- data/SECURITY.md +9 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/potlock/client.rb +63 -0
- data/lib/potlock/configuration.rb +22 -0
- data/lib/potlock/version.rb +5 -0
- data/lib/potlock.rb +22 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 05a7343257f2f8871d794a5437ad38bc13fb5fbd848d6f1a4c32b34ee2eb4e00
|
4
|
+
data.tar.gz: 4fd86dd486496788613b0b473ac6ddc60a60a5cac66c717bba1f5a83459c1d5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f4e47588c42ac74f5bc6897ec892d547fa8aae004c8344dc78bd1ee577ceaa26a32887bd39b51c612bdb63e57bf6ef1f31547fd1ef884655d0994df6e904f08
|
7
|
+
data.tar.gz: 85b4736745cb33ff99bcd57540da574cfcd7efdb5d678598b312e0272b78844a385dfb8ed81807ea1d46c07e5394228cc8fc68b686ef2750f4784e1c688b5245
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/StringLiteralsInInterpolation:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Layout/LineLength:
|
15
|
+
Max: 120
|
16
|
+
|
17
|
+
Style/TrailingCommaInArrayLiteral:
|
18
|
+
EnforcedStyleForMultiline: comma
|
19
|
+
|
20
|
+
Style/TrailingCommaInHashLiteral:
|
21
|
+
EnforcedStyleForMultiline: comma
|
22
|
+
|
23
|
+
Style/TrailingCommaInArguments:
|
24
|
+
EnforcedStyleForMultiline: comma
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics/BlockLength:
|
30
|
+
Exclude:
|
31
|
+
- "spec/**/*.rb"
|
32
|
+
- "potlock.gemspec"
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.4
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
|
2
|
+
# Contributor Covenant Code of Conduct
|
3
|
+
|
4
|
+
## Our Pledge
|
5
|
+
|
6
|
+
We as members, contributors, and leaders pledge to make participation in our
|
7
|
+
community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
9
|
+
identity and expression, level of experience, education, socio-economic status,
|
10
|
+
nationality, personal appearance, race, caste, color, religion, or sexual identity
|
11
|
+
and orientation.
|
12
|
+
|
13
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
14
|
+
diverse, inclusive, and healthy community.
|
15
|
+
|
16
|
+
## Our Standards
|
17
|
+
|
18
|
+
Examples of behavior that contributes to a positive environment for our
|
19
|
+
community include:
|
20
|
+
|
21
|
+
* Demonstrating empathy and kindness toward other people
|
22
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
23
|
+
* Giving and gracefully accepting constructive feedback
|
24
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
25
|
+
and learning from the experience
|
26
|
+
* Focusing on what is best not just for us as individuals, but for the
|
27
|
+
overall community
|
28
|
+
|
29
|
+
Examples of unacceptable behavior include:
|
30
|
+
|
31
|
+
* The use of sexualized language or imagery, and sexual attention or
|
32
|
+
advances of any kind
|
33
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
34
|
+
* Public or private harassment
|
35
|
+
* Publishing others' private information, such as a physical or email
|
36
|
+
address, without their explicit permission
|
37
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
38
|
+
professional setting
|
39
|
+
|
40
|
+
## Enforcement Responsibilities
|
41
|
+
|
42
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
43
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
44
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
45
|
+
or harmful.
|
46
|
+
|
47
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
48
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
49
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
50
|
+
decisions when appropriate.
|
51
|
+
|
52
|
+
## Scope
|
53
|
+
|
54
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
55
|
+
an individual is officially representing the community in public spaces.
|
56
|
+
Examples of representing our community include using an official e-mail address,
|
57
|
+
posting via an official social media account, or acting as an appointed
|
58
|
+
representative at an online or offline event.
|
59
|
+
|
60
|
+
## Enforcement
|
61
|
+
|
62
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
63
|
+
reported to the community leaders responsible for enforcement at
|
64
|
+
[dev@potloc.com](mailto:dev@potloc.com).
|
65
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
66
|
+
|
67
|
+
All community leaders are obligated to respect the privacy and security of the
|
68
|
+
reporter of any incident.
|
69
|
+
|
70
|
+
## Enforcement Guidelines
|
71
|
+
|
72
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
73
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
74
|
+
|
75
|
+
### 1. Correction
|
76
|
+
|
77
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
78
|
+
unprofessional or unwelcome in the community.
|
79
|
+
|
80
|
+
**Consequence**: A private, written warning from community leaders, providing
|
81
|
+
clarity around the nature of the violation and an explanation of why the
|
82
|
+
behavior was inappropriate. A public apology may be requested.
|
83
|
+
|
84
|
+
### 2. Warning
|
85
|
+
|
86
|
+
**Community Impact**: A violation through a single incident or series
|
87
|
+
of actions.
|
88
|
+
|
89
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
90
|
+
interaction with the people involved, including unsolicited interaction with
|
91
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
92
|
+
includes avoiding interactions in community spaces as well as external channels
|
93
|
+
like social media. Violating these terms may lead to a temporary or
|
94
|
+
permanent ban.
|
95
|
+
|
96
|
+
### 3. Temporary Ban
|
97
|
+
|
98
|
+
**Community Impact**: A serious violation of community standards, including
|
99
|
+
sustained inappropriate behavior.
|
100
|
+
|
101
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
102
|
+
communication with the community for a specified period of time. No public or
|
103
|
+
private interaction with the people involved, including unsolicited interaction
|
104
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
105
|
+
Violating these terms may lead to a permanent ban.
|
106
|
+
|
107
|
+
### 4. Permanent Ban
|
108
|
+
|
109
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
110
|
+
standards, including sustained inappropriate behavior, harassment of an
|
111
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
112
|
+
|
113
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
114
|
+
the community.
|
115
|
+
|
116
|
+
## Attribution
|
117
|
+
|
118
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
119
|
+
version 2.1, available at
|
120
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
121
|
+
|
122
|
+
Community Impact Guidelines were inspired by
|
123
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
124
|
+
|
125
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
126
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
|
127
|
+
at [https://www.contributor-covenant.org/translations][translations].
|
128
|
+
|
129
|
+
[homepage]: https://www.contributor-covenant.org
|
130
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
131
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
132
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
133
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Contributing to Potloc
|
2
|
+
|
3
|
+
🎉 First off, thanks for taking the time to contribute! 🎉
|
4
|
+
|
5
|
+
The following is a set of guidelines for contributing to Potloc and its packages, which are hosted in the [Potloc Organization](https://github.com/POTLOC) on GitHub. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.
|
6
|
+
|
7
|
+
## Code of Conduct
|
8
|
+
|
9
|
+
This project and everyone participating in it is governed by the [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please report unacceptable behavior to [dev@potloc.com](mailto:dev@potloc.com).
|
10
|
+
|
11
|
+
## How Can I Contribute?
|
12
|
+
|
13
|
+
### Report a bug
|
14
|
+
|
15
|
+
TODO: Add description on how to report a bug and add a GitHub template.
|
16
|
+
|
17
|
+
### Submit a suggestion
|
18
|
+
|
19
|
+
TODO: Add description on how to submit a suggestion and add a GitHub template.
|
20
|
+
|
21
|
+
### Open a pull request
|
22
|
+
|
23
|
+
TODO: Add description on how to open a pull request and add a GitHub template.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
potlock (0.1.0)
|
5
|
+
redlock (~> 1.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
mock_redis (0.29.0)
|
13
|
+
ruby2_keywords
|
14
|
+
parallel (1.21.0)
|
15
|
+
parser (3.0.2.0)
|
16
|
+
ast (~> 2.4.1)
|
17
|
+
rainbow (3.0.0)
|
18
|
+
rake (13.0.6)
|
19
|
+
redis (4.4.0)
|
20
|
+
redlock (1.2.1)
|
21
|
+
redis (>= 3.0.0, < 5.0)
|
22
|
+
regexp_parser (2.1.1)
|
23
|
+
rexml (3.2.5)
|
24
|
+
rspec (3.10.0)
|
25
|
+
rspec-core (~> 3.10.0)
|
26
|
+
rspec-expectations (~> 3.10.0)
|
27
|
+
rspec-mocks (~> 3.10.0)
|
28
|
+
rspec-core (3.10.1)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-expectations (3.10.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.10.0)
|
33
|
+
rspec-mocks (3.10.2)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.10.0)
|
36
|
+
rspec-support (3.10.2)
|
37
|
+
rubocop (1.21.0)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 3.0.0.0)
|
40
|
+
rainbow (>= 2.2.2, < 4.0)
|
41
|
+
regexp_parser (>= 1.8, < 3.0)
|
42
|
+
rexml
|
43
|
+
rubocop-ast (>= 1.9.1, < 2.0)
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
46
|
+
rubocop-ast (1.11.0)
|
47
|
+
parser (>= 3.0.1.1)
|
48
|
+
rubocop-rspec (2.4.0)
|
49
|
+
rubocop (~> 1.0)
|
50
|
+
rubocop-ast (>= 1.1.0)
|
51
|
+
ruby-progressbar (1.11.0)
|
52
|
+
ruby2_keywords (0.0.5)
|
53
|
+
unicode-display_width (2.0.0)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
mock_redis (~> 0.2)
|
60
|
+
potlock!
|
61
|
+
rake (~> 13.0)
|
62
|
+
rspec (~> 3.0)
|
63
|
+
rubocop (~> 1.7)
|
64
|
+
rubocop-rspec (~> 2.4)
|
65
|
+
|
66
|
+
BUNDLED WITH
|
67
|
+
2.2.26
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) Potloc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
<a href="LICENSE">
|
2
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="Potlock is released under the MIT license." />
|
3
|
+
</a>
|
4
|
+
|
5
|
+
<a href="CODE_OF_CONDUCT.md">
|
6
|
+
<img src="https://img.shields.io/badge/Contributor%20Covenant-2.1-blue.svg" alt="The code of conduct of Potlock." />
|
7
|
+
</a>
|
8
|
+
|
9
|
+
<a href="CONTRIBUTING.md">
|
10
|
+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs welcome!" />
|
11
|
+
</a>
|
12
|
+
|
13
|
+
<a href="https://github.com/POTLOC/potlock/actions/workflows/rspec.yml">
|
14
|
+
<img src="https://github.com/POTLOC/potlock/actions/workflows/rspec.yml/badge.svg?branch=main" alt="RSpec tests" />
|
15
|
+
</a>
|
16
|
+
|
17
|
+
<a href="https://github.com/POTLOC/potlock/actions/workflows/rubocop.yml">
|
18
|
+
<img src="https://github.com/POTLOC/potlock/actions/workflows/rubocop.yml/badge.svg?branch=main" alt="Rubocop" />
|
19
|
+
</a>
|
20
|
+
|
21
|
+
# Potlock - Distributed Read-Write lock using redis
|
22
|
+
|
23
|
+
Potlock is redis-based concurrent read-write lock distributed across processes.
|
24
|
+
|
25
|
+
Allows only one concurrent reader or writer. And if the lock is taken, any readers or writers who come along will have to wait.
|
26
|
+
|
27
|
+
Highly relying on https://github.com/leandromoreira/redlock-rb.
|
28
|
+
|
29
|
+
_Interested in what we do at [Potloc](https://jobs.lever.co/Potloc)? Come join us! We are hiring 🚀_
|
30
|
+
|
31
|
+
<a href="https://jobs.lever.co/Potloc">
|
32
|
+
<img src="https://www.potloc.com/hubfs/raw_assets/public/Potloc_February2021/images/potloc-logo-5887eaeeeb6a65da7d364097a7edee175590aed00ec877d1c6c64ea955a51a5f.svg" alt="Potloc" width="236" height="54"></a>
|
33
|
+
|
34
|
+
## Compatibility
|
35
|
+
|
36
|
+
Potlock works with Redis versions 2.6 or later.
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem "potlock"
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle install
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install potlock
|
53
|
+
|
54
|
+
## Usage
|
55
|
+
|
56
|
+
### Initialize the lock
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
lock = Potlock::Client.new(key: "redis_key")
|
60
|
+
```
|
61
|
+
There's a list of options you can pass as described [here](#redis-client-configuration).
|
62
|
+
|
63
|
+
### Get a value from Redis
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# Will wait until all locks are freed before getting "redis_key".
|
67
|
+
value = lock.get
|
68
|
+
```
|
69
|
+
|
70
|
+
### Set a value on Redis
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
lock.set do
|
74
|
+
# Execute this block and set the return value in "redis_key"
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
### Fetch a value on Redis
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
value = lock.fetch do
|
82
|
+
# Fetch "redis_key" on Redis or execute this block if not present
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
### Redis client configuration
|
87
|
+
|
88
|
+
By default, `Potlock` will use Redis `redis://localhost:6379/1`. There's options you can use to change this default behaviour:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Potlock.configure do |config|
|
92
|
+
config.redis_host = "localhost"
|
93
|
+
config.redis_port = "6379"
|
94
|
+
config.redis_db = "1"
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
### Potlock configuration
|
99
|
+
|
100
|
+
It's possible to customize the retry logic providing the following options:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
Potlock.configure do |config|
|
104
|
+
config.retry_count = 25 # How many times it'll try to lock a resource
|
105
|
+
config.retry_delay = 200 # How many milliseconds to sleep before try to lock again
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
112
|
+
|
113
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/POTLOC/potlock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/POTLOC/potlock/blob/main/CODE_OF_CONDUCT.md).
|
118
|
+
|
119
|
+
## License
|
120
|
+
|
121
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
122
|
+
|
123
|
+
## Code of Conduct
|
124
|
+
|
125
|
+
Everyone interacting in the Potlock project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/POTLOC/potlock/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/SECURITY.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Security Policy
|
2
|
+
|
3
|
+
## Dependencies
|
4
|
+
|
5
|
+
We always keep up-to-date our dependencies to reduce the risk of having a dependency with a vulnerability.
|
6
|
+
|
7
|
+
## Reporting a Vulnerability
|
8
|
+
|
9
|
+
Contact [dev@potloc.com](mailto:dev@potloc.com) to let us know about your findings and see what are the next steps.
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "potlock"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Potlock
|
4
|
+
class Client
|
5
|
+
attr_accessor :key, :lock_key
|
6
|
+
|
7
|
+
def initialize(key:)
|
8
|
+
@key = key
|
9
|
+
@lock_key = "#{key}_lock"
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch
|
13
|
+
lock! do
|
14
|
+
return redis.get(key) if redis.exists?(key)
|
15
|
+
|
16
|
+
value = yield
|
17
|
+
redis.set(key, value)
|
18
|
+
value
|
19
|
+
end
|
20
|
+
rescue Redlock::LockError => _e
|
21
|
+
raise Potlock::LockError
|
22
|
+
end
|
23
|
+
|
24
|
+
def get
|
25
|
+
lock! { redis.get(key) }
|
26
|
+
rescue Redlock::LockError => _e
|
27
|
+
raise Potlock::LockError
|
28
|
+
end
|
29
|
+
|
30
|
+
def set(&block)
|
31
|
+
value = lock!(&block)
|
32
|
+
redis.set(key, value)
|
33
|
+
rescue Redlock::LockError => _e
|
34
|
+
raise Potlock::LockError
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def lock!(&block)
|
40
|
+
lock_manager.lock!(lock_key, Potlock.configuration.retry_delay, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def lock_manager
|
44
|
+
@lock_manager ||= Redlock::Client.new(
|
45
|
+
[
|
46
|
+
redis,
|
47
|
+
],
|
48
|
+
{
|
49
|
+
retry_count: Potlock.configuration.retry_count,
|
50
|
+
retry_delay: Potlock.configuration.retry_delay,
|
51
|
+
},
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def redis
|
56
|
+
@redis ||= Redis.new(
|
57
|
+
host: Potlock.configuration.redis_host,
|
58
|
+
db: Potlock.configuration.redis_db,
|
59
|
+
port: Potlock.configuration.redis_port,
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Potlock
|
4
|
+
class Configuration
|
5
|
+
# Redis connection information
|
6
|
+
attr_accessor :redis_host, :redis_port, :redis_db
|
7
|
+
|
8
|
+
# How many times it'll try to lock a resource
|
9
|
+
attr_accessor :retry_count
|
10
|
+
|
11
|
+
# How many milliseconds to sleep before try to lock again
|
12
|
+
attr_accessor :retry_delay
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@redis_host = "localhost"
|
16
|
+
@redis_port = "6379"
|
17
|
+
@redis_db = "1"
|
18
|
+
@retry_count = 25
|
19
|
+
@retry_delay = 200
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/potlock.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "redis"
|
4
|
+
require "redlock"
|
5
|
+
|
6
|
+
require_relative "potlock/client"
|
7
|
+
require_relative "potlock/configuration"
|
8
|
+
require_relative "potlock/version"
|
9
|
+
|
10
|
+
module Potlock
|
11
|
+
class LockError < StandardError; end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield(configuration)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: potlock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thibault Couraud
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redlock
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mock_redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.7'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.4'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.4'
|
97
|
+
description: Potlock is redis based concurrent read-write lock distributed across
|
98
|
+
processes.
|
99
|
+
email:
|
100
|
+
- thibault.couraud@potloc.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".ruby-version"
|
108
|
+
- CHANGELOG.md
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- CONTRIBUTING.md
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- LICENSE
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- SECURITY.md
|
117
|
+
- bin/console
|
118
|
+
- bin/setup
|
119
|
+
- lib/potlock.rb
|
120
|
+
- lib/potlock/client.rb
|
121
|
+
- lib/potlock/configuration.rb
|
122
|
+
- lib/potlock/version.rb
|
123
|
+
homepage: https://github.com/POTLOC/potlock
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata:
|
127
|
+
allowed_push_host: https://rubygems.org
|
128
|
+
changelog_uri: https://github.com/POTLOC/potlock
|
129
|
+
homepage_uri: https://github.com/POTLOC/potlock
|
130
|
+
source_code_uri: https://github.com/POTLOC/potlock
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: 2.7.0
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubygems_version: 3.1.6
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Distributed Read-Write lock using redis.
|
150
|
+
test_files: []
|