Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rbsafecircuit

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rbsafecircuit

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

rbsafecircuit

rbsafecircuit is a Ruby gem for implementing a circuit breaker pattern with event handling capabilities using event_emitter.

Installation

Add this line to your application's Gemfile:

gem 'rbsafecircuit'

And then execute:

bundle install

Or install it yourself as:

gem install rbsafecircuit

Example Usage

require 'rbsafecircuit'

# Example usage of CircuitBreaker
max_failures = 3
timeout = 10
pause_time = 5  # in seconds for simplicity
max_consecutive_successes = 2

cb = CircuitBreaker.new(max_failures, timeout, pause_time, max_consecutive_successes)

# Define a function to be executed (replace with your actual function)
test_function = lambda do
  # Simulate some work that might succeed or fail
  if rand < 0.8  # 80% success chance
    puts "Function succeeded!"
    "Success"
  else
    puts "Function failed!"
    raise StandardError.new("Function failed")
  end
end

# Execute the function with CircuitBreaker protection
cb.execute(&test_function)

# Event handling examples
cb.on_success { |result| puts "Success: #{result}" }
cb.on_failure { |error| puts "Error: #{error.message}" }
cb.set_on_open(lambda { puts "Circuit breaker is open!" })
cb.set_on_close(lambda { puts "Circuit breaker is closed." })
cb.set_on_half_open(lambda { puts "Circuit breaker is half-open." })

# Example: Let's wait for a while to see the events (just for demonstration)
sleep(15)

# Cleanup or final operations
# ...

License

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

FAQs

Package last updated on 30 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc