mirror of
https://github.com/hanzokv/go.git
synced 2026-09-24 00:31:51 +00:00
We do not use Redis. The client was a go-redis fork and still said so
everywhere: `package redis`, `redis.NewClient`, `redis://`, `extra/redisotel`.
Consumers wrote `redis.Client` against a Valkey-backed store called KV.
package redis -> package kv (93 decls)
redis.X -> kv.X (2,794 call sites)
"redis: …" errors -> "kv: …" (206)
extra/redis{otel,cmd,prometheus,census} -> extra/kv{otel,cmd,prometheus,census}
redis:// , rediss:// -> kv:// , kvs:// (ParseURL)
Redis*/*Redis* idents -> KV*/*KV*
REDIS_VERSION, REDIS_ENDPOINTS_CONFIG_PATH -> KV_*
I previously argued this rename was unsafe because much of it was "wire
contract". That was wrong, and the categorisation shows why: RESP command names
are GET/SET/HSET — they never contain the word. Of 5,771 occurrences only two
kinds were server-visible at all: the ParseURL scheme (we choose what we pass;
our configs already use kv://) and the CLIENT SETINFO lib-name label, which the
server merely records. Nothing here changes a byte of protocol.
Two things deliberately keep the old spelling:
RediSearch — names a specific external module the FT.* commands map to.
StoreDist — contains the letters by accident (sto-reDis-t), not the word.
Breaking for consumers by design: every import site must move redis.X -> kv.X.
Minor bump, never a major — this module stays v9 forever.
109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
package kv
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/hanzokv/go/v9/internal"
|
|
"github.com/hanzokv/go/v9/internal/hashtag"
|
|
"github.com/hanzokv/go/v9/internal/pool"
|
|
)
|
|
|
|
func (c *baseClient) Pool() pool.Pooler {
|
|
return c.connPool
|
|
}
|
|
|
|
func (c *PubSub) SetNetConn(netConn net.Conn) {
|
|
c.cn = pool.NewConn(netConn)
|
|
}
|
|
|
|
func (c *ClusterClient) LoadState(ctx context.Context) (*clusterState, error) {
|
|
// return c.state.Reload(ctx)
|
|
return c.loadState(ctx)
|
|
}
|
|
|
|
func (c *ClusterClient) SlotAddrs(ctx context.Context, slot int) []string {
|
|
state, err := c.state.Get(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var addrs []string
|
|
for _, n := range state.slotNodes(slot) {
|
|
addrs = append(addrs, n.Client.getAddr())
|
|
}
|
|
return addrs
|
|
}
|
|
|
|
func (c *ClusterClient) Nodes(ctx context.Context, key string) ([]*clusterNode, error) {
|
|
state, err := c.state.Reload(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
slot := hashtag.Slot(key)
|
|
nodes := state.slotNodes(slot)
|
|
if len(nodes) != 2 {
|
|
return nil, fmt.Errorf("slot=%d does not have enough nodes: %v", slot, nodes)
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
func (c *ClusterClient) SwapNodes(ctx context.Context, key string) error {
|
|
nodes, err := c.Nodes(ctx, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
nodes[0], nodes[1] = nodes[1], nodes[0]
|
|
return nil
|
|
}
|
|
|
|
func (c *clusterState) IsConsistent(ctx context.Context) bool {
|
|
if len(c.Masters) < 3 {
|
|
return false
|
|
}
|
|
for _, master := range c.Masters {
|
|
s := master.Client.Info(ctx, "replication").Val()
|
|
if !strings.Contains(s, "role:master") {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if len(c.Slaves) < 3 {
|
|
return false
|
|
}
|
|
for _, slave := range c.Slaves {
|
|
s := slave.Client.Info(ctx, "replication").Val()
|
|
if !strings.Contains(s, "role:slave") {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func GetSlavesAddrByName(ctx context.Context, c *SentinelClient, name string) []string {
|
|
addrs, err := c.Replicas(ctx, name).Result()
|
|
if err != nil {
|
|
internal.Logger.Printf(ctx, "sentinel: Replicas name=%q failed: %s",
|
|
name, err)
|
|
return []string{}
|
|
}
|
|
return parseReplicaAddrs(addrs, false)
|
|
}
|
|
|
|
func (c *Ring) ShardByName(name string) *ringShard {
|
|
shard, _ := c.sharding.GetByName(name)
|
|
return shard
|
|
}
|
|
|
|
func (c *ModuleLoadexConfig) ToArgs() []interface{} {
|
|
return c.toArgs()
|
|
}
|
|
|
|
func ShouldRetry(err error, retryTimeout bool) bool {
|
|
return shouldRetry(err, retryTimeout)
|
|
}
|