deepjoy

package module
v0.0.0-...-6d5e5c6 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 12 Imported by: 2

README

Deepjoy

GoDoc Build Status Maintainability Test Coverage

A Go library that wraps gomodule/redigo in a pool. This library is named after Y-40.

Example

First, instantiate a client with any of the following optional config functions. The only required parameter is the address of the remote Redis server - all other values have (hopefully) sane defaults.

client := NewClient(
    "dart.it.corp:6379",
    WithPassword("hunter2"),
    WithDatabase(3),
    WithConnectTimeout(time.Second * 5),
    WithReadTimeout(time.Second * 5),
    WithWriteTimeout(time.Second * 5),
    WithBorrowTimeout(time.Second * 5),
    WithPoolCapacity(10),
    WithBreaker(overcurrent.NewCircuitBreaker()),
    WithLogger(NewLogAdapter()),
)

// Drain the pool
defer client.Close()

Password, database, connect, read, and write timeouts are sent directly to the backing redis library. The borrow timeout setting is used to determine how much time we will spend waiting on an empty pool before returning a no connection error back to the user.

The breaker is an instance of an overcurrent circuit breaker and is invoked when dialing a new redis connection. If dials are failing very rapidly, it is best to back off on the consumer side to let the remote service recover.

A logger is a simple interface with a single Printf function. By default, the logger will call log.Printf. NewNilLogger will return a silent logger. If your application uses a more sophisticated (structured) logging library, a small shim can be created so that it conforms to this protocol.

The client API is otherwise minimal. You can run a redis command, which consists of a single string command and a following variadic list of interfaces composing the command's arguments as follows.

result, err := client.Do("hmset", "myhash", "f1", "Hello", "f2", 14)
if err != nil {
    // handle error
}

// parse interface{} result

In order to run multiple commands in a single round trip, you can use a pipeline. Commands added to the pipeline do not have an effect on the remote server - no network communication is done until the pipeline is run. Piplines are NOT atomic or transactional - it is possible for command in the pipeline to fail and for side-effects from earlier commands to persist. THe result from a pipeline is a slice of interfaces - the result of each command is returned in the order that it was added to the pipeline.

pipeline := client.Pipeline()
pipeline.Add("GET", "foo")
pipeline.Add("GET", "bar")
pipeline.Add("GET", "baz")

results, err := pipeline.Run()
if err != nil {
    // handle error
}

// parse interface{} result

License

Copyright (c) 2017 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoConnection is returned when the borrow timeout elapses.
	ErrNoConnection = errors.New("no connection available in pool")
)
View Source
var NilLogger = NewNilLogger()

NilLogger is a singleton silent logger.

Functions

This section is empty.

Types

type BreakerFunc

type BreakerFunc func(overcurrent.BreakerFunc) error

BreakerFunc bridges the interface between the Call function of an overcurrent breaker and an overcurrent registry.

type Client

type Client = iface.Client

Client is a goroutine-safe, minimal, and pooled Redis client.

func NewClient

func NewClient(addr string, configs ...ConfigFunc) Client

NewClient creates a new Client.

type ConfigFunc

type ConfigFunc func(*clientConfig)

ConfigFunc is a function used to initialize a new client.

func WithBorrowTimeout

func WithBorrowTimeout(timeout time.Duration) ConfigFunc

WithBorrowTimeout sets the maximum time

func WithBreaker

func WithBreaker(breaker overcurrent.CircuitBreaker) ConfigFunc

WithBreaker sets the circuit breaker instance to use around new connections. The default uses a no-op circuit breaker.

func WithBreakerRegistry

func WithBreakerRegistry(registry overcurrent.Registry, name string) ConfigFunc

WithBreakerRegistry sets the overcurrent registry to use and the name of the circuit breaker config tu use around new connections. The default uses a no-op circuit breaker.

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) ConfigFunc

WithConnectTimeout sets the connect timeout for new connections (default is 5 seconds).

func WithDatabase

func WithDatabase(database int) ConfigFunc

WithDatabase sets the database index (default is 0).

func WithDialerFactory

func WithDialerFactory(dialerFactory DialerFactory) ConfigFunc

WithDialerFactory sets the dialer factory to use to create the connection pools. Each factory creates connections to a single unique address.

func WithLogger

func WithLogger(logger Logger) ConfigFunc

WithLogger sets the logger instance (the default will use Go's builtin logging library).

func WithPassword

func WithPassword(password string) ConfigFunc

WithPassword sets the password (default is "").

func WithPoolCapacity

func WithPoolCapacity(capacity int) ConfigFunc

WithPoolCapacity sets the maximum number of concurrent connections that can be in use at once (default is 10).

func WithReadReplicaAddrs

func WithReadReplicaAddrs(addrs ...string) ConfigFunc

WithReadReplicaAddrs sets the addresses of the client returned by client's the ReadReplica() method.

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) ConfigFunc

WithReadTimeout sets the read timeout for all connections in the pool (default is 5 seconds).

func WithRetryBackoff

func WithRetryBackoff(backoff backoff.Backoff) ConfigFunc

WithRetryBackoff sets the circuit backoff prototype to use when retrying a redis command after a non-protocol network error.

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) ConfigFunc

WithWriteTimeout sets the write timeout for all connections in the pool (default is 5 seconds).

type Conn

type Conn = iface.Conn

Conn abstracts a single, feature-minimal connection to Redis.

type DialFunc

type DialFunc func() (Conn, error)

DialFunc creates a connection to Redis or returns an error.

type DialerFactory

type DialerFactory func(addrs []string) DialFunc

DialerFactory creates a DialFunc for the given address.

type Logger

type Logger = iface.Logger

Logger is an interface to the logger the client writes to.

func NewNilLogger

func NewNilLogger() Logger

NewNilLogger creates a silent logger.

func NewPrintLogger

func NewPrintLogger() Logger

NewPrintLogger creates a logger that prints to stdout.

type Pipeline

type Pipeline = iface.Pipeline

Pipeline wraps an ordered sequence of commands to be processed with a single request/response exchange. This reduces bandwidth and latency around communication with the remote server.

type Pool

type Pool = iface.Pool

Pool abstracts a fixed-size Redis connection pool.

func NewPool

func NewPool(
	dialer DialFunc,
	capacity int,
	logger Logger,
	breakerFunc BreakerFunc,
	clock glock.Clock,
) Pool

NewPool creates a pool with initially nil-connections.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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