Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
136dc794
Commit
136dc794
authored
Feb 13, 2017
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Have some simple way to create connection pool
parent
79e8e613
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
lib/gitlab/database.rb
lib/gitlab/database.rb
+24
-0
spec/lib/gitlab/database_spec.rb
spec/lib/gitlab/database_spec.rb
+48
-0
No files found.
lib/gitlab/database.rb
View file @
136dc794
...
...
@@ -69,6 +69,30 @@ module Gitlab
end
end
def
self
.
with_connection_pool
(
pool_size
)
pool
=
create_connection_pool
(
pool_size
)
yield
(
pool
)
ensure
pool
.
disconnect!
end
def
self
.
create_connection_pool
(
pool_size
)
# See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb
env
=
Rails
.
env
original_config
=
ActiveRecord
::
Base
.
configurations
env_config
=
original_config
[
env
].
merge
(
'pool'
=>
pool_size
)
config
=
original_config
.
merge
(
env
=>
env_config
)
spec
=
ActiveRecord
::
ConnectionAdapters
::
ConnectionSpecification
::
Resolver
.
new
(
config
).
spec
(
env
.
to_sym
)
ActiveRecord
::
ConnectionAdapters
::
ConnectionPool
.
new
(
spec
)
end
def
self
.
connection
ActiveRecord
::
Base
.
connection
end
...
...
spec/lib/gitlab/database_spec.rb
View file @
136dc794
...
...
@@ -71,6 +71,54 @@ describe Gitlab::Database, lib: true do
end
end
describe
'.with_connection_pool'
do
it
'creates a new connection pool and disconnect it after used'
do
closed_pool
=
nil
described_class
.
with_connection_pool
(
1
)
do
|
pool
|
pool
.
with_connection
do
|
connection
|
connection
.
execute
(
'SELECT 1 AS value'
)
end
expect
(
pool
).
to
be_connected
closed_pool
=
pool
end
expect
(
closed_pool
).
not_to
be_connected
end
it
'disconnects the pool even an exception was raised'
do
error
=
Class
.
new
(
RuntimeError
)
closed_pool
=
nil
begin
described_class
.
with_connection_pool
(
1
)
do
|
pool
|
pool
.
with_connection
do
|
connection
|
connection
.
execute
(
'SELECT 1 AS value'
)
end
closed_pool
=
pool
raise
error
.
new
(
'boom'
)
end
rescue
error
end
expect
(
closed_pool
).
not_to
be_connected
end
end
describe
'.create_connection_pool'
do
it
'creates a new connection pool with specific pool size'
do
pool
=
described_class
.
create_connection_pool
(
5
)
expect
(
pool
)
.
to
be_kind_of
(
ActiveRecord
::
ConnectionAdapters
::
ConnectionPool
)
expect
(
pool
.
spec
.
config
[
:pool
]).
to
eq
(
5
)
end
end
describe
'#true_value'
do
it
'returns correct value for PostgreSQL'
do
expect
(
described_class
).
to
receive
(
:postgresql?
).
and_return
(
true
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment