diode 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/diode/pgate.rb +71 -0
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a336c0eda5cf34b05fe4d4fe013dc181af28163588ede63ba741c76c8e976b9
4
- data.tar.gz: e84ea6a14a4b6a8682e0ce74593c23fe828bf36ac19d5072b5a534a4c44eb632
3
+ metadata.gz: 9ea759707873eb1e4b875b88f3592a9943bda41f0e7e2b734a02f97907a3418e
4
+ data.tar.gz: 57be21fb601cdb86a63b82f4bb61d1ea7950f1b308099303425cadcf16be99bb
5
5
  SHA512:
6
- metadata.gz: c86e2f8c48f2c053f4f236113de844f811b42058f9d62895cb00b1db397c22ec221bc577366674a35ecc9c463ab8296c378871f8d7ea280427930035eeb2c056
7
- data.tar.gz: b31be1338d45b4999c3688778aacdc7946140444a0293f30cf0c986a3747fd7105e59f687ca782c98e2b79595faa12f3c03bd436a84e74828266d98942f97548
6
+ metadata.gz: 3adb8476510156d01dff10b3b66130a1dc91e65710416c1a4ee07fb43513758172efca9701b876e7d6c5c87e09561842d437c5445c4a03fc352737ac3e5b9f37
7
+ data.tar.gz: 34dceb159914710b19af8e4683de8f361206574aabd6dfaf0fb9b29d76b23a82f7b482af2370d98c0e244823c1b1f697643d48301fd0d91b6c48b10b5307daa6
@@ -0,0 +1,71 @@
1
+ # pgate - Postgresql connection pooling
2
+ require 'pg'
3
+
4
+ module Diode
5
+ class Pgate
6
+ attr_accessor(:max, :min, :connHash)
7
+ attr_reader(:pool, :inuse)
8
+
9
+ def initialize(connectionHash, max=10, min=1)
10
+ @connHash = connectionHash
11
+ @max = max || 10
12
+ @min = min || 1
13
+ @pool = []
14
+ @inuse = []
15
+ @running = true
16
+ Thread.new() {
17
+ curate() while @running
18
+ }
19
+ end
20
+
21
+ def assign()
22
+ conn = nil
23
+ return nil unless @running
24
+ if @pool.empty?
25
+ conn = PG::Connection.new(@connHash) if @inuse.size < @max
26
+ @inuse << conn
27
+ return(conn)
28
+ else
29
+ conn = @pool.shift()
30
+ return(assign()) unless ok?(conn) # recurse if bad connection
31
+ @inuse << conn
32
+ return(conn)
33
+ end
34
+ end
35
+
36
+ def release(conn) # return conn to pool if its alive, else discard it
37
+ @inuse.delete(conn) if @inuse.include?(conn)
38
+ @pool << conn if ok?(conn)
39
+ end
40
+
41
+ def ok?(conn)
42
+ return false unless conn.respond_to?(:conninfo)
43
+ begin
44
+ conn.conninfo()
45
+ return(true)
46
+ rescue PG::ConnectionBad => e
47
+ return(false)
48
+ end
49
+ end
50
+
51
+ def curate()
52
+ if @pool.size + @inuse.size > @max
53
+ unless @pool.empty?
54
+ conn = @pool.shift()
55
+ conn.close
56
+ end
57
+ end
58
+ if @pool.size + @inuse.size < @min
59
+ @pool << conn = PG::Connection.new(@connHash)
60
+ end
61
+ sleep(30)
62
+ end
63
+
64
+ def shutdown()
65
+ @running = false
66
+ (@pool + @inuse).each{|c| c.close() }
67
+ end
68
+
69
+ end
70
+ end
71
+ # load 'pgate.rb'; h = {host: "127.0.0.1", port: 7000, dbname: 'admin', user: 'richard', password: 'redacted24'}; pool = Diode::Pgate.new(h)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kjell Koda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-20 00:00:00.000000000 Z
11
+ date: 2024-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -48,6 +48,7 @@ executables: []
48
48
  extensions: []
49
49
  extra_rdoc_files: []
50
50
  files:
51
+ - lib/diode/pgate.rb
51
52
  - lib/diode/request.rb
52
53
  - lib/diode/response.rb
53
54
  - lib/diode/server.rb