Documentation
¶
Index ¶
- Variables
- type BreakerOption
- type CircuitBreaker
- type FallbackHook
- func (h *FallbackHook) Close()
- func (h *FallbackHook) DialHook(next redis.DialHook) redis.DialHook
- func (h *FallbackHook) IsDown() bool
- func (h *FallbackHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook
- func (h *FallbackHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook
- type HookOption
- type Item
- type LocalCache
- func (c *LocalCache) Close()
- func (c *LocalCache) Delete(keys ...string) int64
- func (c *LocalCache) Exists(keys ...string) int64
- func (c *LocalCache) Expire(key string, ttl time.Duration) bool
- func (c *LocalCache) Flush()
- func (c *LocalCache) Get(key string) (string, bool)
- func (c *LocalCache) MGet(keys ...string) []interface{}
- func (c *LocalCache) MSet(pairs map[string]string)
- func (c *LocalCache) Set(key, value string, ttl time.Duration)
- func (c *LocalCache) TTL(key string) time.Duration
Constants ¶
This section is empty.
Variables ¶
var ErrDegraded = fmt.Errorf("kvdecorator: service degraded, command not supported locally")
ErrDegraded is returned for commands not supported in local fallback mode.
Functions ¶
This section is empty.
Types ¶
type BreakerOption ¶
type BreakerOption func(*CircuitBreaker)
BreakerOption configures the CircuitBreaker.
func WithDialTimeout ¶
func WithDialTimeout(d time.Duration) BreakerOption
WithDialTimeout sets the TCP dial timeout. Default: 500ms.
func WithProbeInterval ¶
func WithProbeInterval(d time.Duration) BreakerOption
WithProbeInterval sets the interval between TCP probes. Default: 1s.
func WithThreshold ¶
func WithThreshold(n int64) BreakerOption
WithThreshold sets the number of consecutive failures to trip. Default: 3.
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker monitors a remote service via TCP probes and switches to a degraded (open) state when the service is unreachable.
func NewCircuitBreaker ¶
func NewCircuitBreaker(addr string, opts ...BreakerOption) *CircuitBreaker
NewCircuitBreaker creates a new CircuitBreaker for the given address.
func (*CircuitBreaker) IsDown ¶
func (cb *CircuitBreaker) IsDown() bool
IsDown returns true if the remote service is considered unreachable.
func (*CircuitBreaker) Start ¶
func (cb *CircuitBreaker) Start()
Start runs an initial synchronous probe to determine the current state of the remote service, then launches the background probe goroutine. If Redis is unreachable, the breaker trips before Start returns.
func (*CircuitBreaker) Stop ¶
func (cb *CircuitBreaker) Stop()
Stop terminates the background TCP probe goroutine.
type FallbackHook ¶
type FallbackHook struct {
// contains filtered or unexported fields
}
FallbackHook implements redis.Hook. It intercepts all commands and routes them to a local in-memory cache when the remote Redis is unreachable.
func NewFallbackHook ¶
func NewFallbackHook(addr string, opts ...HookOption) *FallbackHook
NewFallbackHook creates a FallbackHook for the given Redis address. The addr should match the Redis server address (e.g. "localhost:6379"). Use AddHook on your redis.Client to install it:
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
hook := kvdecorator.NewFallbackHook("localhost:6379")
defer hook.Close()
rdb.AddHook(hook)
func (*FallbackHook) Close ¶
func (h *FallbackHook) Close()
Close stops the circuit breaker probe and the local cache cleanup goroutine.
func (*FallbackHook) DialHook ¶
func (h *FallbackHook) DialHook(next redis.DialHook) redis.DialHook
DialHook passes through to the next hook.
func (*FallbackHook) IsDown ¶
func (h *FallbackHook) IsDown() bool
IsDown reports whether the remote Redis is currently considered unreachable.
func (*FallbackHook) ProcessHook ¶
func (h *FallbackHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook
ProcessHook intercepts each command. If the breaker is open, the command is handled locally. Otherwise, it is forwarded to Redis and the result is backed up in the local cache.
func (*FallbackHook) ProcessPipelineHook ¶
func (h *FallbackHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook
ProcessPipelineHook intercepts pipeline commands with the same logic.
type HookOption ¶
type HookOption func(*hookConfig)
HookOption configures the FallbackHook.
func WithHookCleanupInterval ¶
func WithHookCleanupInterval(d time.Duration) HookOption
WithHookCleanupInterval sets the local cache cleanup interval. Default: 10s.
func WithHookDialTimeout ¶
func WithHookDialTimeout(d time.Duration) HookOption
WithHookDialTimeout sets the TCP dial timeout. Default: 500ms.
func WithHookProbeInterval ¶
func WithHookProbeInterval(d time.Duration) HookOption
WithHookProbeInterval sets the TCP probe interval. Default: 1s.
func WithHookThreshold ¶
func WithHookThreshold(n int64) HookOption
WithHookThreshold sets the consecutive failure threshold. Default: 3.
type LocalCache ¶
type LocalCache struct {
// contains filtered or unexported fields
}
LocalCache is a concurrent-safe in-memory key-value cache with TTL support.
func NewLocalCache ¶
func NewLocalCache(cleanupInterval time.Duration) *LocalCache
NewLocalCache creates a new LocalCache and starts the background cleanup goroutine. cleanupInterval controls how often expired items are removed.
func (*LocalCache) Close ¶
func (c *LocalCache) Close()
Close stops the background cleanup goroutine.
func (*LocalCache) Delete ¶
func (c *LocalCache) Delete(keys ...string) int64
Delete removes one or more keys. Returns the number of keys that were present.
func (*LocalCache) Exists ¶
func (c *LocalCache) Exists(keys ...string) int64
Exists returns the number of specified keys that exist (and are not expired).
func (*LocalCache) Expire ¶
func (c *LocalCache) Expire(key string, ttl time.Duration) bool
Expire sets a TTL on an existing key. Returns true if the key exists.
func (*LocalCache) Get ¶
func (c *LocalCache) Get(key string) (string, bool)
Get retrieves a value by key. Returns the value and true if found and not expired.
func (*LocalCache) MGet ¶
func (c *LocalCache) MGet(keys ...string) []interface{}
MGet returns the values for the given keys. Missing or expired keys have nil entries.
func (*LocalCache) MSet ¶
func (c *LocalCache) MSet(pairs map[string]string)
MSet stores multiple key-value pairs. keys and values must have the same length.