redis

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: 8 Imported by: 0

README

Redis Backend for Circuitry

This provides a StorageBackender implementation for circuitry that uses Redis as the backend.

Note: This requires github.com/redis/go-redis/v9

Usage

import (
    "fmt"
    "time"

    "github.com/bsm/redislock"
    "github.com/redis/go-redis/v9"
    "github.com/sigmavirus24/circuitry"
    redisbackend "github.com/sigmavirus24/circuitry/backends/redis"
)

func main() {
    settings, err := circuitry.NewFactorySettings(
        redisbackend.WithRedisBackend(
            &redis.Options{
                // See also https://pkg.go.dev/github.com/redis/go-redis/v9#readme-quickstart
                // and https://pkg.go.dev/github.com/redis/go-redis/v9#Options
                Addr: "localhost:6379",
                Password: "",
                DB: 0,
            },
            &redislock.Options{
                RetryStrategy: redislock.NoRetry(),
            },
            1 * time.Hour,
        ),
        circuitry.WithDefaultNameFunc(),
        circuitry.WithDefaultTripFunc(),
        circuitry.WithDefaultFallbackErrorMatcher(),
    )
    if err != nil {
        fmt.Printf("could not create settings: %v\n", err)
        return
    }
    factory := circuitry.NewCircuitBreakerFactory(settings)
    breaker := factory.BreakerFor("my-name", map[string]any{})
    ctx := context.Background()
    err = breaker.Start(ctx)
    if err != nil {
        fmt.Printf("could not start circuit breaker: %v\n", err)
    }
    defer breaker.End(ctx, nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(clientOpts *redis.Options, lockOpts *redislock.Options, defaultLockTTL time.Duration) circuitry.StorageBackender

New builds a new StorageBackender for circuitry.

func WithRedisBackend

func WithRedisBackend(clientOpts *redis.Options, lockOpts *redislock.Options, defaultLockTTL time.Duration) circuitry.SettingsOption

WithRedisBackend provides a way to configure the StorageBackend for a Circuit Breaker Factory's esttings.

Types

type Backend

type Backend struct {
	Client         Client
	Locker         Locker
	LockOpts       *redislock.Options
	DefaultLockTTL time.Duration
}

Backend implements the StorageBackender interface for Redis using redis/go-redis/v9 and bsm/redislock

func (*Backend) Lock

func (c *Backend) Lock(ctx context.Context, name string) (sync.Locker, error)

Lock builds a lock in Redis with the DefaultTTL and returns an interface that matches sync.Locker

func (*Backend) Retrieve

func (c *Backend) Retrieve(ctx context.Context, name string) (circuitry.CircuitInformation, error)

Retrieve looks up the key in Redis and returns the value after deserializing it from JSON. If the key is not present in Redis, this will return an empty circuitry.CircuitInformation

func (*Backend) Store

Store saves the CircuitInformation in Redis under the named key after seriailizing it to JSON.

type Client

type Client interface {
	Get(context.Context, string) *redis.StringCmd
	Set(context.Context, string, any, time.Duration) *redis.StatusCmd
	SetArgs(context.Context, string, any, redis.SetArgs) *redis.StatusCmd
	redis.Scripter // Need this interface for redislock
}

Client describes the interface expected for this backend to function appropriately

type Locker

type Locker interface {
	Obtain(context.Context, string, time.Duration, *redislock.Options) (*redislock.Lock, error)
}

Locker describes the interface expected for this backend to function appropriately

Jump to

Keyboard shortcuts

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