Documentation ¶
Index ¶
- Constants
- Variables
- func CloseError(err error, name string) error
- func ConnectError(err error, name string) error
- func DialError(err error, name string) error
- func DisabledError(err error, name string) error
- func DoError(err error, name string) error
- func NoTenantIDError(err error, name string) error
- func SendError(err error, name string) error
- type CachedReader
- type Client
- type ClientContext
- type CountRefresh
- type CountResource
- func (cr *CountResource) Available(ctx context.Context, tenantID string) (int64, error)
- func (cr *CountResource) Consume(ctx context.Context, tenantID string) error
- func (cr *CountResource) GetLimit() int64
- func (cr *CountResource) Limited(ctx context.Context, tenantID string) bool
- func (cr *CountResource) Log() Logger
- func (cr *CountResource) Name() string
- func (cr *CountResource) ReadOnlyAvailable(ctx context.Context, tenantID string) (int64, error)
- func (cr *CountResource) Return(ctx context.Context, tenantID string) error
- type CountResourceOption
- type GobSerialiser
- type GorillaStore
- func (s *GorillaStore) Close() error
- func (s *GorillaStore) Get(r *http.Request, name string) (*sessions.Session, error)
- func (s *GorillaStore) KeyGen(f KeyGenFunc)
- func (s *GorillaStore) KeyPrefix(keyPrefix string)
- func (s *GorillaStore) New(r *http.Request, name string) (*sessions.Session, error)
- func (s *GorillaStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
- func (s *GorillaStore) Serialiser(ss SessionSerialiser)
- type HashCache
- type JsonResource
- func (r *JsonResource) Get(ctx context.Context, tenantID string, target any) error
- func (r *JsonResource) Key(tenantID string) string
- func (r *JsonResource) Log() Logger
- func (r *JsonResource) Name() string
- func (r *JsonResource) Set(ctx context.Context, tenantID string, value any) error
- func (r *JsonResource) URL() string
- type KeyGenFunc
- type Logger
- type Monotonic
- func (c *Monotonic) Del(ctx context.Context, tenantIDOrWallet string) error
- func (c *Monotonic) IncrN(ctx context.Context, tenantIDOrWallet string, n int64) (int64, error)
- func (c *Monotonic) Log() Logger
- func (c *Monotonic) Name() string
- func (c *Monotonic) SetGT(ctx context.Context, tenantIDOrWallet string, cas int64) (int64, error)
- func (c *Monotonic) SetRefresher(refresh CountRefresh) CountRefresh
- func (c *Monotonic) URL() string
- type MonotonicOption
- type RedisClient
- type RedisConfig
- type Resource
- type ResourceCounter
- type ResourceLimiter
- type ResourceOption
- type ScriptRunner
- type Scripter
- type ScripterClient
- type SessionSerialiser
- type SizeResource
Constants ¶
const ( //nolint:gosec RedisClusterPassordEnvFileSuffix = "REDIS_STORE_PASSWORD_FILENAME" RedisClusterSizeEnvSuffix = "REDIS_CLUSTER_SIZE" RedisNamespaceEnvSuffix = "REDIS_KEY_NAMESPACE" RedisNodeAddressFmtSuffix = "REDIS_NODE%d_STORE_ADDRESS" RedisNodeAddressSuffix = "REDIS_STORE_ADDRESS" RedisDBSuffix = "REDIS_STORE_DB" RedisPasswordSuffix = "AZURE_REDIS_STORE_PASSWORD_FILENAME" )
Variables ¶
var ( ErrNoTenantID = errors.New("no tenant ID supplied") ErrRedisClose = errors.New("redis close error") ErrRedisConnect = errors.New("redis connect error") ErrRedisCounterDisabled = errors.New("redis counter disabled") ErrRedisDial = errors.New("redis dial error") ErrRedisDo = errors.New("redis do error") ErrRedisSend = errors.New("redis send error") )
var (
ErrUnknownRedisOperation = errors.New("unknown redis operation")
)
Functions ¶
func CloseError ¶
func ConnectError ¶
func DisabledError ¶
func NoTenantIDError ¶
Types ¶
type CachedReader ¶
type Client ¶
type Client interface { // Do executes a Do command to redis Do(ctx context.Context, args ...any) *redis.Cmd Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd Close() error }
defines a redis client
type ClientContext ¶
type ClientContext struct {
// contains filtered or unexported fields
}
type CountResource ¶
type CountResource struct { *Resource // contains filtered or unexported fields }
CountResource - the counter is initialised with a maximum value and as resources are created the counter is decremented. Additionally when the counter reaches zero the actual number of resources is counted and the current count adjusted accordingly.
func NewResource ¶
func NewResource( cfg RedisConfig, name string, opt CountResourceOption, opts ...ResourceOption, ) *CountResource
NewResource - creates pool of connections to redis that manages a decrementing counter. If limit is less than zero then the counter is disabled and all methods are noops.
func NewResourceWithMockedRedis ¶
func NewResourceWithMockedRedis( name string, log Logger, opt CountResourceOption, opts ...ResourceOption, ) (*CountResource, *mockClient)
func (*CountResource) Available ¶
Available gets current value of counter and adjusts limit if required. An error is returned when either the redis counter cannot be read or this counter is disabled.
func (*CountResource) Consume ¶
func (cr *CountResource) Consume(ctx context.Context, tenantID string) error
Consume subtracts 1 from counter
func (*CountResource) GetLimit ¶ added in v0.13.5
func (cr *CountResource) GetLimit() int64
GetLimit gets the current limit
func (*CountResource) Limited ¶
func (cr *CountResource) Limited(ctx context.Context, tenantID string) bool
Limited returns true if the limit counter is enabled. The current limit is retrieved from upstream if necessary using the Limiter method. This happens when the current TTL parameters are exceeded.
func (*CountResource) Log ¶ added in v0.16.2
func (cr *CountResource) Log() Logger
func (*CountResource) Name ¶
func (cr *CountResource) Name() string
func (*CountResource) ReadOnlyAvailable ¶
ReadOnlyAvailable
Returns the number of available units of this resource. Attempts the fast path first, of getting from the cache directly. Will attempt to re-initialise the count if its inaccessible or 0.
type CountResourceOption ¶
type CountResourceOption func(*CountResource)
func WithResourceCounter ¶
func WithResourceCounter(resourceCounter ResourceCounter) CountResourceOption
type GobSerialiser ¶
type GobSerialiser struct{}
Gob serialiser
func (GobSerialiser) Deserialise ¶
func (gs GobSerialiser) Deserialise(d []byte, s *sessions.Session) error
type GorillaStore ¶
type GorillaStore struct { // Client to connect to redis Client RedisClient Codecs []securecookie.Codec // default Options to use when a new session is created Options sessions.Options MaxLength int // contains filtered or unexported fields }
GorillaStore stores gorilla sessions in Redis
func NewRedisStore ¶
func NewRedisStore(cfg RedisConfig, keyPairs ...[]byte) (*GorillaStore, error)
NewRedisStore returns a new RedisStore with default configuration
func (*GorillaStore) Get ¶
Get returns a session for the given name after adding it to the registry.
func (*GorillaStore) KeyGen ¶
func (s *GorillaStore) KeyGen(f KeyGenFunc)
KeyGen sets the key generator function
func (*GorillaStore) KeyPrefix ¶
func (s *GorillaStore) KeyPrefix(keyPrefix string)
KeyPrefix sets the key prefix to store session in Redis
func (*GorillaStore) New ¶
New returns a session for the given name without adding it to the registry.
func (*GorillaStore) Save ¶
func (s *GorillaStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
Save adds a single session to the response.
If the Options.MaxAge of the session is <= 0 then the session file will be deleted from the store. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.
func (*GorillaStore) Serialiser ¶
func (s *GorillaStore) Serialiser(ss SessionSerialiser)
Serialiser sets the session serialiser to store session
type HashCache ¶
type HashCache struct { Cfg RedisConfig // contains filtered or unexported fields }
HashCache uses redis optimistic locking and hash store to cache strings safely and prevents race conditions between Get/Set and Delete https://redis.io/topics/transactions#optimistic-locking-using-check-and-set
func NewHashCache ¶
func NewHashCache(cfg RedisConfig, expiryTimeoutSeconds int64) (*HashCache, error)
func (*HashCache) CachedRead ¶
func (c *HashCache) CachedRead(ctx context.Context, results any, name, field string, reader CachedReader) error
CachedRead returns the results from the cache if available. Otherwise the reader is invoked and its results are returned instead. The results from the reader, if invoked, are cached. This process is transactional. if the (name, field) is modified on another connection during the span from read to set, the set is not applied.
A note on err handling:
- If we fail to read the value from the cache (for any reason) the reader callback is invoked once (and only once) regardless of any other error.
- if the reader returns an err that is *always* returned to the caller. this is important as otherwise there is no way to distinguish between a failed read and an empty response from the cache or the db
- errors writing to or fetching from the cache are treated as transient and are logged but not returned (persistent cache read/write errors will surface as performance issues or log spam)
- errors marshalling or unmarshaling values for the cache are returned to the caller provided a reader error has not occurred.
type JsonResource ¶ added in v0.13.9
type JsonResource struct { ClientContext // contains filtered or unexported fields }
JsonResource is a Redis resource holding an object in JSON
func NewJsonResource ¶ added in v0.13.9
func NewJsonResource(name string, cfg RedisConfig, resType string) (*JsonResource, error)
NewJsonResource constructs a new instance of JsonResource, given the configuration.
- name: name of the resource
- resType: the general type of resource, mostly to help with organisation
- cfg: the Redis cluster configuration to use
func (*JsonResource) Get ¶ added in v0.13.9
Get reads the resource for the given tenantID, and unmarshals it into target (which must be a pointer to a suitable empty struct.)
func (*JsonResource) Key ¶ added in v0.13.9
func (r *JsonResource) Key(tenantID string) string
Key gets the full resource identification key
func (*JsonResource) Log ¶ added in v0.16.2
func (r *JsonResource) Log() Logger
func (*JsonResource) Name ¶ added in v0.13.9
func (r *JsonResource) Name() string
Name gets the name of the resource
func (*JsonResource) Set ¶ added in v0.13.9
Set takes a JSON-serializable value, marshals it, and stores it for the given tenantID
func (*JsonResource) URL ¶ added in v0.13.9
func (r *JsonResource) URL() string
URL gets the configured Redis URL
type KeyGenFunc ¶
KeyGenFunc defines a function used by store to generate a key
type Monotonic ¶
type Monotonic struct { ClientContext // contains filtered or unexported fields }
func NewMonotonic ¶
func NewMonotonic( cfg RedisConfig, name string, opts ...MonotonicOption, ) Monotonic
func (*Monotonic) SetRefresher ¶
func (c *Monotonic) SetRefresher(refresh CountRefresh) CountRefresh
type MonotonicOption ¶
type MonotonicOption func(*Monotonic)
type RedisClient ¶
type RedisClient interface { Do(ctx context.Context, args ...any) *redis.Cmd Del(ctx context.Context, keys ...string) *redis.IntCmd Ping(ctx context.Context) *redis.StatusCmd Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd Get(ctx context.Context, key string) *redis.StringCmd Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) error Close() error Scripter }
func NewRedisClient ¶
func NewRedisClient(cfg RedisConfig) (RedisClient, error)
type RedisConfig ¶
type RedisConfig interface { GetClusterOptions() (*redis.ClusterOptions, error) GetOptions() (*redis.Options, error) Namespace() string IsCluster() bool URL() string Log() Logger }
func FromEnvOrFatal ¶
func FromEnvOrFatal(log Logger) RedisConfig
ReadClusterConfigOrFatal assumes conventional service env vars and populates a ClusterConfig or Fatals out
type Resource ¶
type Resource struct { ClientContext // contains filtered or unexported fields }
func (*Resource) Limited ¶
Limited returns true if the limit is enabled. The current limit is retrieved from upstream if necessary using the Limiter method.
func (*Resource) RefreshLimit ¶ added in v0.18.2
RefreshLimit gets the limit for a given tenant from upstream.
type ResourceCounter ¶
ResourceCounter is a function which accepts a context and counts the number of reources in the database.
type ResourceLimiter ¶
ResourceLimiter is a function which accepts a resource and retrieves the latest caps limit setting from upstream.
type ResourceOption ¶
type ResourceOption func(*Resource)
func WithRefreshCount ¶
func WithRefreshCount(refreshCount int64) ResourceOption
func WithRefreshTTL ¶
func WithRefreshTTL(refreshTTL time.Duration) ResourceOption
func WithResourceLimiter ¶
func WithResourceLimiter(resourceLimiter ResourceLimiter) ResourceOption
type ScriptRunner ¶
type Scripter ¶
type Scripter interface { Eval(ctx context.Context, script string, keys []string, args ...any) *redis.Cmd EvalSha(ctx context.Context, sha1 string, keys []string, args ...any) *redis.Cmd ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd ScriptLoad(ctx context.Context, script string) *redis.StringCmd }
type ScripterClient ¶
type SessionSerialiser ¶
type SessionSerialiser interface { Serialise(s *sessions.Session) ([]byte, error) Deserialise(b []byte, s *sessions.Session) error }
SessionSerialiser provides an interface for serialise/deserialise a session
type SizeResource ¶
type SizeResource struct {
*Resource
}
func NewSizeResource ¶
func NewSizeResource( cfg RedisConfig, name string, opts ...ResourceOption, ) (*SizeResource, error)
If limit is less than zero then the limit is disabled and all methods are noops.