uhatools

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 9 Imported by: 1

README

uhatools

GoDoc

Tools for managing Uhaha services. Right now this only has the Uhaha client library.

Open a Cluster

This will define a new cluster pool that will be used for establishing connections to a Uhaha cluster.

cl := uhatools.OpenCluster(uhatools.ClusterOptions{
    InitialServers: []string { 
        "127.0.0.1:11001", // Server 1
        "127.0.0.1:11002", // Server 2
        "127.0.0.1:11003", // Server 3
    },
})

Close the Cluster when you don't need it anymore

cl.Close()

Connect to cluster

Get a connection from the cluster pool. The connection will automatically track the leadership changes. It's api is modeled after the redigo project.

conn := cl.Get()
defer conn.Close() // Always close the connection when you're done

pong, err := uhatools.String(conn.Do("PING"))
if err != nil{
    return err
}
println(pong) // should be PONG

License

uhatools source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("closed")

ErrClosed is returned when a Uhaha connection has been closed.

View Source
var ErrConnectionTimeout = errors.New("connection timeout")

ErrConnectionTimeout is returned when a conenction to a Uhaha server could not be established within the required time.

View Source
var ErrLeadershipTimeout = errors.New("leadership timeout")

ErrLeadershipTimeout is returned when a conenction to a Uhaha leader could not be established within the required time.

View Source
var ErrNil = redis.ErrNil

ErrNil indicates that a reply value is nil.

Functions

func Bool

func Bool(reply interface{}, err error) (bool, error)

Bool is a helper that converts a command reply to a boolean. If err is not equal to nil, then Bool returns false, err. Otherwise Bool converts the reply to boolean as follows:

Reply type      Result
integer         value != 0, nil
bulk string     strconv.ParseBool(reply)
nil             false, ErrNil
other           false, error

func ByteSlices

func ByteSlices(reply interface{}, err error) ([][]byte, error)

ByteSlices is a helper that converts an array command reply to a [][]byte. If err is not equal to nil, then ByteSlices returns nil, err. Nil array items are stay nil. ByteSlices returns an error if an array item is not a bulk string or nil.

func Bytes

func Bytes(reply interface{}, err error) ([]byte, error)

Bytes is a helper that converts a command reply to a slice of bytes. If err is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts the reply to a slice of bytes as follows:

Reply type      Result
bulk string     reply, nil
simple string   []byte(reply), nil
nil             nil, ErrNil
other           nil, error

func Float64

func Float64(reply interface{}, err error) (float64, error)

Float64 is a helper that converts a command reply to 64 bit float. If err is not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts the reply to an int as follows:

Reply type    Result
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Float64s

func Float64s(reply interface{}, err error) ([]float64, error)

Float64s is a helper that converts an array command reply to a []float64. If err is not equal to nil, then Float64s returns nil, err. Nil array items are converted to 0 in the output slice. Floats64 returns an error if an array item is not a bulk string or nil.

func Int

func Int(reply interface{}, err error) (int, error)

Int is a helper that converts a command reply to an integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int converts the reply to an int as follows:

Reply type    Result
integer       int(reply), nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int64

func Int64(reply interface{}, err error) (int64, error)

Int64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int64 returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int64Map

func Int64Map(reply interface{}, err error) (map[string]int64, error)

Int64Map is a helper that converts an array of strings (alternating key, value) into a map[string]int64. The HGETALL commands return replies in this format. Requires an even number of values in result.

func Int64s

func Int64s(reply interface{}, err error) ([]int64, error)

Int64s is a helper that converts an array command reply to a []int64. If err is not equal to nil, then Int64s returns nil, err. Nil array items are stay nil. Int64s returns an error if an array item is not a bulk string or nil.

func IntMap

func IntMap(reply interface{}, err error) (map[string]int, error)

IntMap is a helper that converts an array of strings (alternating key, value) into a map[string]int.

func Ints

func Ints(reply interface{}, err error) ([]int, error)

Ints is a helper that converts an array command reply to a []in. If err is not equal to nil, then Ints returns nil, err. Nil array items are stay nil. Ints returns an error if an array item is not a bulk string or nil.

func String

