oversip-mod-postgresql 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.
data/AUTHORS ADDED
@@ -0,0 +1,5 @@
1
+ MAIN AUTHOR
2
+ ===========
3
+
4
+ - Iñaki Baz Castillo <ibc@aliax.net> (Github @ibc)
5
+
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Name: oversip-mod-postgresql
2
+ Maintainer: Iñaki Baz Castillo <ibc@aliax.net>
3
+ Copyright (c) 2012 Iñaki Baz Castillo <ibc@aliax.net>
4
+
5
+
6
+ License: The MIT License
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining
9
+ a copy of this software and associated documentation files (the
10
+ "Software"), to deal in the Software without restriction, including
11
+ without limitation the rights to use, copy, modify, merge, publish,
12
+ distribute, sublicense, and/or sell copies of the Software, and to
13
+ permit persons to whom the Software is furnished to do so, subject to
14
+ the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # oversip-mod-postgresql
2
+
3
+ ## Overview
4
+
5
+ `oversip-mod-postgresql` provides an easy to use PostgreSQL connector for [OverSIP](http://www.oversip.net) proxy based on [ruby-em-pg-client](https://github.com/royaltm/ruby-em-pg-client) driver (which is based on [ruby-pg](https://bitbucket.org/ged/ruby-pg/wiki/Home)).
6
+
7
+ `oversip-mod-postgresql` depends on [OverSIP](http://www.oversip.net) >= 1.3.0 which enforces the usage of "sync" style coding via [em-synchrony](https://github.com/igrigorik/em-synchrony/) Gem.
8
+
9
+ * For more information about `em-synchrony` usage check [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/).
10
+
11
+ Check the [ruby-em-pg-client documentation](https://github.com/royaltm/ruby-em-pg-client/blob/master/README.rdoc) and [ruby-pg documentation](http://deveiate.org/code/pg/) for the exact syntax and usage.
12
+
13
+
14
+ ## API
15
+
16
+
17
+ ### Method `OverSIP::Modules::Postgresql.add_pool(options)`
18
+
19
+ Creates a PostgreSQL connection pool by receiving a mandatory `options` (a `Hash`) with the following fields:
20
+ * `:pool_name`: Mandatory field. Must be a `Symbol` with the name for this pool.
21
+ * `:pool_size`: The number of parallel PostgreSQL connections to perform. By default 10.
22
+ * The rest of fields will be passed to each `PG::EM::Client.new` being created (which inherits from [`PG::Connection`](http://deveiate.org/code/pg/PG/Connection.html)).
23
+
24
+ The method allows passing a block which would be later called by passing as argument each generated `PG::EM::Client` instance.
25
+
26
+ The created connection pool is an instance of [`EventMachine::Synchrony::ConnectionPool`](https://github.com/igrigorik/em-synchrony/blob/master/lib/em-synchrony/connection_pool.rb).
27
+
28
+
29
+ ### Method `OverSIP::Modules::PostgreSQL.pool(pool_name)`
30
+
31
+ Retrieves a previously created pool with the given name. Raises an `ArgumentError` if the given name does not exist in the list of created pools.
32
+
33
+
34
+
35
+ ## Usage Example
36
+
37
+ On top of `/etc/oversip/server.rb`:
38
+
39
+ ```
40
+ require "oversip-mod-postgresql"
41
+ ```
42
+
43
+
44
+ Within the `OverSIP::SipEvents.on_initialize()` method in `/etc/oversip/server.rb`:
45
+
46
+ ```
47
+ def (OverSIP::SystemEvents).on_initialize
48
+ OverSIP::M::Postgresql.add_pool({
49
+ :pool_name => :my_db,
50
+ :pool_size => 5,
51
+ :host => "localhost",
52
+ :user => "oversip",
53
+ :password => "xxxxxx",
54
+ :dbname => "oversip"
55
+ }) {|conn| log_info "PostgreSQL created connection: #{conn.inspect}" }
56
+ end
57
+ ```
58
+
59
+ Somewhere within the `OverSIP::SipEvents.on_request()` method in `/etc/oversip/server.rb`:
60
+
61
+ ```
62
+ pool = OverSIP::M::Postgresql.pool(:my_db)
63
+
64
+ begin
65
+ result = pool.query "SELECT * FROM users WHERE user = \'#{request.from.user}\'"
66
+ log_info "DB query result: #{result.to_a.inspect}"
67
+ if result.any?
68
+ # Add a X-Header with value the 'custom_header' field of the table row:
69
+ request.set_header "X-Header", result.first["custom_header"]
70
+ proxy = ::OverSIP::SIP::Proxy.new :proxy_out
71
+ proxy.route request
72
+ return
73
+ else
74
+ request.reply 404, "User not found in DB"
75
+ return
76
+ end
77
+
78
+ rescue ::PG::Error => e
79
+ log_error "DB query error:"
80
+ log_error e
81
+ request.reply 500, "DB query error"
82
+ return
83
+ end
84
+ ```
85
+
86
+
87
+ ## Dependencies
88
+
89
+ * Ruby > 1.9.2.
90
+ * [oversip](http://www.oversip.net) Gem >= 1.3.0.
91
+ * PostgreSQL development library (the package `libpq-dev` in Debian/Ubuntu).
92
+
93
+
94
+ ## Installation
95
+
96
+ ```
97
+ ~$ gem install oversip-mod-postgresql
98
+ ```
99
+
100
+
101
+ ## Author
102
+
103
+ Iñaki Baz Castillo
104
+ * Mail: ibc@aliax.net
105
+ * Github profile: [@ibc](https://github.com/ibc)
@@ -0,0 +1,58 @@
1
+ require "oversip-mod-postgresql/version.rb"
2
+
3
+ require "em-synchrony/pg" # NOTE: Included in em-pg-client/lib/.
4
+
5
+
6
+ module OverSIP
7
+ module Modules
8
+
9
+ module Postgresql
10
+
11
+ extend ::OverSIP::Logger
12
+
13
+ DEFAULT_POOL_SIZE = 10
14
+
15
+ @log_id = "Postgresql module"
16
+ @pools = {}
17
+
18
+ def self.add_pool options
19
+ raise ::ArgumentError, "`options' must be a Hash" unless options.is_a? ::Hash
20
+
21
+ pool_name = options.delete(:pool_name)
22
+ pool_size = options.delete(:pool_size) || DEFAULT_POOL_SIZE
23
+
24
+ raise ::ArgumentError, "`options[:pool_name]' must be a Symbol" unless pool_name.is_a? ::Symbol
25
+ raise ::ArgumentError, "`options[:pool_size]' must be a positive Fixnum" unless pool_size.is_a? ::Fixnum and pool_size > 0
26
+
27
+ block = ::Proc.new if block_given?
28
+
29
+ ::OverSIP::SystemCallbacks.on_started do
30
+ log_info "Adding PostgreSQL connection pool (name: #{pool_name.inspect}, size: #{pool_size})..."
31
+ @pools[pool_name] = ::EM::Synchrony::ConnectionPool.new(size: pool_size) do
32
+ # Avoid the hash to be modified by PG::EM::Client.
33
+ new_options = options.clone
34
+ # Force DB autoreconnect.
35
+ new_options[:async_autoreconnect] = true
36
+
37
+ conn = ::PG::EM::Client.new(new_options)
38
+
39
+ # Call the given block by passing conn as argument.
40
+ block.call(conn) if block
41
+ conn
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.pool pool_name
47
+ pool = @pools[pool_name]
48
+ raise ::ArgumentError, "no pool with `name' #{pool_name.inspect}" unless pool
49
+ pool
50
+ end
51
+ class << self
52
+ alias :get_pool :pool
53
+ end
54
+
55
+ end # module Postgresql
56
+
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module OverSIP
2
+ module Modules
3
+
4
+ module Postgresql
5
+ module Version
6
+ MAJOR = 0
7
+ MINOR = 1
8
+ TINY = 0
9
+ DEVEL = nil # Set to nil for stable releases.
10
+ end
11
+
12
+ VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].join(".")
13
+ VERSION << ".#{Version::DEVEL}" if Version::DEVEL
14
+ end
15
+
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oversip-mod-postgresql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Inaki Baz Castillo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oversip
16
+ requirement: &15205740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *15205740
25
+ - !ruby/object:Gem::Dependency
26
+ name: em-pg-client
27
+ requirement: &15205280 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *15205280
36
+ description: oversip-mod-postgresql provides an easy to use PostgreSQL connector for
37
+ OverSIP proxy.
38
+ email:
39
+ - ibc@aliax.net
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/oversip-mod-postgresql.rb
45
+ - lib/oversip-mod-postgresql/version.rb
46
+ - README.md
47
+ - AUTHORS
48
+ - LICENSE
49
+ homepage: https://github.com/versatica/oversip-mod-postgresql
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.2
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.11
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: PostgreSQL connector module for OverSIP
73
+ test_files: []
74
+ has_rdoc: false