MCPcopy Index your code
hub / github.com/bsm/redeo

github.com/bsm/redeo @v2.2.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.2.1 ↗ · + Follow
365 symbols 1,229 edges 36 files 236 documented · 65%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Redeo

GoDoc Build Status Go Report Card License

The high-performance Swiss Army Knife for building redis-protocol compatible servers/services.

Parts

This repository is organised into multiple components:

  • root package contains the framework for building redis-protocol compatible, high-performance servers.
  • resp implements low-level primitives for dealing with RESP (REdis Serialization Protocol), client and server-side. It contains basic wrappers for readers and writers to read/write requests and responses.
  • client contains a minimalist pooled client.

For full documentation and examples, please see the individual packages and the official API documentation: https://godoc.org/github.com/bsm/redeo.

Examples

A simple server example with two commands:

package main

import (
  "net"

  "github.com/bsm/redeo"
)

func main() {
    srv := redeo.NewServer(nil)

    // Define handlers
    srv.HandleFunc("ping", func(w resp.ResponseWriter, _ *resp.Command) {
        w.AppendInlineString("PONG")
    })
    srv.HandleFunc("info", func(w resp.ResponseWriter, _ *resp.Command) {
        w.AppendBulkString(srv.Info().String())
    })

    // More handlers; demo usage of redeo.WrapperFunc
    srv.Handle("echo", redeo.WrapperFunc(func(c *resp.Command) interface{} {
        if c.ArgN() != 1 {
            return redeo.ErrWrongNumberOfArgs(c.Name)
        }
        return c.Arg(0)
    }))

    // Open a new listener
    lis, err := net.Listen("tcp", ":9736")
    if err != nil {
        panic(err)
    }
    defer lis.Close()

    // Start serving (blocking)
    srv.Serve(lis)
}

More complex handlers:

func main() {
    mu := sync.RWMutex{}
    data := make(map[string]string)
    srv := redeo.NewServer(nil)

    srv.HandleFunc("set", func(w resp.ResponseWriter, c *resp.Command) {
        if c.ArgN() != 2 {
            w.AppendError(redeo.WrongNumberOfArgs(c.Name))
            return
        }

        key := c.Arg(0).String()
        val := c.Arg(1).String()

        mu.Lock()
        data[key] = val
        mu.Unlock()

        w.AppendInt(1)
    })

    srv.HandleFunc("get", func(w resp.ResponseWriter, c *resp.Command) {
        if c.ArgN() != 1 {
            w.AppendError(redeo.WrongNumberOfArgs(c.Name))
            return
        }

        key := c.Arg(0).String()
        mu.RLock()
        val, ok := data[key]
        mu.RUnlock()

        if ok {
            w.AppendBulkString(val)
            return
        }
        w.AppendNil()
    })
}

Redeo also supports command wrappers:

func main() {
    mu := sync.RWMutex{}
    data := make(map[string]string)
    srv := redeo.NewServer(nil)

    srv.Handle("set", redeo.WrapperFunc(func(c *resp.Command) interface{} {
        if c.ArgN() != 2 {
            return redeo.ErrWrongNumberOfArgs(c.Name)
        }

        key := c.Arg(0).String()
        val := c.Arg(1).String()

        mu.Lock()
        data[key] = val
        mu.Unlock()

        return 1
    }))

    srv.Handle("get", redeo.WrapperFunc(func(c *resp.Command) interface{} {
        if c.ArgN() != 1 {
            return redeo.ErrWrongNumberOfArgs(c.Name)
        }

        key := c.Arg(0).String()
        mu.RLock()
        val, ok := data[key]
        mu.RUnlock()

        if ok {
            return val
        }
        return nil
    }))
}

Extension points exported contracts — how you extend this code

Value (Interface)
Value must be exportable as a string [7 implementers]
info/values.go
ResponseReader (Interface)
ResponseReader is used by clients to wrap a server connection and parse responses. [4 implementers]
resp/response.go
Scannable (Interface)
-------------------------------------------------------------------- Scannable interfaces may implement custom Scan beha [2 …
resp/resp.go
Handler (Interface)
-------------------------------------------------------------------- Handler is an abstract handler interface for respon
redeo.go
Conn (Interface)
Conn wraps a single network connection and exposes common read/write methods.
client/conn.go
Callback (FuncType)
-------------------------------------------------------------------- Callback function
info/values.go
CustomResponse (Interface)
CustomResponse values implement custom serialization and can be passed to ResponseWriter.Append. [1 implementers]
resp/response.go
HandlerFunc (FuncType)
HandlerFunc is a callback function, implementing Handler.
redeo.go

Core symbols most depended-on inside this repo

String
called by 76
info/values.go
scanErrf
called by 39
resp/scan.go
Flush
called by 29
resp/response.go
AppendBulkString
called by 26
resp/response.go
PeekType
called by 25
resp/response.go
ArgN
called by 24
resp/command.go
Arg
called by 23
resp/command.go
Flush
called by 19
client/conn.go

Shape

Method 238
Function 73
Struct 31
TypeAlias 10
Interface 9
FuncType 4

Languages

Go100%

Modules by API surface

resp/bufio.go51 symbols
resp/response.go34 symbols
info.go27 symbols
resp/command.go26 symbols
client/conn.go23 symbols
redeo.go21 symbols
resp/request.go20 symbols
info/values.go17 symbols
resp/resp_test.go14 symbols
info/info.go14 symbols
client.go14 symbols
resp/resp.go11 symbols

For agents

$ claude mcp add redeo \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact