mirror of
https://github.com/hanzokv/go.git
synced 2026-09-24 00:31:51 +00:00
KV lives in its own org now, like hanzokms / hanzos3 / hanzo-ds. The repo moved hanzoai/kv-go -> hanzokv/go, so the module path follows: a Go module path must match where the repo actually is, or the proxy cannot resolve it. The name is `go`, not `kv-go`, following the hanzo-ds convention — that org holds go, mock, native, sqlbuilder, sqlparser, each named for its language or concern with the ORG supplying the product noun. Repeating "kv" inside a repo that already lives under hanzokv is the compound-word habit we are dropping. 349 occurrences across 188 files: the root module, all four extra/* modules, every example module, imports throughout the tree, and the docs. Builds clean. The package is still `package redis` and the extra dirs are still named redis*. That rename is deliberately NOT bundled here: it is 5,771 occurrences, it breaks all 11 consumers at once, and a large share are WIRE CONTRACTS rather than branding — the redis:// scheme parser, the "redis: nil" error string, RESP command names. Renaming those breaks protocol compatibility with the Valkey server. It needs the same our-vocabulary-vs-the-wire split the argo sweep used, as its own change. GitHub keeps the old path redirecting, and versions already published under github.com/hanzoai/kv-go/v9 stay resolvable, so nothing breaks the moment this lands. Consumers move on their own schedule.
109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
package redis
|
|
|
|
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)
|
|
}
|