func String(reply interface{}, err error) (string, error)

String is a helper that converts a command reply to a string. If err is not equal to nil, then String returns "", err. Otherwise String converts the reply to a string as follows:

Reply type      Result
bulk string     string(reply), nil
simple string   reply, nil
nil             "",  ErrNil
other           "",  error

func StringMap

func StringMap(reply interface{}, err error) (map[string]string, error)

StringMap is a helper that converts an array of strings (alternating key, value) into a map[string]string. Requires an even number of values in result.

func Strings

func Strings(reply interface{}, err error) ([]string, error)

Strings is a helper that converts an array command reply to a []string. If err is not equal to nil, then Strings returns nil, err. Nil array items are converted to "" in the output slice. Strings returns an error if an array item is not a bulk string or nil.

func Uint64

func Uint64(reply interface{}, err error) (uint64, error)

Uint64 is a helper that converts a command reply to 64 bit unsigned integer. If err is not equal to nil, then Uint64 returns 0, err. Otherwise, Uint64 converts the reply to an uint64 as follows:

Reply type    Result
+integer      reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Uint64Map

func Uint64Map(reply interface{}, err error) (map[string]uint64, error)

Uint64Map is a helper that converts an array of strings (alternating key, value) into a map[string]uint64. Requires an even number of values in result.

func Uint64s

func Uint64s(reply interface{}, err error) ([]uint64, error)

Uint64s is a helper that converts an array command reply to a []uint64. If err is not equal to nil, then Uint64s returns nil, err. Nil array items are stay nil. Uint64s returns an error if an array item is not a bulk string or nil.

func ValueMap added in v0.4.1

func ValueMap(reply interface{}, err error) (map[string]interface{}, error)

ValueMap is a helper that converts an array of values (alternating key, value) into a map[string]interface{}. Requires an even number of values in result.

func Values

func Values(reply interface{}, err error) ([]interface{}, error)

Values is a helper that converts an array command reply to a []interface{}. If err is not equal to nil, then Values returns nil, err. Otherwise, Values converts the reply as follows:

Reply type      Result
array           reply, nil
nil             nil, ErrNil
other           nil, error

Types

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

Cluster represents a Uhaha connection cluster and pool

func OpenCluster added in v0.3.0

func OpenCluster(opts ClusterOptions) *Cluster

OpenCluster returns a new Uhaha Cluster connection pool

func (*Cluster) Close added in v0.3.0

func (cl *Cluster) Close() error

Close the cluster and pooled connections

func (*Cluster) Get

func (cl *Cluster) Get() *Conn

Get a connection from the Cluster.

type ClusterOptions

type ClusterOptions struct {
	DialOptions                  // The Dial Options for each connection
	InitialServers []string      // The initial cluster server addresses
	AutoCloseStale time.Duration // Auto close conns after time. default: 60s
	PoolSize       int           // Max number of conns in pool. default: 15
}

ClusterOptions are provide to OpenCluster.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents a connection to a Uhaha cluster

func Dial added in v0.2.0

func Dial(addr string, opts *DialOptions) (*Conn, error)

Dial connects to the Uhaha cluster at the given TCP address using the specified options. The addr param can be a single value or a comma-delimited set of addresses, where the set represents the servers in the Uhaha cluster.

func (*Conn) Close added in v0.2.0

func (c *Conn) Close() error

Close a connection

func (*Conn) Do

func (c *Conn) Do(commandName string, args ...interface{}) (reply interface{},
	err error,
)

Do exectes a Uhaha command on the server and returns the reply or an error.

func (*Conn) Downgrade added in v0.5.0

func (c *Conn) Downgrade(requireLeader bool) (redis.Conn, error)

Downgrade returns a "downgraded" connection that is a direct connection using redigo connection. Param "requireLeader" performs a "BARRIER" command to ensure that the returned connection belongs to a leader.

type DialOptions added in v0.2.0

type DialOptions struct {
	Auth              string        // optional
	TLSConfig         *tls.Config   // optional
	ConnectionTimeout time.Duration // default: 5 seconds
	LeadershipTimeout time.Duration // default: 20 seconds
}

DialOptions ...

Jump to

Keyboard shortcuts

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