batch_any 1.3.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92c5a5e652092edbb5b409b38d80314daf72ffb7
4
+ data.tar.gz: 4bb6bc663d2eba26aa229117bbef93fb38f52a38
5
+ SHA512:
6
+ metadata.gz: 62c787644cc09f815333b57f275a75266b15c58cd23c84d96cce88fcd1722c0a8f4b5373f04295c2eeb9d13425b12b1d93a76d4ccd851cf8262f0c0305410502
7
+ data.tar.gz: ef3b09a0713c9b032e4c9088474cff6985976e162b5d0be405c460c5c18113dca2cc54ab7e83e5634c95ce678c64d1bca26804f49f246e84aac38ac338ec643b
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in batch_any.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dmitry Bochkarev
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.
@@ -0,0 +1,84 @@
1
+ # BatchAny
2
+
3
+ Allows you to use the batching service both for grouping requests into one and a single access to api. It makes it easy to integrate the batching service into the current logic without huge refactoring.
4
+ Internally it uses [Fibers](http://ruby-doc.org/core-2.4.1/Fiber.html) to pause current control flow and resumes after batches are formed.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'batch_any'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install batch_any
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ STORAGE = [:a, :b].freeze
26
+
27
+ class Service < BatchAny::Service
28
+ @@fetch_count = 0
29
+
30
+ def self.fetch_count
31
+ @@fetch_count
32
+ end
33
+
34
+ def can_serve?(item)
35
+ item.class == Request
36
+ end
37
+
38
+ def fetch
39
+ @@fetch_count += 1
40
+ items.each { |item| item.result { STORAGE.fetch(item.index) } }
41
+ end
42
+ end
43
+
44
+ class Request < BatchAny::Item
45
+ attr_reader :index
46
+
47
+ def initialize(index)
48
+ @index = index
49
+ end
50
+
51
+ def service_class
52
+ Service
53
+ end
54
+ end
55
+
56
+ a = nil
57
+ b = nil
58
+
59
+ batching_manager = BatchAny::Manager.new
60
+ batching_manager.add_computation { a = Request.new(0).fetch }
61
+ batching_manager.add_computation { b = Request.new(1).fetch }
62
+ batching_manager.run
63
+
64
+ puts a
65
+ # => a
66
+ puts b
67
+ # => b
68
+ puts Service.fetch_count
69
+ # => 1 # not 2
70
+ ```
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryBochkarev/batch_any.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'batch_any/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "batch_any"
8
+ spec.version = BatchAny::VERSION
9
+ spec.authors = ["Dmitry Bochkarev"]
10
+ spec.email = ["dimabochkarev@gmail.com"]
11
+
12
+ spec.summary = %q{Practical application of ruby fibers. Help you to batching requests.}
13
+ spec.description = %q{Allows you to use the batching service both for grouping requests into one and a single \
14
+ access to api. It makes it easy to integrate the batching service into the current logic without huge refactoring.}
15
+ spec.homepage = "https://github.com/DmitryBochkarev/batch_any"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ 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", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "batch_any"
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__)
@@ -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,8 @@
1
+ require 'fiber'
2
+ require 'batch_any/version'
3
+ require 'batch_any/manager'
4
+ require 'batch_any/service'
5
+ require 'batch_any/item'
6
+
7
+ module BatchAny
8
+ end
@@ -0,0 +1,26 @@
1
+ module BatchAny
2
+ class Item
3
+ attr_accessor :value, :exception
4
+
5
+ def service_class
6
+ raise "Not implemented: #{self.class}#service_class -> Class, required by BatchAny::Item"
7
+ end
8
+
9
+ def fetch
10
+ batching_manager = Thread.current[:batch_any_manager]
11
+ if batching_manager
12
+ batching_manager.enqueue_item(self)
13
+ else
14
+ service_class.new(self).fetch
15
+ end
16
+ raise @exception if @exception
17
+ @value
18
+ end
19
+
20
+ def result
21
+ @value = yield
22
+ rescue => e
23
+ @exception = e
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,88 @@
1
+ module BatchAny
2
+ class Manager
3
+ class FiberError
4
+ attr_reader :fiber, :exception
5
+
6
+ def initialize(fiber, exception)
7
+ @fiber = fiber
8
+ @exception = exception
9
+ end
10
+ end
11
+
12
+ attr_reader :exceptions
13
+
14
+ def initialize
15
+ @computations = []
16
+ @awaiting_services = {}
17
+ end
18
+
19
+ def add_computation(&block)
20
+ fiber = Fiber.new do
21
+ Thread.current[:batch_any_manager] = self
22
+ block.call
23
+ end
24
+ @computations << fiber
25
+ fiber
26
+ end
27
+
28
+ def run
29
+ @exceptions = []
30
+ while @computations.any?
31
+ @computations.each do |computation|
32
+ begin
33
+ computation.resume
34
+ rescue => e
35
+ @exceptions << FiberError.new(computation, e)
36
+ end
37
+ end
38
+ linear_keep_if!(@computations, &:alive?)
39
+ @awaiting_services.values.each do |services|
40
+ services.each do |service|
41
+ begin
42
+ service.fetch
43
+ rescue => e
44
+ service.items.each { |item| item.exception = e }
45
+ end
46
+ end
47
+ end
48
+ @awaiting_services.clear
49
+ end
50
+ @exceptions
51
+ end
52
+
53
+ def enqueue_item(item)
54
+ service_class = item.service_class
55
+ @awaiting_services[service_class] ||= []
56
+ awaiting_services = @awaiting_services[service_class]
57
+ service = awaiting_services.find { |service| service.can_serve?(item) }
58
+ if service
59
+ service.items << item
60
+ else
61
+ awaiting_services << item.service_class.new(item)
62
+ end
63
+ Fiber.yield
64
+ end
65
+
66
+ private
67
+
68
+ # https://bugs.ruby-lang.org/issues/10714
69
+ # https://github.com/ruby/ruby/commit/5ec029d1ea52224a365a11987379c3e9de74b47a
70
+ def linear_keep_if!(arr)
71
+ i = 0
72
+ j = 0
73
+ while i < arr.length
74
+ v = arr[i]
75
+ if yield v
76
+ arr[j] = v
77
+ j += 1
78
+ end
79
+ i += 1
80
+ end
81
+ i != j ? arr : nil
82
+ ensure
83
+ if i != j
84
+ arr[j, i-j] = []
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,18 @@
1
+ module BatchAny
2
+ class Service
3
+ attr_reader :item_class, :items
4
+
5
+ def initialize(item)
6
+ @item_class = item.class
7
+ @items = [item]
8
+ end
9
+
10
+ def can_serve?(item)
11
+ raise "Not implemented: #{self.class}#can_serve?(item) -> truthy, required by BatchAny::Service"
12
+ end
13
+
14
+ def fetch
15
+ raise "Not implemented: #{self.class}#fetch, required by BatchAny::Service"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module BatchAny
2
+ VERSION = "1.3.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batch_any
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Bochkarev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-17 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: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: |-
56
+ Allows you to use the batching service both for grouping requests into one and a single \
57
+ access to api. It makes it easy to integrate the batching service into the current logic without huge refactoring.
58
+ email:
59
+ - dimabochkarev@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - batch_any.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/batch_any.rb
75
+ - lib/batch_any/item.rb
76
+ - lib/batch_any/manager.rb
77
+ - lib/batch_any/service.rb
78
+ - lib/batch_any/version.rb
79
+ homepage: https://github.com/DmitryBochkarev/batch_any
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.11
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Practical application of ruby fibers. Help you to batching requests.
103
+ test_files: []