diode 1.3.2 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea33eabe22d4cdc90c861901276c3c1f9f397d631c3aba3b9a541f1f87390189
4
- data.tar.gz: 3d5616c2b0a342f229b49a3e7c27c77c4c255484c21876f2cdfadf7cbe7a4ffa
3
+ metadata.gz: 9ea759707873eb1e4b875b88f3592a9943bda41f0e7e2b734a02f97907a3418e
4
+ data.tar.gz: 57be21fb601cdb86a63b82f4bb61d1ea7950f1b308099303425cadcf16be99bb
5
5
  SHA512:
6
- metadata.gz: 8ccade8f6c924359da6ebe142220351b5462015bfee482399cd7e52fb09ef2349df610c6d2e149d9128a4f76af0fc56c8c14b1fcc1dc44337d456379c333b903
7
- data.tar.gz: 31869f03329647cac6f4a5d7746d1801683105b538b5c98cfdb3bad0643153b39044f7611de323b84061e503b4f4ee2baacd959100821d5086fd181da318cec9
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)
data/lib/diode/static.rb CHANGED
@@ -12,8 +12,8 @@ class Static
12
12
 
13
13
  def serve(request)
14
14
  return Diode::Response.standard(405) unless request.method == "GET"
15
- unless guardian.nil?
16
- securityResponse = guardian.call(request)
15
+ unless @guardian.nil?
16
+ securityResponse = @guardian.call(request)
17
17
  unless securityResponse.nil? or securityResponse == 200
18
18
  return securityResponse if securityResponse.is_a?(Diode::Response)
19
19
  return Diode::Response.standard(securityResponse) if [400,403,404,405].include?(securityResponse)
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.2
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