go 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09ae6d77311c5f0093959d0b0b1701cc7684bda4
4
- data.tar.gz: 8a5be4a29e3bdcb3df09c1d2a591a869527a4c80
3
+ metadata.gz: 5d9a1e19c34020700bd447ec9b86dae9d5553de9
4
+ data.tar.gz: 751dfbc41b3303002078ea20c8dfbe9b78c68cf0
5
5
  SHA512:
6
- metadata.gz: 2ec5750139e08493cf506268455bd3908fd0ff3329b3793ef4a456ed3c20b9b8364982bc6f8e40e7dc3490f309d310a46dda11dec18876fae12443ae4ca1d049
7
- data.tar.gz: ad7e364559a0c86edbab73a0a1fe48b79c73d4761bab05325dc9493bce7e2d0a1609a0c8144aac36773d08b3a02577584e460f9e859f22ebe9ad69f209eca161
6
+ metadata.gz: 242ffa4e12d8b0ef1a4ac43909546aefd80b88a19247b587282d230974f66c6ecb69af8961bf40c5ed0afc51cdc766268fcc22cef67b60ee03cfb940287abdbb
7
+ data.tar.gz: 17673a4ba50edbffe3965408f71865fb88915dac199db4a1efee8c3fb568114825510009647fe07b54b84bbbe12ff9090de5d9c92b39170cc620b19752fb86ac
data/README.md CHANGED
@@ -92,3 +92,11 @@ You can close the channel with the close() method which will stop all the listen
92
92
  ch.close()
93
93
  ```
94
94
 
95
+ #### Automatically closing the channel after x messages
96
+
97
+ When making the channel, pass in :close_after parameter:
98
+
99
+ ```ruby
100
+ ch = Go::Channel.new(:close_after=>100)
101
+ ```
102
+
data/lib/go.rb CHANGED
@@ -2,14 +2,13 @@ require 'logger'
2
2
  require 'concur'
3
3
 
4
4
  require_relative 'go/version'
5
- require_relative 'go/config'
6
5
  require_relative 'go/channel'
7
6
  require_relative 'go/kernel'
8
7
 
9
8
  module Go
10
9
  @@logger = Logger.new(STDOUT)
11
10
  @@logger.level = Logger::INFO
12
- @@config = Config.new
11
+ @@config = Concur::Config.new
13
12
 
14
13
  def self.logger
15
14
  @@logger
@@ -1,8 +1,11 @@
1
1
  module Go
2
2
  class Channel
3
- attr_reader :closed
3
+ attr_reader :closed, :msg_counter
4
+ attr_accessor :options
4
5
 
5
- def initialize
6
+ def initialize(options={})
7
+ @options = options
8
+ @msg_counter = 0
6
9
  @queue = Queue.new
7
10
  end
8
11
 
@@ -12,12 +15,14 @@ module Go
12
15
 
13
16
  def shift
14
17
  begin
18
+ @msg_counter += 1
19
+ if @options[:close_after] && @msg_counter >= @options[:close_after]
20
+ close()
21
+ end
15
22
  @queue.shift
16
23
  rescue Exception => ex
17
- puts ex.class.name
18
- p ex
19
- puts ex.message
20
- if ex.class.name == "fatal" && ex.message.include?("deadlock")
24
+ Go.logger.debug "#{ex.class.name}: #{ex.message}"
25
+ if (ex.message.include?("deadlock") || ex.message.include?("Deadlock")) # ruby 2.0 uses Deadlock, capitalized
21
26
  close()
22
27
  return Go::Exit
23
28
  end
@@ -1,3 +1,3 @@
1
1
  module Go
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -8,6 +8,7 @@ class TestGo < Test::Unit::TestCase
8
8
 
9
9
  Go.config.max_threads = 10
10
10
 
11
+
11
12
  3.times do |i|
12
13
  go do
13
14
  puts "#{i} sleeping..."
@@ -83,6 +84,49 @@ class TestGo < Test::Unit::TestCase
83
84
  assert_equal times / errmod, errs
84
85
  puts 'donezo'
85
86
 
87
+
88
+ # Convenience trick with channels
89
+ ch = Go::Channel.new
90
+ times = 10
91
+ errmod = 5
92
+ times.times do |i|
93
+ go(ch) do
94
+ puts "raise? #{i % errmod == 0}"
95
+ raise "Error yo!" if i % errmod == 0
96
+ s = "hello channel #{i}"
97
+ sleep 1
98
+ s # don't put the return keyword, it doesn't work with threads for some reason, you'll get "unexpected return"
99
+ end
100
+ end
101
+ go do
102
+ # wait for a bit, then close channel
103
+ sleep 10
104
+ ch.close
105
+ end
106
+ puts "waiting on channel..."
107
+ i = 0
108
+ errs = 0
109
+ ch.each do |x|
110
+ puts "Got #{x} from channel"
111
+ i += 1
112
+ case x
113
+ when String
114
+ puts "Is String"
115
+ when Exception
116
+ puts "Is Exception!"
117
+ errs += 1
118
+ when nil
119
+ puts "got nil, breaking"
120
+ break
121
+ else
122
+ puts "Is something else"
123
+ end
124
+ end
125
+ assert_equal times, i
126
+ assert_equal times / errmod, errs
127
+ puts 'donezo'
128
+
129
+
86
130
  end
87
131
 
88
132
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-02-27 00:00:00.000000000 Z
11
+ date: 2013-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concur
@@ -52,7 +52,6 @@ files:
52
52
  - go.gemspec
53
53
  - lib/go.rb
54
54
  - lib/go/channel.rb
55
- - lib/go/config.rb
56
55
  - lib/go/kernel.rb
57
56
  - lib/go/version.rb
58
57
  - test/test_go.rb
@@ -75,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
74
  version: 1.3.6
76
75
  requirements: []
77
76
  rubyforge_project:
78
- rubygems_version: 2.0.0
77
+ rubygems_version: 2.0.3
79
78
  signing_key:
80
79
  specification_version: 4
81
80
  summary: A concurrency library for Ruby inspired by Go (golang).
@@ -1,36 +0,0 @@
1
- module Go
2
- class Config
3
- attr_reader :defaults
4
-
5
- def initialize(options={})
6
- init_defaults
7
- @listeners = []
8
- @max_threads = options[:max_threads] || defaults[:max_threads]
9
- end
10
-
11
- def init_defaults
12
- @defaults = {
13
- max_threads: 20
14
- }
15
- end
16
-
17
- def max_threads=(x)
18
- @max_threads = x
19
- notify_listeners(:max_threads=>x)
20
- end
21
-
22
- def max_threads
23
- @max_threads
24
- end
25
-
26
- def add_listener(l)
27
- @listeners << l
28
- end
29
-
30
- def notify_listeners(changes)
31
- @listeners.each do |l|
32
- l.update(changes)
33
- end
34
- end
35
- end
36
- end