MCPcopy Index your code
hub / github.com/bitleak/go-redis-pool

github.com/bitleak/go-redis-pool @v3.3.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.3.2 ↗ · + Follow
269 symbols 859 edges 17 files 15 documented · 6%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

go-redis-pool

Build Status Go Report Card Coverage Status GitHub release GitHub release date LICENSE GoDoc

go-redis-pool was designed to implement the read/write split in Redis master-slave mode, and easy way to sharding the data.

Installation

go-redis-pool requires a Go version with Modules support and uses import versioning. So please make sure to initialize a Go module before installing go-redis-pool:

go mod init github.com/my/repo
go get github.com/bitleak/go-redis-pool/v3

Quick Start

API documentation and examples are available via godoc

Setup The Master-Slave Pool

pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",
        "127.0.0.1:6381",
    },
    Password: "", // set master password
    ReadonlyPassword: "", // use password if no set
})

pool.Set(ctx, "foo", "bar", 0)

The read-only commands would go throught slaves and write commands would into the master instance. We use the Round-Robin as default when determing which slave to serve the readonly request, and currently supports:

  • RoundRobin (default)
  • Random
  • Weight

For example, we change the distribution type to Weight:

pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",  // default weight is 100 if missing
        "127.0.0.1:6381:200", // weight is 200
        "127.0.0.1:6382:300", // weigght is 300
    },
    PollType: pool.PollByWeight,
})

The first slave would serve 1/6 reqeusts, and second slave would serve 2/6, last one would serve 3/6.

Auto Eject The Failure Host
pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",  // default weight is 100 if missing
        "127.0.0.1:6381:200", // weight is 200
        "127.0.0.1:6382:300", // weigght is 300
    },
    AutoEjectHost: true,
    ServerFailureLimit: 3,
    ServerRetryTimeout: 5 * time.Second,
    MinServerNum: 2,
})

The pool would evict the host if reached ServerFailureLimit times of failure and retry the host after ServerRetryTimeout. The MinServerNum was used to avoid evicting too many and would overrun other alive servers.

Setup The Sharding Pool

pool, err := pool.NewShard(&pool.ShardConfig{
    Shards: []*HAConfig {
        // shard 1
        &pool.HAConfig{
            Master: "127.0.0.1:6379",
            Slaves: []string{
                "127.0.0.1:6380",
                "127.0.0.1:6381",
            },
            DistributeType: pool.DistributeByModular,
            HashFn: hashkit.Xxh3,
        },
        // shard 2
        &pool.HAConfig{
            Master: "127.0.0.1:6382",
            Slaves: []string{
                "127.0.0.1:6383",
            },
            DistributeType: pool.DistributeByModular,
            HashFn: hashkit.Xxh3,
        },
    },
})

pool.Set(ctx, "foo", "bar", 0)

Shard pool use the CRC32 as default hash function when sharding the key, you can overwrite the HashFn in config if wants to use other sharding hash function. The distribution type supports ketama and modular, default is modular.

Test

$ make test

See Also

https://github.com/go-redis/redis

Extension points exported contracts — how you extend this code

ConnFactory (Interface)
(no doc) [2 implementers]
pool.go
HashKit (Interface)
(no doc) [1 implementers]
hashkit/hashkit.go

Core symbols most depended-on inside this repo

getMasterConn
called by 133
pool.go
getSlaveConn
called by 68
pool.go
newErrorIntCmd
called by 64
pool.go
Del
called by 43
pool.go
ZAdd
called by 33
pool.go
newErrorStringSliceCmd
called by 27
pool.go
Set
called by 26
pool.go
SAdd
called by 24
pool.go

Shape

Method 215
Function 39
Struct 11
Interface 2
FuncType 1
TypeAlias 1

Languages

Go100%

Modules by API surface

pool.go205 symbols
ha_conn_factory.go19 symbols
shard_conn_factory.go13 symbols
hashkit/ketama.go11 symbols
hooks.go6 symbols
hashkit/hashkit.go4 symbols
hashkit/ketama_test.go2 symbols
util_test.go1 symbols
util.go1 symbols
main_test.go1 symbols
hashkit/xxh3.go1 symbols
hashkit/md5.go1 symbols

For agents

$ claude mcp add go-redis-pool \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact