petruchio 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b127618289e59ee4d795a3f625390c510a8e6a46214cee477593990fbcba220
4
+ data.tar.gz: edfa3d726ec9fe1ac779a2f7a28d3709712554784a831080e2f3b1bf2b601877
5
+ SHA512:
6
+ metadata.gz: 63dd6c39379eb507aa6c0e03446f1ceadc5c3186761286997ebf967142a11004e77bbeae6bf9cd17e2ab70c46758d5b09ba033d082947d846718e3c8400904f9
7
+ data.tar.gz: 6d17493b75706d368ff00ab00353cabd72a163aebbe499ad01c9c548b26552e99f967f136368a83caa814e9936d74d856749b72a42acaa8cc6fa1531f0522aa6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-01-30
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in petruchio.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ petruchio (0.1.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.17.0)
10
+ rake (13.0.6)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ minitest (~> 5.0)
17
+ petruchio!
18
+ rake (~> 13.0)
19
+
20
+ BUNDLED WITH
21
+ 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,55 @@
1
+ # Petruchio - a Redis ring buffer in Ruby and Lua
2
+
3
+ > We will have rings, and things, and fine array.
4
+
5
+ **-Petruchio** (The Taming of the Shrew)
6
+
7
+ ## What?
8
+
9
+ Need a Redis ring buffer? Here's a simple one in Ruby and Lua.
10
+
11
+ Supports resizing too if you later need to grow or shrink the buffer.
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add petruchio
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install petruchio
22
+
23
+ ## Usage
24
+
25
+ There's a bunch of reasons you might want to use a ring buffer. For example, you might need one to back a rate limiter. Give it your Redis instance, a name for the buffer, buffer size, and a default value each entry in the buffer is initialized with:
26
+
27
+ ```ruby
28
+ @ring = Petruchio::Ring.new(redis_instance, 'request_limiter', 10, 0.0)
29
+ ```
30
+
31
+ Once you have a ring, pop will remove the oldest things from the buffer:
32
+
33
+ ```ruby
34
+ previous_request_time = @ring.pop.to_f
35
+ ```
36
+
37
+ push will add the newest things:
38
+
39
+ ```ruby
40
+ @ring.push(future_request_time.to_f)
41
+ ```
42
+
43
+ (If you need a lock around pushing and popping stay tuned and [watch our org](https://github.com/sutrolabs).)
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ Feedback
50
+ --------
51
+ [Source code available on Github](https://github.com/sutrolabs/petruchio). Feedback and pull requests are greatly appreciated. Let us know if we can improve this.
52
+
53
+ From
54
+ -----------
55
+ :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
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Petruchio
4
+ VERSION = "0.1.3"
5
+ end
data/lib/petruchio.rb ADDED
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "petruchio/version"
4
+
5
+ module Petruchio
6
+ class Ring
7
+
8
+ INIT_LUA_SCRIPT = <<~LUA
9
+ local key = KEYS[1]
10
+ local size = tonumber(ARGV[1])
11
+ local value = ARGV[2]
12
+
13
+ if redis.call('EXISTS', key) == 0 then
14
+ for i=1,size do
15
+ redis.call('RPUSH', key, value)
16
+ end
17
+ end
18
+ LUA
19
+
20
+ RESIZE_LUA_SCRIPT = <<~LUA
21
+ local key = KEYS[1]
22
+ local size = redis.call("llen", key)
23
+ local new_size = tonumber(ARGV[1])
24
+ local value = ARGV[2]
25
+
26
+ if size > new_size then
27
+ redis.call('LTRIM', key, (size - new_size), (size - 1))
28
+ elseif size < new_size then
29
+ for i = 1, (new_size - size) do
30
+ redis.call('RPUSH', key, value)
31
+ end
32
+ end
33
+
34
+ return redis.call('LLEN', key)
35
+ LUA
36
+
37
+ def initialize(redis, ring_name, size, initial_item)
38
+ @redis = redis
39
+ @ring_name = ring_name
40
+ @initial_item = initial_item
41
+
42
+ @redis.eval(INIT_LUA_SCRIPT, [@ring_name], [size, @initial_item])
43
+ end
44
+
45
+ def resize(new_size)
46
+ # returns the new size
47
+ @redis.eval RESIZE_LUA_SCRIPT, [@ring_name], [new_size, @initial_item]
48
+ end
49
+
50
+ def pop
51
+ @redis.lpop @ring_name
52
+ end
53
+
54
+ def push(item)
55
+ @redis.rpush @ring_name, item
56
+ end
57
+
58
+ def push_pop(item)
59
+ popped, *rest =
60
+ T.let(
61
+ @redis.multi do |transaction|
62
+ transaction.lpop @ring_name
63
+ transaction.rpush @ring_name, item
64
+ end,
65
+ [String, T.untyped]
66
+ )
67
+
68
+ popped
69
+ end
70
+
71
+ def read
72
+ @redis.lrange(@ring_name, 0, -1)
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: petruchio
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-01-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Redis makes a great shared ring buffer
14
+ email:
15
+ - nate@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/petruchio.rb
27
+ - lib/petruchio/version.rb
28
+ homepage: https://github.com/sutrolabs/petruchio
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/sutrolabs/petruchio
33
+ source_code_uri: https://github.com/sutrolabs/petruchio
34
+ changelog_uri: https://github.com/sutrolabs/petruchio/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 ring buffer written in ruby and lua
54
+ test_files: []