sequel_fresh_connections 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34f62fc00a104fd9bee3bdfa6a92a18fa35d6575
4
+ data.tar.gz: 5f6d050a58af9521d7523c6622730869be9d9bfb
5
+ SHA512:
6
+ metadata.gz: c3d3d094a84e1ed7cc170e27689f929e25928761aaa969b984c69527360778acd92c0e21e8e6be48933fa1f9135fb54c674394f903f5ca6f3efd976d28f56276
7
+ data.tar.gz: 3c40bb67b88eec107f479313b8f6192ccfd5a27895112c1ca3853f1ee33bceeb1630362565d020dd1645f158c4a2e442d6de634ccbdec42dac36fc0a6e0ba46b
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ # Fresh Connections
2
+
3
+ The fresh_connections extension removes stale connections from the connection
4
+ pool, ensuring any connections that have timed out won't be used.
5
+
6
+ Enable the extension with:
7
+
8
+ Sequel.extension :fresh_connections
9
+
10
+ A :max_connection_age option will then be available with Sequel::connect, this
11
+ allows setting the maximum age a connection is allowed to reach before being
12
+ removed from the pool.
13
+
14
+ Sequel.connect(..., :max_connection_age => 600)
15
+
16
+ Each time a connection is used its age is reset, so as long as connections are
17
+ in constant use they will not be removed from the pool.
@@ -0,0 +1,26 @@
1
+ Sequel.require 'connection_pool/sharded_single'
2
+
3
+ class Sequel::FreshShardedSingleConnectionPool < Sequel::ShardedSingleConnectionPool
4
+
5
+ def initialize(opts={})
6
+ super
7
+ @max_connection_age = opts[:max_connection_age] || 1800 # 30 mins
8
+ @last_uses = {}
9
+ end
10
+
11
+ def hold(server=:default)
12
+ begin
13
+ server = pick_server(server)
14
+ conn_age = @last_uses[server] ? Time.now.to_i - @last_uses[server] : 0
15
+ if conn_age > @max_connection_age
16
+ disconnect_server(server, &@disconnection_proc)
17
+ end
18
+ @last_uses[server] = Time.now.to_i
19
+ yield(@conns[server] ||= make_new(server))
20
+ rescue Sequel::DatabaseDisconnectError
21
+ disconnect_server(server, &@disconnection_proc)
22
+ raise
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,28 @@
1
+ Sequel.require 'connection_pool/sharded_threaded'
2
+
3
+ class Sequel::FreshShardedThreadedConnectionPool < Sequel::ShardedThreadedConnectionPool
4
+
5
+ def initialize(opts={})
6
+ super
7
+ @max_connection_age = opts[:max_connection_age] || 1800 # 30 mins
8
+ @last_uses = {}
9
+ end
10
+
11
+ def acquire(thread, server)
12
+ while true
13
+ conn = super
14
+ last_use = @last_uses[conn.object_id]
15
+ connection_age = last_use ? Time.now.to_i - last_use : 0
16
+ break if connection_age <= @max_connection_age
17
+ sync {remove(thread, conn, server)}
18
+ end
19
+ @last_uses[conn.object_id] = Time.now.to_i
20
+ conn
21
+ end
22
+
23
+ def remove(thread, conn, server)
24
+ @last_uses.delete(conn.object_id)
25
+ super
26
+ end
27
+
28
+ end
@@ -0,0 +1,18 @@
1
+ Sequel.require 'connection_pool/single'
2
+
3
+ class Sequel::FreshSingleConnectionPool < Sequel::SingleConnectionPool
4
+
5
+ def initialize(opts={})
6
+ super
7
+ @max_connection_age = opts[:max_connection_age] || 1800 # 30 mins
8
+ @last_use = nil
9
+ end
10
+
11
+ def hold(server=nil)
12
+ connection_age = @last_use ? Time.now.to_i - @last_use : 0
13
+ disconnect if connection_age > @max_connection_age
14
+ @last_use = Time.now.to_i
15
+ super
16
+ end
17
+
18
+ end
@@ -0,0 +1,25 @@
1
+ Sequel.require 'connection_pool/threaded'
2
+
3
+ class Sequel::FreshThreadedConnectionPool < Sequel::ThreadedConnectionPool
4
+
5
+ def initialize(opts={})
6
+ super
7
+ @max_connection_age = opts[:max_connection_age] || 1800 # 30 mins
8
+ @last_uses = {}
9
+ end
10
+
11
+ def acquire(thread)
12
+ while true
13
+ conn = super
14
+ last_use = @last_uses[conn.object_id]
15
+ connection_age = last_use ? Time.now.to_i - last_use : 0
16
+ break if connection_age <= @max_connection_age
17
+ @disconnection_proc.call(conn) if @disconnection_proc
18
+ @allocated.delete(thread)
19
+ @last_uses.delete(conn.object_id)
20
+ end
21
+ @last_uses[conn.object_id] = Time.now.to_i
22
+ conn
23
+ end
24
+
25
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path("../../connection_pool/fresh_single", __FILE__)
2
+ require File.expand_path("../../connection_pool/fresh_sharded_single", __FILE__)
3
+ require File.expand_path("../../connection_pool/fresh_threaded", __FILE__)
4
+ require File.expand_path("../../connection_pool/fresh_sharded_threaded", __FILE__)
5
+
6
+ single_threaded = true
7
+ threaded = false
8
+ sharded = true
9
+ not_sharded = false
10
+
11
+ map = Sequel::ConnectionPool::CONNECTION_POOL_MAP
12
+
13
+ map[[single_threaded, not_sharded]] = Sequel::FreshSingleConnectionPool
14
+ map[[single_threaded, sharded]] = Sequel::FreshShardedSingleConnectionPool
15
+ map[[threaded, not_sharded]] = Sequel::FreshThreadedConnectionPool
16
+ map[[threaded, sharded]] = Sequel::FreshShardedThreadedConnectionPool
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_fresh_connections
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Sadler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A Sequel extension to remove stale, potentially timed out, connections
28
+ from the connection pool.
29
+ email: mat@sourcetagsandcodes.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README.rdoc
34
+ files:
35
+ - README.rdoc
36
+ - lib/sequel/connection_pool/fresh_sharded_single.rb
37
+ - lib/sequel/connection_pool/fresh_sharded_threaded.rb
38
+ - lib/sequel/connection_pool/fresh_single.rb
39
+ - lib/sequel/connection_pool/fresh_threaded.rb
40
+ - lib/sequel/extensions/fresh_connections.rb
41
+ homepage: https://github.com/globaldev/sequel_fresh_connections
42
+ licenses: []
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options:
46
+ - "--main"
47
+ - README.rdoc
48
+ - "--charset"
49
+ - utf-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Keep your Sequel DB connections fresh
68
+ test_files: []