Skip to content

Commit

Permalink
Merge pull request #1 from tedaford/add_postgres_patch
Browse files Browse the repository at this point in the history
Experiment to add a patch for postgres
  • Loading branch information
tedaford authored Sep 26, 2022
2 parents 3eaf01c + 4c8d9ce commit d088bf8
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/faulty/patch/postgres.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require 'pg'

class Faulty
module Patch
# Patch for the Postgres gem
module Postgres
include Base

Patch.define_circuit_errors(self, ::PG::ConnectionBad)

QUERY_WHITELIST = [
%r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i,
%r{\A(?:/\*.*?\*/)?\s*COMMIT}i,
%r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i
].freeze

def initialize(opts = {})
@faulty_circuit = Patch.circuit_from_hash(
'pg',
opts[:faulty],
errors: [
::PG::ConnectionBad,
::PG::UnableToSend
],
patched_error_mapper: Faulty::Patch::Postgres
)

super
end

def ping
faulty_run { super }
rescue Faulty::Patch::Postgres::FaultyError
false
end

def connect(*args)
faulty_run { super }
end

def query(*args)
return super if QUERY_WHITELIST.any? { |r| !r.match(args.first).nil? }

faulty_run { super }
end
end
end
end

module PG
class Connection
prepend Faulty::Patch::Postgres
end
end

0 comments on commit d088bf8

Please sign in to comment.