multithink 0.0.1

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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec(development_group: :runtime)
data/LICENSE ADDED
@@ -0,0 +1,41 @@
1
+ Copyright (c) 2011 Mike Perham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ Copyright (c) 2013 Joseph Glanville
23
+
24
+ Permission is hereby granted, free of charge, to any person obtaining
25
+ a copy of this software and associated documentation files (the
26
+ "Software"), to deal in the Software without restriction, including
27
+ without limitation the rights to use, copy, modify, merge, publish,
28
+ distribute, sublicense, and/or sell copies of the Software, and to
29
+ permit persons to whom the Software is furnished to do so, subject to
30
+ the following conditions:
31
+
32
+ The above copyright notice and this permission notice shall be
33
+ included in all copies or substantial portions of the Software.
34
+
35
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
39
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
40
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
41
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ MultiThink
2
+ ==========
3
+
4
+ Simple RethinkDB connection pool heavily based on the work of the connection_pool gem.
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+ rescue LoadError
5
+ end
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.libs << 'test'
10
+ test.warning = true
11
+ test.pattern = 'test/**/test_*.rb'
12
+ end
13
+
14
+ task :default => :test
@@ -0,0 +1,54 @@
1
+ require 'multithink/version'
2
+ require 'multithink/timed_stack'
3
+
4
+ class MultiThink
5
+ DEFAULTS = {size: 5, timeout: 5, servers: [{host: '127.0.0.1', port: 28015}]}
6
+
7
+ def initialize(options = {})
8
+ options = DEFAULTS.merge(options)
9
+
10
+ @size = options.fetch(:size)
11
+ @timeout = options.fetch(:timeout)
12
+ @servers = options.fetch(:servers)
13
+
14
+ @available = TimedStack.new(@size, @servers)
15
+ @key = :"current-#{@available.object_id}"
16
+ end
17
+
18
+ def with(options = {})
19
+ conn = checkout(options)
20
+ begin
21
+ yield conn
22
+ ensure
23
+ checkin
24
+ end
25
+ end
26
+
27
+ def checkout(options = {})
28
+ stack = ::Thread.current[@key] ||= []
29
+
30
+ if stack.empty?
31
+ timeout = options[:timeout] || @timeout
32
+ conn = @available.pop(timeout)
33
+ else
34
+ conn = stack.last
35
+ end
36
+
37
+ stack.push conn
38
+ conn
39
+ end
40
+
41
+ def checkin
42
+ stack = ::Thread.current[@key]
43
+ conn = stack.pop
44
+ if stack.empty?
45
+ @available << conn
46
+ end
47
+ nil
48
+ end
49
+
50
+ def shutdown
51
+ @available.shutdown
52
+ end
53
+
54
+ end
@@ -0,0 +1,25 @@
1
+ require 'thread'
2
+ require 'timeout'
3
+ require 'rethinkdb'
4
+
5
+ include RethinkDB::Shortcuts
6
+
7
+ class MultiThink::Connection
8
+
9
+ def initialize(servers)
10
+ @servers = servers
11
+ connect
12
+ end
13
+
14
+ def connect
15
+ # TODO try all servers until we get a connection
16
+ options = @servers.first
17
+ @conn = r.connect(options)
18
+ end
19
+
20
+ def run(query)
21
+ # TODO handle connection failure
22
+ query.run(@conn)
23
+ end
24
+
25
+ end
@@ -0,0 +1,63 @@
1
+ require 'thread'
2
+ require 'timeout'
3
+ require_relative 'connection'
4
+
5
+ class MultiThink::PoolShuttingDownError < RuntimeError; end
6
+
7
+ class MultiThink::TimedStack
8
+
9
+ def initialize(size = 0, servers)
10
+ @que = Array.new(size) {|s| MultiThink::Connection.new(servers)}
11
+ @mutex = Mutex.new
12
+ @resource = ConditionVariable.new
13
+ @shutdown_block = nil
14
+ end
15
+
16
+ def push(obj)
17
+ @mutex.synchronize do
18
+ if @shutdown_block
19
+ @shutdown_block.call(obj)
20
+ else
21
+ @que.push obj
22
+ end
23
+
24
+ @resource.broadcast
25
+ end
26
+ end
27
+ alias_method :<<, :push
28
+
29
+ def pop(timeout=0.5)
30
+ deadline = Time.now + timeout
31
+ @mutex.synchronize do
32
+ loop do
33
+ raise MultiThink::PoolShuttingDownError if @shutdown_block
34
+ return @que.pop unless @que.empty?
35
+ to_wait = deadline - Time.now
36
+ raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
37
+ @resource.wait(@mutex, to_wait)
38
+ end
39
+ end
40
+ end
41
+
42
+ def shutdown(&block)
43
+ raise ArgumentError, "shutdown must receive a block" unless block_given?
44
+
45
+ @mutex.synchronize do
46
+ @shutdown_block = block
47
+ @resource.broadcast
48
+
49
+ @que.size.times do
50
+ conn = @que.pop
51
+ block.call(conn)
52
+ end
53
+ end
54
+ end
55
+
56
+ def empty?
57
+ @que.empty?
58
+ end
59
+
60
+ def length
61
+ @que.length
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ class MultiThink
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "./lib/multithink/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "multithink"
6
+ s.version = MultiThink::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Joseph Glanville"]
9
+ s.email = ["jpg@jpg.id.au"]
10
+ s.homepage = "https://github.com/josephglanville/multithink"
11
+ s.description = s.summary = %q{Simple RethinkDB connection pool.}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ s.license = "MIT"
18
+ s.add_development_dependency 'minitest', '>= 5.0.0'
19
+ s.add_development_dependency 'rake'
20
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multithink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joseph Glanville
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 5.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple RethinkDB connection pool.
47
+ email:
48
+ - jpg@jpg.id.au
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/multithink.rb
59
+ - lib/multithink/connection.rb
60
+ - lib/multithink/timed_stack.rb
61
+ - lib/multithink/version.rb
62
+ - multithink.gemspec
63
+ homepage: https://github.com/josephglanville/multithink
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Simple RethinkDB connection pool.
88
+ test_files: []