Package: insanic-infuse
InfuseInfuse is a Python implementation of the Circuit Breaker pattern, described
in Michael T. Nygard’s bookRelease It!.In Nygard’s words,“circuit breakers exists to allow one subsystem to fail
without destroying the entire system. This is done by wrapping dangerous
operations (typically integration points) with a component that can circumvent
calls when the system is not healthy”.This basically extendspybreakerwith support forInsanic.
For full documentation refer topybreaker.
What is different from pybreaker is that it is an asynchronous implementation
with asynchronous storage options.We needed a lot more customizations compared to what the pybreaker was providing.
Especially with async storage options. The whole CircuitBreaker implementation needed
to be fixed for asyncio support.Whats up with the name?Some people might ask why infuse? My basic thought process:Need a name that starts with “in~”“Circuit Breaker” -> Fuse boxinfuse?Featurespybreaker features +aioredis backed storage optionRequirementsinfuse is onlyPython3.4+ (support for asyncio)pybreaker is originally :Python2.7+ (or Python 3.0+)redis if using async redis (aioredis)InstallationRun the following command line to download the latest stable version of
infuse fromPyPI$pipinstallinsanic-infuseUsageUsage of Infuse is different frompybreaker, where everything is done
through the Infuseinit_appmethod.frominsanicimportInsanicfrominfuseimportInfuseapp=Insanic("example",version="0.1.0")Infuse.init_app(app)But, before we go on some explanation of what a circuit breaker does:What Does a Circuit Breaker Do? (frompybreaker)Let’s say you want to use a circuit breaker on a function that updates a row
in thecustomerdatabase table.defupdate_customer(cust):# Do stuff here...pass# Will trigger the circuit breakerupdated_customer=awaitdb_breaker.call(update_customer,my_customer)According to the default parameters, the circuit breakerdb_breakerwill
automaticallyOPENthe circuit after 5 consecutive failures inupdate_customer.When the circuit isOPEN, all calls toupdate_customerwill fail immediately
(raising aCircuitBreakerError) without any attempt to execute the real
operation.After 60 seconds, the circuit breaker will allow the next call toupdate_customerpass through.  This state is calledHALF OPEN.
If that call succeeds, the circuit isCLOSED;
if it fails, however, the circuit isOPENed again until another timeout elapses.Excluding Exceptions(frompybreaker)By default, a failed call is any call that raises an exception. However, it’s
common to raise exceptions to also indicate business exceptions, and those
exceptions should be ignored by the circuit breaker as they don’t indicate
system errors.# At creation time...db_breaker=CircuitBreaker(exclude=[CustomerValidationError])# ...or laterdb_breaker.add_excluded_exception(CustomerValidationError)In that case, when any function guarded by that circuit breaker raisesCustomerValidationError(or any exception derived fromCustomerValidationError), that call won’t be considered a system failure.What does Infuse do?Infuse, when initializing the Insanic applicationSets its own state on the storage as defined inINFUSE_INITIAL_CIRCUIT_STATE.Patches Insanic’s Service object to wrap with circuit breaking.Other than this, there are some configurations you can tweak.
Pretty simple.For more information, please refer to theDocumentation.Release HistoryView release historyhereContributingFor guidance on setting up a development environment and how to make a contribution to Infuse,
see theCONTRIBUTING.rstguidelines.MetaDistributed under the MIT license. SeeLICENSEfor more information.Thanks to all the people at my prior company that worked with me to make this possible.LinksDocumentation:https://infuse.readthedocs.io/en/latest/Releases:https://pypi.org/project/insanic-infuse/Code:https://www.github.com/crazytruth/infuse/Issue Tracker:https://www.github.com/crazytruth/infuse/issuesInsanic Documentation:http://insanic.readthedocs.io/Insanic Repository:https://www.github.com/crazytruth/insanic/