ruby_gothreads 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
+ SHA1:
3
+ metadata.gz: 8c8fbb30f3494d3bb74b50e70fa3918e494b3ced
4
+ data.tar.gz: 81909909e24a5b18fc9f43a4b4124a5f8c293dc3
5
+ SHA512:
6
+ metadata.gz: d77e05748a0fae44aab663b0181c50dad934a40f72e224bc52e21a95c369472a2b0bf78545b1cae3f5c5757a32c752c6aab9a7c8e4252480da92928c108502ea
7
+ data.tar.gz: fd34fd50afa636a905a9ccdc5434eb64d901bbd23f3c391e8253a8cfc65da1ea24916d1e9f81c48bbe3ac116e9c7af5bbcccb4e97d27d451a8035eebc84a7019
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at zhuochun@hotmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_go.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Zhuochun
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,27 @@
1
+ # RubyGo
2
+
3
+ Syntax sugars (`go`, `chan`, `select`) around Ruby threads and queues, emulating Golang Goroutines.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_go'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby_go
20
+
21
+ ## Usage
22
+
23
+ Refer to examples in `example/`.
24
+
25
+ ## License
26
+
27
+ MIT 2016 @ [Zhuochun](https://github.com/zhuochun).
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby_go"
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
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,39 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ lib = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "ruby_go"
8
+
9
+ include RubyGo
10
+
11
+ nums_chan = chan(8)
12
+
13
+ wg = []
14
+
15
+ wg << go do
16
+ x, y = 1, 1
17
+
18
+ 4.times do
19
+ sleep 0.2
20
+ nums_chan << "1 - #{y}"
21
+ x, y = y, x + y
22
+ end
23
+ end
24
+
25
+ wg << go do
26
+ x, y = 1, 1
27
+
28
+ 4.times do
29
+ sleep 0.8
30
+ nums_chan << "2 - #{y}"
31
+ x, y = y, x + y
32
+ end
33
+ end
34
+
35
+ 8.times do
36
+ p nums_chan.pop
37
+ end
38
+
39
+ wg.map(&:join)
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ lib = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "logger"
8
+ require "ruby_go"
9
+
10
+ include RubyGo
11
+
12
+ LOGGER = Logger.new(STDOUT)
13
+ LOGGER.level = Logger::INFO
14
+
15
+ ExitException = Class.new(StandardError)
16
+
17
+ Task = Struct.new(:name, :result)
18
+
19
+ NUM_WORKERS = 5
20
+ NUM_TASKS = 25
21
+
22
+ task_chan = chan(NUM_WORKERS)
23
+ done_chan = chan(NUM_WORKERS)
24
+ exit_chan = chan(NUM_WORKERS)
25
+
26
+ def worker(worker_id, task_chan, done_chan, exit_chan)
27
+ -> do
28
+ LOGGER.info "Worker #{worker_id} Started"
29
+
30
+ begin
31
+ loop do
32
+ select({
33
+ task_chan => ->(task) do
34
+ LOGGER.info "[#{worker_id}] Start #{task.name}"
35
+ task.result = rand()
36
+ sleep task.result
37
+ done_chan << task
38
+ LOGGER.info "[#{worker_id}] Finished #{task.name}"
39
+ end,
40
+ exit_chan => ->(_) { raise ExitException },
41
+ })
42
+ end
43
+ rescue ExitException
44
+ end
45
+
46
+ LOGGER.info "Worker #{worker_id} Ended"
47
+ end
48
+ end
49
+
50
+ wg = NUM_WORKERS.times.map do |i|
51
+ go(&worker(i, task_chan, done_chan, exit_chan))
52
+ end
53
+
54
+ wg << go do
55
+ LOGGER.info "Producer Started"
56
+ NUM_TASKS.times { |i| task_chan << Task.new("Task #{i}") }
57
+ LOGGER.info "Producer Ended"
58
+ end
59
+
60
+ wg << go do
61
+ LOGGER.info "Consumer Started"
62
+ NUM_TASKS.times do
63
+ task = done_chan.pop
64
+ LOGGER.info "Consumer Read #{task.name}, Result #{task.result}"
65
+ end
66
+ LOGGER.info "Consumer Ended"
67
+
68
+ LOGGER.info "Consumer Signal Exit Start"
69
+ NUM_WORKERS.times { exit_chan << true }
70
+ LOGGER.info "Consumer Signal End Start"
71
+ end
72
+
73
+ wg.map(&:join)
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ lib = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "ruby_go"
8
+
9
+ include RubyGo
10
+
11
+ c = chan()
12
+ timeout = RubyGo::Timeout.new(5)
13
+
14
+ p Time.now()
15
+
16
+ go do
17
+ sleep 20
18
+ c << true
19
+ end
20
+
21
+ select({
22
+ c => ->(result) { p "Result: #{result}" },
23
+ timeout => ->(_) { p "Timeout" }
24
+ })
25
+
26
+ p Time.now()
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ lib = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "logger"
8
+ require "ruby_go"
9
+
10
+ include RubyGo
11
+
12
+ LOGGER = Logger.new(STDOUT)
13
+ LOGGER.level = Logger::INFO
14
+
15
+ class Task
16
+ include RubyGo
17
+
18
+ def initialize(name)
19
+ @name = name
20
+ end
21
+
22
+ def do
23
+ sleep_sec = rand() * 30
24
+ timeout_sec = rand() * 30
25
+
26
+ c = chan()
27
+ t = RubyGo::Timeout.new(timeout_sec)
28
+
29
+ go do
30
+ sleep sleep_sec
31
+ c << true
32
+ end
33
+
34
+ select({
35
+ c => ->(result) do
36
+ correct = sleep_sec <= timeout_sec
37
+ LOGGER.info "[#{@name}][#{correct}] RESULT: #{sleep_sec}, Timeout: #{timeout_sec}"
38
+ end,
39
+ t => ->(_) do
40
+ correct = timeout_sec <= sleep_sec
41
+ LOGGER.info "[#{@name}][#{correct}] timeout: #{timeout_sec}, Sleep: #{sleep_sec}"
42
+ end
43
+ })
44
+ end
45
+ end
46
+
47
+ LOGGER.info "Starting"
48
+
49
+ wg = 100.times.map do |i|
50
+ go { Task.new("Task #{i}").do }
51
+ end
52
+
53
+ wg.map(&:join)
54
+
55
+ LOGGER.info "Finished"
data/lib/ruby_go.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "thread"
2
+
3
+ require "ruby_go/version"
4
+ require "ruby_go/timeout"
5
+
6
+ module RubyGo
7
+ # go starts a thread
8
+ def go(&block)
9
+ Thread.new(&block)
10
+ end
11
+
12
+ # chan creates a size channel with default size 1
13
+ def chan(len = 1)
14
+ SizedQueue.new(len)
15
+ end
16
+
17
+ # select receives a { chan => action } map, and blocked til a chan to execute
18
+ def select(chans)
19
+ chan_method = ->(_) {}
20
+ value = nil
21
+
22
+ selected = false
23
+ loop do
24
+ sleep 0.01 # to prevent busy loop
25
+
26
+ chans.each do |c, cm|
27
+ begin
28
+ value = c.pop(true)
29
+ chan_method = cm
30
+ selected = true
31
+ break
32
+ rescue ThreadError
33
+ end
34
+ end
35
+
36
+ break if selected
37
+ end
38
+
39
+ chan_method.call(value)
40
+ end
41
+ end
@@ -0,0 +1,24 @@
1
+ class RubyGo::Timeout
2
+ attr_reader :timeout_at
3
+
4
+ def initialize(seconds)
5
+ @mutex = Mutex.new
6
+ @timeout = false
7
+ @timeout_at = Time.now() + seconds
8
+ end
9
+
10
+ def pop(non_block = true)
11
+ return true if @timeout
12
+
13
+ @mutex.synchronize do
14
+ diff = @timeout_at - Time.now()
15
+ @timeout = true if diff <= 0
16
+ end
17
+
18
+ if @timeout
19
+ true
20
+ else
21
+ raise ThreadError
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RubyGo
2
+ VERSION = "0.1.0"
3
+ end
data/ruby_go.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_go/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_gothreads"
8
+ spec.version = RubyGo::VERSION
9
+ spec.authors = ["Zhuochun"]
10
+ spec.email = ["zhuochun@hotmail.com"]
11
+
12
+ spec.summary = %q{Syntax sugars around Ruby threads like Goroutines}
13
+ spec.description = %q{Syntax sugars (`go`, `chan`, `select`) around Ruby threads and queues, emulating Golang Goroutines}
14
+ spec.homepage = "https://github.com/zhuochun"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_gothreads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zhuochun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.11'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '10.0'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Syntax sugars (`go`, `chan`, `select`) around Ruby threads and queues, emulating Golang Goroutines
56
+ email:
57
+ - zhuochun@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - example/fibonacci.rb
73
+ - example/schedular.rb
74
+ - example/timeout.rb
75
+ - example/timeout_2.rb
76
+ - lib/ruby_go.rb
77
+ - lib/ruby_go/timeout.rb
78
+ - lib/ruby_go/version.rb
79
+ - ruby_go.gemspec
80
+ homepage: https://github.com/zhuochun
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.4
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Syntax sugars around Ruby threads like Goroutines
104
+ test_files: []