Code Monkey home page Code Monkey logo

circuitbreaker's Introduction

Kitura

APIDoc Build Status - Master macOS Linux Apache 2 Slack Status

CircuitBreaker

The Circuit Breaker design pattern is used to increase application stability, improve response times and prevent the application from making constant failing requests. This library provides the tools needed to bring Circuit Breaker logic to your Swift applications.

Circuit Breaker Diagram

Swift version

The latest version of CircuitBreaker works with the 5.1 and newer version of the Swift binaries. You can download this version of the Swift binaries by following this link.

Getting Started

Add CircuitBreaker to the dependencies within your application's Package.swift file. Substitute "x.x.x" with the latest CircuitBreaker release.

.package(url: "https://github.com/Kitura/CircuitBreaker.git", from: "x.x.x")

Add CircuitBreaker to your target's dependencies:

.target(name: "example", dependencies: ["CircuitBreaker"]),

Import the package inside your application:

import CircuitBreaker

Usage

The CircuitBreaker state is based on timeouts and user defined failures (quite useful when the function you are circuit breaking makes an asynchronous call). To use the CircuitBreaker in your application code you need to do the following:

  • Define a fallback function with the signature (BreakerError, (fallbackArg1, fallbackArg2, ...)) -> Void:
func myFallback(err: BreakerError, msg: String) {
    // The fallback will be called if one of the below occurs:
    //  1. The request does not return before the specified timeout
    //  2. CircuitBreaker is currently in Open state and set to fail fast.
    //  3. There was an error in the user's called context function (networking error, etc.)
    Log.verbose("Error: \(err)")
    Log.verbose("Message: \(msg)")
}
  • Extend BreakerError by defining your own error handling to be used in your context function:
extension BreakerError {
    public static let encodingURLError = BreakerError(reason: "URL could not be created")
    public static let networkingError = BreakerError(reason: "There was an error, while sending the request")
    public static let jsonDecodingError = BreakerError(reason: "Could not decode result into JSON")
}
  • Create a context function for the logic you intend to circuit break (this allows you to alert the CircuitBreaker of a failure or a success). A context function receives an Invocation object as its parameter. An instance of the Invocation class states:
    • The parameter types that must be passed to the context function.
    • The return type from the execution of the context function.
    • The parameter type used as the second argument for the fallback closure.
func myContextFunction(invocation: Invocation<(String), String>) {
  let requestParam = invocation.commandArgs
  // Create HTTP request
  guard let url = URL(string: "http://myserver.net/path/\(requestParam)") else {
    // Something went wrong

    invocation.notifyFailure(error: .encodingURLError)
    return
  }

  var req = URLRequest(url: url)
  req.httpMethod = "GET"
  let session = URLSession.shared

  // Perform the request
  session.dataTask(with: req) { result, res, err in
    guard let result = result else {
      // Failed getting a result from the server

      invocation.notifyFailure(error: .networkingError)
      return
    }

    // Convert results to a JSON object
    guard let json = (try? JSONSerialization.jsonObject(with: result, options: [])) as? [String: Any] else {
      invocation.notifyFailure(error: .jsonDecodingError)
      return
    }
    // Process JSON data

    invocation.notifySuccess()
  }.resume()
}
  • Create a CircuitBreaker instance for each context function (e.g. endpoint) you wish to circuit break. The CircuitBreaker instance must specify a name for the circuit breaker, the endpoint to circuit break and the fallback function. Optional configurations include: group, timeout, resetTimeout, maxFailures, rollingWindow and bulkhead, for further details about these configuration options see the API reference.
let breaker = CircuitBreaker(name: "Circuit1", command: myContextFunction, fallback: myFallback)
  • Invoke the call to the endpoint by calling the CircuitBreaker run() method. You should pass the corresponding arguments for the context command and fallback closures. In this sample, myContextFunction takes a string as its parameter while myFallback takes a string as its second parameter:
let requestParam: String = "myRequestParams"
breaker.run(commandArgs: requestParam, fallbackArgs: "Something went wrong.")

Full Implementation

extension BreakerError {
    public static let encodingURLError = BreakerError(reason: "URL could not be created")
    public static let networkingError = BreakerError(reason: "There was an error, while sending the request")
    public static let jsonDecodingError = BreakerError(reason: "Could not decode result into JSON")
}

func myFallback(err: BreakerError, msg: String) {
    // The fallback will be called if one of the below occurs:
    //  1. The request does not return before the specified timeout
    //  2. CircuitBreaker is currently in Open state and set to fail fast.
    //  3. There was an error in the user's called context function (networking error, etc.)
    Log.verbose("Error: \(err)")
    Log.verbose("Message: \(msg)")
}

func myContextFunction(invocation: Invocation<(String), String>) {
  let requestParam = invocation.commandArgs
  // Create HTTP request
  guard let url = URL(string: "http://mysever.net/path/\(requestParam)") else {
    // Something went wrong...

    invocation.notifyFailure(error: .encodingURLError)
  }

  var req = URLRequest(url: url)
  req.httpMethod = "GET"
  let session = URLSession.shared

  // Perform Request
  session.dataTask(with: req) { result, res, err in
    guard let result = result else {
      // Failed getting a result from the server

      invocation.notifyFailure(error: .networkingError)
      return
    }

    // Convert results to a JSON object
    guard let json = (try? JSONSerialization.jsonObject(with: result, options: [])) as? [String: Any] else {
        invocation.notifyFailure(error: .jsonDecodingError)
        return
    }
    // Process JSON data

    invocation.notifySuccess()
  }.resume()
}

