Categorygithub.com/dolab/redis-go
modulepackage
1.6.4
Repository: https://github.com/dolab/redis-go.git
Documentation: pkg.go.dev

# README

redis-go CircleCI Go Report Card GoDoc

Go package providing tools for building redis clients, servers and middleware.

Motivation

While there's already good client support for Redis in Go, when it comes to building middleware (which require server components) the landscape of options shrinks dramatically. The existing client libraries also have limitations when it comes to supporting newer Go features (like context.Context) and each of them adopts a different design, making it harder to integrate with other components of a system.
On the other hand, the standard net/http package has proven to have a simple, and still extensible design, making it possible to leverage composition to build software that is easier to develop, maintain and evolve.
This is where the redis-go package comes into play, it follows the same design than the standard net/http package while offering both client and server-side abstractions to build Redis-compatible software.

Client

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/dolab/redis-go"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    // Use the default client which is configured to connect to the redis server
    // running at localhost:6379.
    if err := redis.Exec(ctx, "SET", "hello", "world"); err != nil {
        fmt.Println(err)
    }

    // Passing request or response arguments is done by consuming the stream of
    // values.
    var args = redis.Query(ctx, "GET", "hello")
    var value string

    if args.Next(&value) {
        fmt.Println(value)
    }

    if err := args.Close(); err != nil {
        fmt.Println(err)
    }
}

Server

package main

import (
    "github.com/dolab/redis-go"
)

func main() {
    // Starts a new server speaking the redis protocol, the server automatically
    // handle asynchronusly pipelining the requests and responses.
    redis.ListenAndServe(":6380", redis.HandlerFunc(func(res redis.ResponseWriter, req *redis.Request) {
        // Put the response in streaming mode, will send 3 values.
        res.WriteStream(3)

        // The response writer automatically encodes Go values into their RESP
        // representation.
        res.Write(1)
        res.Write(2)
        res.Write(3)
    }))
}

Metrics

package main

import (
    "net/http"

    "github.com/prometheus/client_golang/prometheus"

    "github.com/dolab/redis-go"
)

func main() {
    // Starts a new http server async for prom.
    go http.ListenAndServe(":8080", http.HandlerFunc(redis.ServeMetrics))

    // Starts a new counter for local usage.
    counter := redis.NewCounterVec("handler", "custom handler counter", []string{"cmd"})

    // Starts a new server speaking the redis protocol, the server automatically
    // handle asynchronusly pipelining the requests and responses.

    redis.ListenAndServe(":6380", redis.HandlerFunc(func(res redis.ResponseWriter, req *redis.Request) {
        for _, cmd := range req.Cmds {
            counter.With(prometheus.Labels{
                "cmd": cmd.Cmd,
            }).Inc()
        }

        // Put the response in streaming mode, will send 3 values.
        res.WriteStream(3)

        // The response writer automatically encodes Go values into their RESP
        // representation.
        res.Write(1)
        res.Write(2)
        res.Write(3)
    }))
}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Dial connects to the redis server at the given address, returning a new client redis connection.
Dial connects to the redis server at the given address, returning a new client redis connection.
Exec is a wrapper around DefaultClient.Exec.
Int parses an integer value from the list of arguments and closes it, returning an error if no integer could not be read.
Int64 parses a 64 bits integer value from the list of arguments and closes it, returning an error if no integer could not be read.
List creates an argument list from a sequence of values.
ListenAndServe listens on the network address addr and then calls Serve with handler to handle requests on incoming connections.
MultiArgs returns an Args value that produces values sequentially from all of the given argument lists.
NewClientConn creates a new redis connection from an already open client connections.
NewCounterVec returns a *prometheus.CounterVec for handler usage.
NewGaugeVec returns a *prometheus.GaugeVec for handler usage.
No description provided by the author
NewHistogramVec returns a *prometheus.HistogramVec for handler usage.
NewRequest returns a new Request, given an address, command, and list of arguments.
NewServerConn creates a new redis connection from an already open server connections.
NewSubConn creates a new SubConn from a pre-existing network connection.
ParseArgs reads a list of arguments into a sequence of destination pointers and closes it, returning any error that occurred while parsing the values.
Query is a wrapper around DefaultClient.Query.
Serve accepts incoming Redis connections on the listener l, creating a new service goroutine for each.
ServeMetrics exports prometheus metrics of internal server.
String parses a string value from the list of arguments and closes it, returning an error if no string could not be read.

# Variables

DefaultClient is the default client and is used by Exec and Query.
DefaultDialer is the default dialer used by Transports when no DialContext is set.
DefaultTransport is the default implementation of Transport and is used by DefaultClient.
ErrDiscard is the error returned to indicate that transactions are discarded.
No description provided by the author
No description provided by the author
ErrServerClosed is returned by Server.Serve when the server is closed.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

A Client is a Redis client.
A Command represent a Redis command used withing a Request.
CommandReader is a type produced by the Conn.ReadCommands method to read a single command or a sequence of commands belonging to the same transaction.
Conn is a low-level API to represent client connections to redis.
A Request represents a Redis request received by a server or to be sent by a client.
Response represents the response from a Redis request.
ReverseProxy is the implementation of a redis reverse proxy.
A Server defines parameters for running a Redis server.
A ServerEndpoint represents a single backend redis server.
SubConn represents a redis connection that has been switched to PUB/SUB mode.
Transport is an implementation of RoundTripper.

# Interfaces

Args represents a list of arguments in Redis requests and responses.
A Commander responds to a Redis CMD.
The Flusher interface is implemented by ResponseWriters that allow a Redis handler to flush buffered data to the client.
A Handler responds to a Redis request.
The Hijacker interface is implemented by ResponseWriters that allow a Redis handler to take over the connection.
No description provided by the author
A ResponseWriter interface is used by a Redis handler to construct an Redis response.
RoundTripper is an interface representing the ability to execute a single Redis transaction, obtaining the Response for a given Request.
ServerBlacklist is implemented by some ServerRegistry to support black listing some server addresses.
The ServerCommander interface is an abstraction used to extend redis command.
The ServerHandler interface is an abstraction used to extend redis handler.
The ServerRegistry interface is an abstraction used to expose a (potentially changing) list of backend redis servers.
The ServerRing interface is an abstraction used to pick a backend redis server for key among servers.
TxArgs is an interface implemented by types that produce the sequence of argument list in response to a transaction.

# Type aliases

The CommanderFunc type is an adapter to allow the use of ordinary functions as redis commanders.
The HandlerFunc type is an adapter to allow the use of ordinary functions as Redis handlers.
A ServerList represents a list of backend redis servers.
A ServerRingFunc satisfies the ServerRing interface of custom hashing func.