This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start flushing out clean failover strategies
- Loading branch information
Showing
9 changed files
with
153 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ tmp/ | |
.replica | ||
mongo.key | ||
TODO | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# encoding: utf-8 | ||
require "moped/failover/ignore" | ||
|
||
module Moped | ||
|
||
# Provides behaviour around failover scenarios for different types of | ||
# exceptions that get raised on connection and execution of operations. | ||
# | ||
# @since 2.0.0 | ||
module Failover | ||
extend self | ||
|
||
# Hash lookup for the failover classes based off the exception type. | ||
# | ||
# @since 2.0.0 | ||
STRATEGIES = {}.freeze | ||
|
||
# Get the appropriate failover handler given the provided exception. | ||
# | ||
# @example Get the failover handler for an IOError. | ||
# Moped::Failover.get(IOError) | ||
# | ||
# @param [ Exception ] exception The raised exception. | ||
# | ||
# @return [ Object ] The failover handler. | ||
# | ||
# @since 2.0.0 | ||
def get(exception) | ||
STRATEGIES.fetch(exception, Ignore).new(exception) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: utf-8 | ||
module Moped | ||
module Failover | ||
|
||
# Ignore is for the case when we get exceptions we do not know about, or | ||
# exceptions we deem are proper user errors and should be re-raised. | ||
# | ||
# @since 2.0.0 | ||
class Ignore | ||
|
||
# @!attribute exception | ||
# @return [ Exception ] The raised exception. | ||
attr_reader :exception | ||
|
||
# Instantiate the new Ignore handler. | ||
# | ||
# @example Instantiate the new ignore handler. | ||
# Moped::Failover::Ignore.new(exception) | ||
# | ||
# @param [ Exception ] exception The raised exception. | ||
# | ||
# @since 2.0.0 | ||
def initialize(exception) | ||
@exception = exception | ||
end | ||
|
||
# Executes the failover strategy. In the case of ignore, we just re-raise | ||
# the exception that was thrown previously. | ||
# | ||
# @example Execute the ignore strategy. | ||
# ignore.execute(node) | ||
# | ||
# @param [ Node ] node The node the exception got raised on. | ||
# @param [ Proc ] block The optional block. | ||
# | ||
# @raise [ Exception ] The exception that was previously thrown. | ||
# | ||
# @since 2.0.0 | ||
def execute(node, &block) | ||
raise(exception) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# encoding: utf-8 | ||
require "moped/read" | ||
require "moped/read_preference" | ||
|
||
module Moped | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "spec_helper" | ||
|
||
describe Moped::Failover::Ignore do | ||
|
||
describe "#initialize" do | ||
|
||
let(:exception) do | ||
RuntimeError.new | ||
end | ||
|
||
let(:ignore) do | ||
described_class.new(exception) | ||
end | ||
|
||
it "sets the exception" do | ||
expect(ignore.exception).to eq(exception) | ||
end | ||
end | ||
|
||
describe "#execute" do | ||
|
||
let(:exception) do | ||
RuntimeError.new | ||
end | ||
|
||
let(:node) do | ||
Moped::Node.new("127.0.0.1:27017") | ||
end | ||
|
||
let(:ignore) do | ||
described_class.new(exception) | ||
end | ||
|
||
it "raises the exception" do | ||
expect { | ||
ignore.execute(node) | ||
}.to raise_error(exception) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "spec_helper" | ||
|
||
describe Moped::Failover do | ||
|
||
describe ".get" do | ||
|
||
context "when providing an unregistered exception" do | ||
|
||
let(:failover) do | ||
described_class.get(RuntimeError) | ||
end | ||
|
||
it "returns ignore" do | ||
expect(failover).to be_a(Moped::Failover::Ignore) | ||
end | ||
end | ||
end | ||
end |