high_school 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +12 -0
- data/lib/high_school/version.rb +5 -0
- data/lib/high_school.rb +49 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c630f397982e498679da0ae302f68513a48608f97d3d13c7b8a155507d72425
|
4
|
+
data.tar.gz: fc807676662df3157dd910f0e708a00bdc6e35f55d00a79d7785ce71775b65bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d7ad06cf433014fccb6e4f865ecaa12dff55aa3cb47ab39d4e2dca1d7388e53d92dfc19f90bc4e5d8c1163bb36cae4d9b03b975cd48010c08a16683a49b500ef
|
7
|
+
data.tar.gz: 8a4fc9c17175576d02e8d106d8f1dae3aad9405ead958a6ceda8beee6df89a841e39244ad578900f45dd61b1bf6ee7ec59ff31612f6e21d91cbae62ed2ebf250
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
high_school (0.1.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.17.0)
|
10
|
+
rake (13.0.6)
|
11
|
+
redis (4.8.0)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
high_school!
|
18
|
+
minitest (~> 5.0)
|
19
|
+
rake (~> 13.0)
|
20
|
+
redis
|
21
|
+
|
22
|
+
BUNDLED WITH
|
23
|
+
2.3.22
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Sutro Labs Inc.
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# High School - a Redis mutex in Ruby and Lua
|
2
|
+
|
3
|
+
I had my backpack stolen in high school while I was distracted playing chess. :(
|
4
|
+
|
5
|
+
Lesson learned. Keep your stuff safe in your locker.
|
6
|
+
|
7
|
+
My loss to high school bullies => your win! :)
|
8
|
+
|
9
|
+
Here's a simple Redis based Locker for your stuff.
|
10
|
+
|
11
|
+
(Implements the [locking strategy](https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-2-distributed-locking/6-2-5-locks-with-timeouts/) from Redis Labs' book about... Redis)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
16
|
+
|
17
|
+
$ bundle add high_school
|
18
|
+
|
19
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
20
|
+
|
21
|
+
$ gem install high_school
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
@locker = HighSchool::Locker.new(my_redis_instance)
|
27
|
+
|
28
|
+
@locker.lock(
|
29
|
+
"the_name_of_my_lock",
|
30
|
+
acquire_timeout: 10.seconds,
|
31
|
+
lock_timeout: 1.hour
|
32
|
+
) do
|
33
|
+
|
34
|
+
# keep this code safe from other threads ~~stealing your backpack~~ trampling on.
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
Feedback
|
43
|
+
--------
|
44
|
+
[Source code available on Github](https://github.com/sutrolabs/high_school). Feedback and pull requests are greatly appreciated. Let us know if we can improve this.
|
45
|
+
|
46
|
+
From
|
47
|
+
-----------
|
48
|
+
:wave: The folks at [Census](http://getcensus.com) originally put this together. Have data? We'll sync your data warehouse with your CRM and the customer success apps critical to your team. Interested in joining the team? **[Come work with us](https://www.getcensus.com/careers)**.
|
data/Rakefile
ADDED
data/lib/high_school.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "high_school/version"
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
module HighSchool
|
7
|
+
# implements the locking strategy from Redis Labs' book about... Redis
|
8
|
+
# https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-2-distributed-locking/6-2-5-locks-with-timeouts/
|
9
|
+
class Locker
|
10
|
+
|
11
|
+
# unlock is a lua script because this call will run atomically vs having to issue 2 calls independently to delete the lock.
|
12
|
+
# Script alo makes sure that the creator of the lock is the one deleting the lock.
|
13
|
+
UNLOCK_LUA_SCRIPT = "if redis.call('get',KEYS[1])==ARGV[1] then redis.call('del',KEYS[1]) end"
|
14
|
+
|
15
|
+
def initialize(redis)
|
16
|
+
@redis = redis
|
17
|
+
end
|
18
|
+
|
19
|
+
class LockNotAcquired < StandardError
|
20
|
+
end
|
21
|
+
|
22
|
+
# A default timeout of 10 seconds is set on the lock. You probably want to set that to something safe for yourself.
|
23
|
+
# But leaving it locked for infinity is probably not what you want.
|
24
|
+
def lock(lock_name, acquire_timeout: nil, lock_timeout: 10, &block)
|
25
|
+
identifier = SecureRandom.uuid
|
26
|
+
|
27
|
+
end_time = Time.now + acquire_timeout if acquire_timeout
|
28
|
+
|
29
|
+
# prefix it to make them easier to find in redis if we need to.
|
30
|
+
lock_name = "lock:" + lock_name
|
31
|
+
|
32
|
+
begin
|
33
|
+
while end_time.nil? || Time.now < end_time
|
34
|
+
# returns false if the lock with this name already exists
|
35
|
+
if @redis.set(lock_name, identifier, nx: true, ex: lock_timeout)
|
36
|
+
return yield
|
37
|
+
else
|
38
|
+
# just to vary up how many simulatenous threads are asking to set the lock
|
39
|
+
sleep(rand(0.10..0.5))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
raise LockNotAcquired.new("Waited #{acquire_timeout} seconds to get lock for #{lock_name}")
|
44
|
+
ensure
|
45
|
+
@redis.eval(UNLOCK_LUA_SCRIPT, [lock_name], [identifier])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: high_school
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- n8
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Use Redis to establish a lock.
|
14
|
+
email:
|
15
|
+
- n8@getcensus.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- CHANGELOG.md
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/high_school.rb
|
27
|
+
- lib/high_school/version.rb
|
28
|
+
homepage: https://github.com/sutrolabs/high_school
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata:
|
32
|
+
homepage_uri: https://github.com/sutrolabs/high_school
|
33
|
+
source_code_uri: https://github.com/sutrolabs/high_school
|
34
|
+
changelog_uri: https://github.com/sutrolabs/high_school/CHANGELOG.md
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.6.0
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.1.6
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: A Redis based Mutex
|
54
|
+
test_files: []
|