Home

Awesome

semian-postgres

CI Workflow Gem Version

This library provides a Postgres adapter for Semian by wrapping the pg gem Semian is a resiliency toolkit for Ruby applications that provides a way to protect your application from external failures by limiting the number of resources that can be used at a time. You can read more about Semian here).

Usage

Add the gem to your Gemfile and require it in your application.

gem 'semian-postgres', require: %w(semian semian/pg)

Configuration

The adapter is configured by a callback function, which would be ideally defined in some sort of initialization file. For Rails applications these would usually live in the config/initializers/ directory.

Minimal example

The following example configures an adapter to open the circuit after three unsuccessful connection attempts and close it after each successful attempt.

Bulkheading is disabled, because this is not supported with servers that have a thread-oriented model, such as Puma

require "semian"
require "semian/pg"

SEMIAN_PARAMETERS = {
  circuit_breaker: true,
  success_threshold: 1,
  error_threshold: 3,
  error_timeout: 3,
  bulkhead: false,
}

Semian::PG.semian_configuration = proc do |host, port|
  if host == "localhost" && port == 5432
    return SEMIAN_PARAMETERS
  end
end

conn = PG.connect(host: "example.com", port: 5432)
conn.exec("SELECT 1")

Development

Semian, and by extension semian-postgres, currently depends on Linux for Bulkheading.

The development environment is based on docker-compose, spinning up containers for Postgres and Toxiproxy. Additionally a dev container is spun up. The Gemfile contains ruby-debug-ide to support remote debugging from the IDE.

A typical development workflow would be to run the tests in the dev container

docker compose up -d
docker compose exec dev bin/setup
docker compose exec dev rake rubocop spec

Contributing

Bug reports and pull requests are welcome on GitHub.

Thanks to everyone that has already contributed

<a href="https://github.com/mschoenlaub/semian-postgres/graphs/contributors"> <img src="https://contrib.rocks/image?repo=mschoenlaub/semian-postgres" alt="Pictures of the folks who have contributed to the project" /> </a>

License

The gem is available as open source under the terms of the MIT License.