Giter Site home page Giter Site logo

jahir8a / rxcircuitbreaker-ios Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yaircarreno/rxcircuitbreaker-ios

0.0 0.0 0.0 225 KB

This repository contains an example of the Circuit Breaker pattern implementation in iOS applications.

Ruby 3.21% Swift 96.79%

rxcircuitbreaker-ios's Introduction

RxCircuitBreaker in iOS

This repository contains an example of the Circuit Breaker pattern implementation in iOS applications. You can also find the Android implementation at RxCircuitBreaker-Android

Articles

States Diagram

Circuit Breaker Pattern

Implementation

We have CircuitBreaker as a main component in the pattern, like this:

class CircuitBreaker {

    ...

    init(name: String, localPersistence: LocalPersistence) {
        ...
    }

    public func callService(_ service: Single<Any>) -> Single<Any> {
        switch self.status {
        case .open:
            return openActions()
        case .closed, .halfOpen:
            return closeActions(service)
        }
    }
}

In this example, I have combined the two events (reset - success).

You also have the Rx wrapper for the service:

private func service(_ tySimulation: String) -> Single<Any> {
    return Single<Any>.create { emitter in
        self.functions.httpsCallable("simulateResponses?typeSimulation=" + tySimulation)
            .call() { (result, error) in
                if let error = error as NSError? {
                    emitter(.failure(error))
                }
                emitter(.success(result?.data ?? ""))
            }
        return Disposables.create()
    }
}

Calling from the client, you could use something like this:

private func callServiceWithCircuitBreaker(tySimulation: String) {
    ...
    let circuitBreaker = circuitBreakersManager.getCircuitBreaker("cicuit-1", userDefaultsStorage)
    CircuitBreakersManager
        .callWithCircuitBreaker(service(tySimulation), circuitBreaker)
        .subscribe(on: serialScheduler)
        .observe(on: MainScheduler.instance)
        .subscribe(onSuccess: { result in
            self.logs(result as! String, circuitBreaker.status, true)
            ...
        } ,
        onFailure: { error in
            self.logs(error.localizedDescription,circuitBreaker.status, false)
            ...
        })
        .disposed(by: disposeBag)
}

Demo

Circuit Breaker Pattern

Versions of IDEs and technologies used.

  • Xcode 12.3 - Swift 5
  • RxSwift 6
  • Storyboards
  • Cocoapods

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.