let breaker = CircuitBreaker(name: "Circuit1", command: myContextFunction, fallback: myFallback)

let requestParam: String = "myRequestParams"
breaker.run(commandArgs: requestParam, fallbackArgs: "Something went wrong.")

Statistics

The following statistics will be tracked for the CircuitBreaker instance:

Tracked Statistics
  • Total Requests
  • Concurrent Requests
  • Rejected Requests
  • Successful Responses
  • Average Execution Response Time
  • Average Total Response Time
  • Failed Responses
  • Total Timeouts
  • Total Latency
  • Total Execution Latency
  • Hystrix Compliant Snapshot

The example code below shows how to log a snapshot of the statistics and how to create a Hystrix compliant snapshot.

// Create CircuitBreaker
let breaker = CircuitBreaker(name: "Circuit1", command: myFunction, fallback: myFallback)

// Invoke breaker call
breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: "Something went wrong.")

// Log statistics snapshot
breaker.logSnapshot()

// Hystrix compliant snapshot
let snapshot = breaker.snapshot

Observing statistics

The CircuitBreaker library provides an interface for observing new CircuitBreaker instances in order to register and track statistics changes. In the initialization of a CircuitBreaker instance, the linked monitors are notified of its instantiation allowing them to begin tracking the instance's statistics. The CircuitBreaker instance exposes a Hystrix compliant statistics snapshot to the monitor which can then be processed accordingly. See the API documentation for more information.

API Documentation

For more information visit our API reference.

Community

We love to talk server-side Swift, and Kitura. Join our Slack to meet the team!

License

This Swift package is licensed under Apache 2.0. Full license text is available in LICENSE.

circuitbreaker's People

Contributors

acbodine avatar andrew-lees11 avatar dannys42 avatar djones6 avatar helenmasters avatar ianpartridge avatar kostickm avatar kweinmeister avatar mbarnach avatar rolivieri avatar sandmman avatar shihabmehboob avatar swiftdevops avatar tfrank64 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

circuitbreaker's Issues

Intermittent test failures in Travis

The CircuitBreaker tests appear to have started intermittently failing in Travis. My suspicion is that this is test fragility rather than a real problem, but it ought to be investigated.

Example:
https://api.travis-ci.org/v3/job/461370473/log.txt

Test Case '-[CircuitBreakerTests.CircuitBreakerTests testStateCycle]' started.
/Users/travis/build/IBM-Swift/CircuitBreaker/Tests/CircuitBreakerTests/CircuitBreakerTests.swift:617: error: -[CircuitBreakerTests.CircuitBreakerTests testStateCycle] : XCTAssertEqual failed: ("closed") is not equal to ("open") - 
/Users/travis/build/IBM-Swift/CircuitBreaker/Tests/CircuitBreakerTests/CircuitBreakerTests.swift:620: error: -[CircuitBreakerTests.CircuitBreakerTests testStateCycle] : XCTAssertEqual failed: ("closed") is not equal to ("open") - 
/Users/travis/build/IBM-Swift/CircuitBreaker/Tests/CircuitBreakerTests/CircuitBreakerTests.swift:625: error: -[CircuitBreakerTests.CircuitBreakerTests testStateCycle] : XCTAssertEqual failed: ("closed") is not equal to ("halfopen") - 
Test Case '-[CircuitBreakerTests.CircuitBreakerTests testStateCycle]' failed (1.026 seconds).

Error with fallback signature and/or README documentation

This seems to be a known issue and one that, in theory, has already been addressed (see #16). But I'm still having issues with the signature for, and invocation of, the fallback function.

The README indicates that the fallback functions's signature should match

(<BreakerError, (fallbackArg1, fallbackArg2,...)>) -> Void

(Unless I'm missing something, the angle brackets are a typo in the README.)

and that the run method on CircuitBreaker should be called as follows:

breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: (msg: "Something went wrong."))

However, if I create a fallback with the signature (BreakerError, String) -> Void and try to invoke run as indicated above, I get the following error message:

error: cannot convert value of type '(msg: String)' to expected argument type 'String' 
breaker.run(commandArgs: (a: 12, b: 13), fallbackArgs: (msg: "This is a test"))                                                                                
                                                   ^~~~~~~~~~~~~~~~~~~~~~~                                                                                 
                                                                           as! String  

Further, if I create a fallback with the signature (BreakerError, String, String) -> Void and try to invoke run with breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: (msg, "First Message", msg2: "SecondMessage:)) I receive the following error:

error: cannot convert value of type '(Int, Int) -> Int' to expected argument type '(_) -> (_)'
let breaker = CircuitBreaker(command: myFunc, fallback: myFallback)                                                                                            
                                  ^~~~~~                                                

(Note: the error is with parsing the command, not the fallback.)

I can, however, successfully invoke run as follows:

 breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: "Something went wrong.")

I haven't yet finished working my way through the source, so that might clear up some mis-understanding on my part. Nevertheless, it seems that the README is definitely incorrect.
Also, it would be very useful to actually be able to pass more than one argument to the fallback function.

Update fallback signature to include BreakerError

Currently the fallback function is of type: AnyFallback<C> = (C) -> Void which allows for a variable number of arguments to be passed by the client when calling the fallback function. The goal is to force the first argument in the list to be of type BreakerError to notify the client if the fallback was called because of a timeout or a fastFail.

For some reason making AnyFallback<C> = (BreakerError, C) -> Void will not allow C to have a variable number of arguments. Need to find a way to fix this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.