letterbox 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ecf23c81ad7a2ac948f6edae443c34bc3cf7308
4
+ data.tar.gz: 6b9a922d23fb715ff34f729ac3738f1521f9df8f
5
+ SHA512:
6
+ metadata.gz: c61dd0d80333f89d8485b24b491dfcdf0f78dd0933d0420c27b70cfcf3d69184bef5a4350ae96d1c30acaf55c77f260508957bb846cc6effff36f13653eae3aa
7
+ data.tar.gz: 2496e1beee3debd2e3a9b0ac8399e91cde000ff95e1d778e87c3eade851ba8ac9ac427e5f4e3a8a3774e568d461e5ba98defa5cc320a8b1fce44db5507850247
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Craig Little
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # Letterbox
2
+
3
+ A short-lived actor framework using Celluloid.
4
+
5
+ ## Installation
6
+
7
+ With Bundler:
8
+ ```ruby
9
+ gem 'letterbox'
10
+ ```
11
+
12
+ Otherwise:
13
+ ```ruby
14
+ gem install letterbox
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ When instantiating a `Letterbox::Exchange` you must provide a class
20
+ that will do the work. The work class' constructor should accept one
21
+ argument (the "payload") and implement a `perform` method that performs
22
+ the desired work.
23
+
24
+ You can send work to a particular address by specifying a payload and
25
+ the address that should do the work. All work sent to a particular
26
+ address will be processed serially.
27
+
28
+ Work spread across multiple mailboxes will be processed concurrently.
29
+
30
+ Empty addresses are cleaned up automatically once there is no more work
31
+ to be done.
32
+
33
+ ```ruby
34
+ module MyWork
35
+
36
+ attr_reader :payload
37
+
38
+ def initialize(payload)
39
+ @payload = payload
40
+ end
41
+
42
+ def perform
43
+ sleep rand
44
+ puts "Working on: #{payload}"
45
+ end
46
+
47
+ end
48
+
49
+ e = Letterbox::Exchange.new(MyWork)
50
+
51
+ # spread 100 units of work across 10 addresses
52
+ 100.times { |i| e.dispatch("payload #{i}", "a#{i % 10}") }
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/letterbox/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../lib/letterbox/version', __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'letterbox'
5
+ spec.version = Letterbox::VERSION
6
+ spec.authors = ['Craig Little']
7
+ spec.email = ['craiglttl@gmail.com']
8
+ spec.description = %q{Short-lived actor framework using Celluloid}
9
+ spec.summary = spec.description
10
+ spec.homepage = 'https://github.com/craiglittle/letterbox'
11
+ spec.license = 'MIT'
12
+
13
+ spec.require_paths = ['lib']
14
+ spec.files = `git ls-files`.split($\)
15
+ spec.test_files = spec.files.grep(%r{^spec/})
16
+
17
+ spec.add_runtime_dependency 'celluloid', '~> 0.15'
18
+
19
+ spec.add_development_dependency 'rake'
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'letterbox/exchange'
2
+ require 'letterbox/version'
@@ -0,0 +1,22 @@
1
+ require 'celluloid'
2
+
3
+ module Letterbox
4
+ class Address
5
+
6
+ include Celluloid
7
+
8
+ attr_reader :exchange, :unit_of_work
9
+
10
+ def initialize(exchange, unit_of_work)
11
+ @exchange = exchange
12
+ @unit_of_work = unit_of_work
13
+ end
14
+
15
+ def process(payload)
16
+ unit_of_work.new(payload).perform
17
+
18
+ exchange.async.completed_by(name)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ require 'celluloid'
2
+
3
+ module Letterbox
4
+ class Directory
5
+
6
+ attr_reader :registry, :number_of_units
7
+
8
+ def initialize
9
+ @registry = Celluloid::Registry.new
10
+ @number_of_units = Hash.new(0)
11
+ end
12
+
13
+ def send_to(address, &new_address)
14
+ registry.set(address, new_address.call) unless registry.get(address)
15
+
16
+ track_unit(address)
17
+
18
+ registry.get(address)
19
+ end
20
+
21
+ def completed_by(address)
22
+ untrack_unit(address)
23
+
24
+ if empty?(address)
25
+ registry.get(address).terminate
26
+ registry.delete(address)
27
+
28
+ untrack_address(address)
29
+ end
30
+ end
31
+
32
+ def number_outstanding
33
+ number_of_units.values.reduce(:+)
34
+ end
35
+
36
+ private
37
+
38
+ def track_unit(address)
39
+ number_of_units[address.to_sym] += 1
40
+ end
41
+
42
+ def untrack_unit(address)
43
+ number_of_units[address.to_sym] -= 1
44
+ end
45
+
46
+ def untrack_address(address)
47
+ number_of_units.delete(address.to_sym)
48
+ end
49
+
50
+ def empty?(address)
51
+ number_of_units[address.to_sym] == 0
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ require 'celluloid'
2
+
3
+ require 'letterbox/directory'
4
+ require 'letterbox/address'
5
+
6
+ module Letterbox
7
+ class Exchange
8
+
9
+ extend Forwardable
10
+
11
+ include Celluloid
12
+
13
+ attr_reader :unit_of_work, :directory
14
+
15
+ def initialize(unit_of_work)
16
+ @unit_of_work = unit_of_work
17
+ @directory = Directory.new
18
+ end
19
+
20
+ def dispatch(payload, address)
21
+ directory.send_to(address) {
22
+ Address.new_link(Actor.current, unit_of_work)
23
+ }.async.process(payload)
24
+ end
25
+
26
+ delegate [:completed_by, :number_outstanding] => :directory
27
+
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Letterbox
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letterbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Craig Little
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: celluloid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Short-lived actor framework using Celluloid
42
+ email:
43
+ - craiglttl@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - letterbox.gemspec
54
+ - lib/letterbox.rb
55
+ - lib/letterbox/address.rb
56
+ - lib/letterbox/directory.rb
57
+ - lib/letterbox/exchange.rb
58
+ - lib/letterbox/version.rb
59
+ homepage: https://github.com/craiglittle/letterbox
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.14
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Short-lived actor framework using Celluloid
83
+ test_files: []