backends

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Circuitry Backends

This package provides a built-in in-memory backend for use with Circuitry, which can be useful for testing or proofs of concept.

It also has other sub-packages which provide implementations for:

  • Redis
  • DynamoDB

Quickstart

import (
    "context"
    "fmt"

    "github.com/google/uuid"
    "github.com/sigmavirus24/circuitry"
    "github.com/sigmavirus24/circuitry/backends"
)

func NameGenerator(name string, breakerContext map[string]any) string {
    workID := breakerContext["work_uuid"]
    return fmt.Sprintf("%s::%s", name, workID)
}

func BreakerFactory() *circuitry.CircuitBreakerFactory {
    settings, err := circuitry.NewFactorySettings(
        backends.WithInMemoryBackend(),
        circuitry.WithNameFunc
        // Additional options
    )
    return circuitry.NewCircuitBreakerFactory(settings)
}

func Work() (any, error) {
    // Function with the work you want to protect with the circuit breaker
}

func main() {
    ctx := context.Background()
    factory := BreakerFactory()
    breakerContext := map[string]string{
        "work_uuid": uuid.NewString(),
        // Additional context relevant
    }
    breaker := factory.BreakerFor("example-breaker", breakerContext)
    workResult, workErr, breakerErr := breaker.Execute(ctx, Work)
    if breakerErr != nil {
        fmt.Printf("could not execute work in breaker: %v\n", breakerErr)
        return
    }
    if workErr != nil {
        fmt.Printf("work failed: %s\n", workErr)
        state, err := breaker.State(ctx)
        if err != nil {
            fmt.Printf("cannot display breaker state: %s\n", err)
            return
        }
        fmt.Printf("breaker status: %s\n", state)
        return
    }
    fmt.Printf("finished work")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInMemoryBackend

func NewInMemoryBackend() circuitry.StorageBackender

NewInMemoryBackend creates a new in memory storage backend

func WithInMemoryBackend

func WithInMemoryBackend() circuitry.SettingsOption

WithInMemoryBackend creates an in memory backend storage for a circuit breaker

Types

type InMemoryBackend

type InMemoryBackend struct {
	// contains filtered or unexported fields
}

InMemoryBackend defines an in memory backend designed to be used primarily for proofs of concept and testing

func (*InMemoryBackend) Lock

func (b *InMemoryBackend) Lock(_ context.Context, name string) (sync.Locker, error)

Lock provides a lock around the given circuit state to protect the atomicity of the data

func (*InMemoryBackend) Retrieve

Retrieve fetches the desired circuit state from memory

func (*InMemoryBackend) Store

Store saves the circuit information in memory

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL