resource_warden 0.1.0

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: 6156e0930be5bea9d2114a52a5d23fe29cb466b36b2733f58861af2968a80ef5
4
+ data.tar.gz: 3124064d103b827665d952445cdbdf53898d02a7a4a6c36fda1c8831a81e3ef8
5
+ SHA512:
6
+ metadata.gz: 794209e8d0d9740b9f4dbdabef32176779fcfc0ca43ed62a6225eacbd4e2a7c19af919089338536d16e59e494768415a1d16907688a16e767c5133cf8569c135
7
+ data.tar.gz: 7ade6cfad2a4eeb07eca26f5239577d3e3cc9204ed7f4c60f4505d5359ae5a7bc32079a7f328239abef28f2c35de6d8067e6248f166ad9eb2466d13971d33dbe
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.8
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,62 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Standards
4
+
5
+ Examples of behavior that contributes to creating a positive environment
6
+ include:
7
+
8
+ * Being respectful of differing viewpoints and experiences
9
+ * Gracefully accepting constructive criticism
10
+ * Focusing on what is best for the community
11
+ * Showing empathy towards other community members
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery and unwelcome sexual attention or
16
+ advances
17
+ * Trolling, insulting/derogatory comments, and personal or political attacks
18
+ * Public or private harassment
19
+ * Publishing others' private information, such as a physical or electronic
20
+ address, without explicit permission
21
+
22
+ ## Our Responsibilities
23
+
24
+ Project maintainers are responsible for clarifying the standards of acceptable
25
+ behavior and are expected to take appropriate and fair corrective action in
26
+ response to any instances of unacceptable behavior.
27
+
28
+ Project maintainers have the right and responsibility to remove, edit, or
29
+ reject comments, commits, code, wiki edits, issues, and other contributions
30
+ that are not aligned to this Code of Conduct, or to ban temporarily or
31
+ permanently any contributor for other behaviors that they deem inappropriate,
32
+ threatening, offensive, or harmful.
33
+
34
+ ## Scope
35
+
36
+ This Code of Conduct applies both within project spaces and in public spaces
37
+ when an individual is representing the project or its community. Examples of
38
+ representing a project or community include using an official project e-mail
39
+ address, posting via an official social media account, or acting as an appointed
40
+ representative at an online or offline event. Representation of a project may be
41
+ further defined and clarified by project maintainers.
42
+
43
+ ## Enforcement
44
+
45
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
46
+ reported by contacting the project team at aaron.deibele@capco.com. All
47
+ complaints will be reviewed and investigated and will result in a response that
48
+ is deemed necessary and appropriate to the circumstances. The project team is
49
+ obligated to maintain confidentiality with regard to the reporter of an incident.
50
+ Further details of specific enforcement policies may be posted separately.
51
+
52
+ Project maintainers who do not follow or enforce the Code of Conduct in good
53
+ faith may face temporary or permanent repercussions as determined by other
54
+ members of the project's leadership.
55
+
56
+ ## Attribution
57
+
58
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
59
+ available at [http://contributor-covenant.org/version/1/4][version]
60
+
61
+ [homepage]: http://contributor-covenant.org
62
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ResourceWarden.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Aaron Deibele
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,43 @@
1
+ # ResourceWarden
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ResourceWarden`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ResourceWarden'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ResourceWarden
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ResourceWarden. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the ResourceWarden project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ResourceWarden/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ResourceWarden/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "resource_warden"
7
+ spec.homepage = "https://github.com/deibele1/ResourceWarden"
8
+ spec.version = ResourceWarden::VERSION
9
+ spec.authors = ["Aaron Deibele"]
10
+ spec.email = ["deibele1@gmail.com"]
11
+
12
+ spec.summary = %q{Ensures exclusive access to multiple resource across many threads.}
13
+ spec.description = %q{Allows exclusive resources to be created which can be requested in lists. Requests for the resources guarantee a resource will be granted provided the jobs sent to the warden all finish at some point}
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 2.3"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ResourceWarden"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ class Key
2
+ class ReuseException < Exception; end
3
+ def initialize(&block)
4
+ @proc = Proc.new(&block)
5
+ end
6
+
7
+ def use
8
+ throw ReuseException.new("A key can only be used once") unless @proc.is_a?(Proc)
9
+
10
+ @proc.call
11
+ @proc = nil
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require_relative './Key'
2
+ # wraps a resource to ensure only a keyholder can execute code
3
+ module ResourceWarden
4
+ class ResourceCell
5
+ class JailbreakException < Exception; end
6
+ class Unlock; end
7
+ private_constant :Unlock
8
+
9
+ def initialize(resource)
10
+ @mutex = Mutex.new
11
+ @registration = Mutex.new
12
+ @heir = nil
13
+ @resource = resource
14
+ @registry = []
15
+ end
16
+
17
+ # changes the heir to the caller and builds a connector proc to wait on the current thread
18
+ def get_key
19
+ heir = nil
20
+ @registration.synchronize do
21
+ heir = @heir
22
+ @heir = Thread.current
23
+ end
24
+ Key.new do
25
+ heir&.join
26
+ chown(Thread.current)
27
+ Unlock.new
28
+ end
29
+ end
30
+
31
+ private def owner
32
+ @owner
33
+ end
34
+
35
+ private def chown(thread)
36
+ @mutex.synchronize do
37
+ @owner = thread
38
+ end
39
+ end
40
+
41
+ private def method_missing(method_name, *args)
42
+ return @resource.send(method_name, *args) if Thread.current == owner
43
+ throw JailbreakException.new("A thread must first use a resource key before accessing a resource")
44
+ end
45
+
46
+ def respond_to_missing?(method_name, include_private = false)
47
+ super || Thread.current == @owner && @resource.respond_to?(method_name, include_private)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ # Resource Exclusion class
2
+ # A block is passed which will then be encapsulated in a thread immediately joined by the current thread.
3
+ # This allows requests for multiple resources and guarantees a lock on all resources will be granted
4
+ # as soon as all prior virtual threads with a claim on a resource terminate.
5
+ # The warden calls the cell which generates a key which will join the prior resource claimant and change ownership
6
+ # of the resource when used. Attempting to use a resource without ownership will result in an exception.
7
+ # Auto joining in this way ensures no resource starvation, no deadlocks, no live-locks and true concurrency.
8
+ # Tasks should be kept small when using this model and all tasks must eventually terminate.
9
+ # This model can solve a generalized dining philosophers problem with any resource exclusion structure.
10
+ require_relative('./ResourceCell')
11
+ module ResourceWarden
12
+ class Warden
13
+ @mutex = Mutex.new
14
+ @registration = Mutex.new
15
+
16
+ def initialize(*resources)
17
+ @cell_block = resources || []
18
+ @block_mutex = Mutex.new
19
+ end
20
+
21
+ def add(resource)
22
+ @block_mutex.synchronize { @cell_block << resource }
23
+ end
24
+
25
+ def resources
26
+ @cell_block
27
+ end
28
+
29
+ def synchronize(&block)
30
+ @block_mutex.synchronize { Warden.synchronize(*@cell_block, &block) }
31
+ end
32
+
33
+ # global synchronization
34
+ def self.synchronize(*resources, &block)
35
+ keys = []
36
+ # creating a virtual thread to guarantee the owning thread is joinable
37
+ Thread.new do
38
+ @registration.synchronize { keys = resources.map(&:get_key) }
39
+ keys.each(&:use)
40
+ block.call
41
+ end.join
42
+ end
43
+
44
+
45
+ # creates a resource
46
+ def self.create(object = nil)
47
+ @registration.synchronize do
48
+ item = block_given? ? yield : object
49
+ ResourceCell.new(item)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # Transparent wrapper class ignores requests from any caller other than the current owner
2
+ module ResourceWarden
3
+ class ResourceCell
4
+ # Creates the cell that will only allow a owning thread to call any method of the resource
5
+ def initialize: (untyped) -> void
6
+ # creates a blocking resource key that can only be used once to change the owner of the resource to the calling thread
7
+ def get_key: -> void
8
+
9
+ # returns the current resource owner
10
+ private def owner: -> Thread
11
+ private def chown: (Thread) -> void
12
+
13
+ # resource guard method
14
+ private def method_missing: (Symbol | String, *untyped) -> untyped
15
+ private def respond_to_missing?: (Symbol | String, *untyped) -> bool
16
+
17
+ class Unlock
18
+ def initialize: () -> void
19
+ end
20
+ end
21
+
22
+ class Warden
23
+ # warden instance handles a specific resource group
24
+ def intialize: (*ResourceCell) -> void
25
+ # synchronize a block with all resource under the wardens management
26
+ def synchronize: () { () -> BasicObject } -> void
27
+ # puts a resource under this wardens management
28
+ def add: (ResourceCell) -> void
29
+ # returns the resources managed by this warden
30
+ def resources: () -> Array[ResourceCell]
31
+ # synchronizes a block with a resource list
32
+ def self.synchronize:(*Symbol) { () -> BasicObject } -> void
33
+ # creates a resourceCell either from an object or a cell
34
+ def self.create:(untyped) -> ResourceCell | () { () -> Object } -> ResourceCell
35
+ end
36
+
37
+ class Key
38
+ # creates a key this should only be created by a resource cell
39
+ def initialize: () {() -> ResourceCell::Unlock } -> void
40
+ # changes the owner of the creating cell to the current thread after waiting for prior threads to complete
41
+ def use: () -> void
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module ResourceWarden
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "./ResourceWarden/version"
2
+ require_relative "./ResourceWarden/Warden"
3
+
4
+ module ResourceWarden; end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resource_warden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Deibele
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Allows exclusive resources to be created which can be requested in lists.
56
+ Requests for the resources guarantee a resource will be granted provided the jobs
57
+ sent to the warden all finish at some point
58
+ email:
59
+ - deibele1@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - ResourceWarden.gemspec
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/ResourceWarden.rb
76
+ - lib/ResourceWarden/Key.rb
77
+ - lib/ResourceWarden/ResourceCell.rb
78
+ - lib/ResourceWarden/Warden.rb
79
+ - lib/ResourceWarden/types.rbs
80
+ - lib/ResourceWarden/version.rb
81
+ homepage: https://github.com/deibele1/ResourceWarden
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Ensures exclusive access to multiple resource across many threads.
104
+ test_files: []