Documentation ¶
Overview ¶
Package redisconn implements integration with a Redis connection pool.
Usage as a server module:
func main() { modules := []module.Module{ redisconn.NewModuleFromFlags(), } server.Main(nil, modules, func(srv *server.Server) error { srv.Routes.GET("/", ..., func(c *router.Context) { conn, err := redisconn.Get(c.Context) if err != nil { // handle error } defer conn.Close() // use Redis API via `conn` }) return nil }) }
When used that way, Redis is also installed as the default implementation of caching.BlobCache (which basically speeds up various internal guts of the LUCI server framework).
Can also be used as a low-level Redis connection pool library, see NewPool(...)
Index ¶
- Variables
- func Get(ctx context.Context) (redis.Conn, error)
- func GetPool(ctx context.Context) *redis.Pool
- func NewModule(opts *ModuleOptions) module.Module
- func NewModuleFromFlags() module.Module
- func NewPool(addr string, db int) *redis.Pool
- func ReportStats(ctx context.Context, pool *redis.Pool, name string)
- func UsePool(ctx context.Context, pool *redis.Pool) context.Context
- type ModuleOptions
Constants ¶
This section is empty.
Variables ¶
var ErrNotConfigured = errors.New("Redis connection pool is not configured")
ErrNotConfigured is returned by Get if the context has no Redis pool inside.
var ModuleName = module.RegisterName("go.chromium.org/luci/server/redisconn")
ModuleName can be used to refer to this module when declaring dependencies.
Functions ¶
func Get ¶
Get returns a Redis connection using the pool installed in the context.
May block until such connection is available. Returns an error if the context expires before that. The returned connection itself is not associated with the context and can outlive it.
The connection MUST be explicitly closed as soon as it's no longer needed, otherwise leaks and slow downs are eminent.
func NewModule ¶
func NewModule(opts *ModuleOptions) module.Module
NewModule returns a server module that adds a Redis connection pool to the global server context and installs Redis as the default caching.BlobCache implementation.
The Redis connection pool can be used through redisconn.Get(ctx).
Does nothing if RedisAddr options is unset. In this case redisconn.Get(ctx) returns ErrNotConfigured.
func NewModuleFromFlags ¶
NewModuleFromFlags is a variant of NewModule that initializes options through command line flags.
Calling this function registers flags in flag.CommandLine. They are usually parsed in server.Main(...).
func NewPool ¶
NewPool returns a new pool configured with default parameters.
"addr" is TCP "host:port" of a Redis server to connect to. No actual connection is established yet (this happens first time the pool is used).
"db" is a index of a logical DB to SELECT in the connection by default, see https://redis.io/commands/select. It can be used as a weak form of namespacing. It is easy to bypass though, so please do not depend on it for anything critical (better to setup multiple Redis instances in this case).
Doesn't use any authentication or encryption.
func ReportStats ¶
ReportStats reports the connection pool stats as tsmon metrics.
For best results should be called once a minute or right before tsmon flush.
"name" is used as "pool" metric field, to distinguish pools between each other.
Types ¶
type ModuleOptions ¶
type ModuleOptions struct { RedisAddr string // Redis server to connect to as "host:port" RedisDB int // index of a logical Redis DB to use by default }
ModuleOptions contain configuration of the Redis server module.
func (*ModuleOptions) Register ¶
func (o *ModuleOptions) Register(f *flag.FlagSet)
Register registers the command line flags.