client

package
v0.0.0-...-db606fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

@author zhangxiaomin

@author zxm @date 2020-09-08 @description 一致性hash的实现算法,参考自https://blog.csdn.net/xcl168/article/details/43898807

@author zxm 基于客户端分片形式的集群 采用一致性哈希

事务处理器 @author zxm @date 2020-06-02

Index

Constants

View Source
const DEFAULT_REPLICAS = 160

默认每个节点的虚拟节点数量

Variables

This section is empty.

Functions

func Connect

func Connect(server string) *net.TCPConn

*

  • tcp连接

func LoggerInit

func LoggerInit()

func SendCommand

func SendCommand(conn *net.TCPConn, cmd string, a ...[]byte) string

*

  • 通信协议处理:如redis操作:set key value,解析成redis理解的格式后就是:
  • *3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

*

  • gedis 客户端

func BuildClient

func BuildClient(pool *ConnPool) *Client

*

  • 初始化客户端
  • @param pool 连接池

func (*Client) Auth

func (client *Client) Auth(pwd string) (interface{}, error)

*

  • 认证权限

func (*Client) Decr

func (client *Client) Decr(key string) (interface{}, error)

func (*Client) Del

func (client *Client) Del(key string) (interface{}, error)

func (*Client) Discard

func (client *Client) Discard() (interface{}, error)

终止事务

func (*Client) Exec

func (client *Client) Exec() (interface{}, error)

执行事务

func (*Client) Expire

func (client *Client) Expire(key string, value int64) (interface{}, error)

func (*Client) Get

func (client *Client) Get(key string) (interface{}, error)

func (*Client) Hdel

func (client *Client) Hdel(hash string, key string) (interface{}, error)

func (*Client) Hexists

func (client *Client) Hexists(hash string, key string) (interface{}, error)

func (*Client) Hget

func (client *Client) Hget(hash string, key string) (interface{}, error)

func (*Client) Hgetall

func (client *Client) Hgetall(hash string) (interface{}, error)

func (*Client) Hkeys

func (client *Client) Hkeys(hash string) (interface{}, error)

func (*Client) Hlen

func (client *Client) Hlen(hash string) (interface{}, error)

func (*Client) Hmget

func (client *Client) Hmget(hash string, keys ...string) (interface{}, error)

func (*Client) Hmset

func (client *Client) Hmset(hash string, keyvalues ...string) (interface{}, error)

func (*Client) Hset

func (client *Client) Hset(hash string, key string, value string) (interface{}, error)

func (*Client) Hvalues

func (client *Client) Hvalues(hash string) (interface{}, error)

func (*Client) Incr

func (client *Client) Incr(key string) (interface{}, error)

func (*Client) Keys

func (client *Client) Keys(regex string) (interface{}, error)

func (*Client) Lindex

func (client *Client) Lindex(list string, pos int64) (interface{}, error)

func (*Client) LinsertAfter

func (client *Client) LinsertAfter(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Client) LinsertBefore

func (client *Client) LinsertBefore(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Client) Llen

func (client *Client) Llen(list string) (interface{}, error)

func (*Client) Lpop

func (client *Client) Lpop(list string) (interface{}, error)

func (*Client) Lpush

func (client *Client) Lpush(list string, elements ...string) (interface{}, error)

func (*Client) Lrange

func (client *Client) Lrange(list string, start int64, end int64) (interface{}, error)

func (*Client) Lrem

func (client *Client) Lrem(list string, count int64, value string) (interface{}, error)

*

  • count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
  • count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
  • count = 0 : 移除表中所有与 VALUE 相等的值。

func (*Client) Lset

func (client *Client) Lset(list string, pos int64, value string) (interface{}, error)

func (*Client) Mget

func (client *Client) Mget(keys ...string) (interface{}, error)

func (*Client) Mset

func (client *Client) Mset(keyvalues ...string) (interface{}, error)

func (*Client) Multi

func (client *Client) Multi() (interface{}, error)

开启事务

func (*Client) Ping

func (client *Client) Ping() (interface{}, error)

func (*Client) Rpop

func (client *Client) Rpop(list string) (interface{}, error)

func (*Client) Rpush

func (client *Client) Rpush(list string, elements ...string) (interface{}, error)

func (*Client) Sadd

func (client *Client) Sadd(set string, elements ...string) (interface{}, error)

func (*Client) Scard

func (client *Client) Scard(set string) (interface{}, error)

func (*Client) Sdiff

func (client *Client) Sdiff(sets ...string) (interface{}, error)

*

  • 返回给定所有集合的差集

func (*Client) Set

func (client *Client) Set(key string, value string) (interface{}, error)

func (*Client) Setex

func (client *Client) Setex(key string, time int64, value string) (interface{}, error)

func (*Client) Setnx

func (client *Client) Setnx(key string, value string) (interface{}, error)

func (*Client) Sismember

func (client *Client) Sismember(set string, value string) (interface{}, error)

func (*Client) Smembers

func (client *Client) Smembers(set string) (interface{}, error)

func (*Client) Spop

func (client *Client) Spop(set string) (interface{}, error)

func (*Client) Srandmembers

func (client *Client) Srandmembers(set string, count int64) (interface{}, error)

func (*Client) Srem

func (client *Client) Srem(set string, elements ...string) (interface{}, error)

func (*Client) Ttl

func (client *Client) Ttl(key string) (interface{}, error)

func (*Client) Zadd

func (client *Client) Zadd(zset string, scoresvalues ...string) (interface{}, error)

func (*Client) Zcard

func (client *Client) Zcard(zset string) (interface{}, error)

func (*Client) Zrange

func (client *Client) Zrange(zset string, start int64, end int64) (interface{}, error)

func (*Client) ZrangeWithScores

func (client *Client) ZrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Client) Zrank

func (client *Client) Zrank(zset string, value string) (interface{}, error)

func (*Client) Zrem

func (client *Client) Zrem(zset string, elements ...string) (interface{}, error)

func (*Client) Zrevrange

func (client *Client) Zrevrange(zset string, start int64, end int64) (interface{}, error)

func (*Client) ZrevrangeWithScores

func (client *Client) ZrevrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Client) Zrevrank

func (client *Client) Zrevrank(zset string, value string) (interface{}, error)

func (*Client) Zscore

func (client *Client) Zscore(zset string, value string) (interface{}, error)

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

*

  • 集群客户端
  • heartBeatInterval 心跳检测时间间隔,单位s
  • clusterPool key:连接串 value:连接池

func NewCluster

func NewCluster(clusterConfig ClusterConfig) *Cluster

*

  • 初始化Cluster client

func (*Cluster) Auth

func (cluster *Cluster) Auth(pwd string) (interface{}, error)

func (*Cluster) Decr

func (cluster *Cluster) Decr(key string) (interface{}, error)

func (*Cluster) Del

func (cluster *Cluster) Del(key string) (interface{}, error)

func (*Cluster) Expire

func (cluster *Cluster) Expire(key string, value int64) (interface{}, error)

func (*Cluster) Get

func (cluster *Cluster) Get(key string) (interface{}, error)

func (*Cluster) GetClusterNodesInfo

func (cluster *Cluster) GetClusterNodesInfo() []*Node

func (*Cluster) GetClusterPool

func (cluster *Cluster) GetClusterPool() map[string]*ConnPool

func (*Cluster) Hdel

func (cluster *Cluster) Hdel(hash string, key string) (interface{}, error)

func (*Cluster) Hexists

func (cluster *Cluster) Hexists(hash string, key string) (interface{}, error)

func (*Cluster) Hget

func (cluster *Cluster) Hget(hash string, key string) (interface{}, error)

func (*Cluster) Hgetall

func (cluster *Cluster) Hgetall(hash string) (interface{}, error)

func (*Cluster) Hkeys

func (cluster *Cluster) Hkeys(hash string) (interface{}, error)

func (*Cluster) Hmget

func (cluster *Cluster) Hmget(hash string, keys ...string) (interface{}, error)

func (*Cluster) Hmset

func (cluster *Cluster) Hmset(hash string, keyvalues ...string) (interface{}, error)

func (*Cluster) Hset

func (cluster *Cluster) Hset(hash string, key string, value string) (interface{}, error)

func (*Cluster) Hvalues

func (cluster *Cluster) Hvalues(hash string) (interface{}, error)

func (*Cluster) Incr

func (cluster *Cluster) Incr(key string) (interface{}, error)

func (*Cluster) Keys

func (cluster *Cluster) Keys(regex string) (interface{}, error)

func (*Cluster) Lindex

func (cluster *Cluster) Lindex(list string, pos int64) (interface{}, error)

func (*Cluster) LinsertAfter

func (cluster *Cluster) LinsertAfter(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Cluster) LinsertBefore

func (cluster *Cluster) LinsertBefore(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Cluster) Llen

func (cluster *Cluster) Llen(list string) (interface{}, error)

func (*Cluster) Lpop

func (cluster *Cluster) Lpop(list string) (interface{}, error)

func (*Cluster) Lpush

func (cluster *Cluster) Lpush(list string, elements ...string) (interface{}, error)

func (*Cluster) Lrange

func (cluster *Cluster) Lrange(list string, start int64, end int64) (interface{}, error)

func (*Cluster) Lrem

func (cluster *Cluster) Lrem(list string, count int64, value string) (interface{}, error)

*

  • count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
  • count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
  • count = 0 : 移除表中所有与 VALUE 相等的值。

func (*Cluster) Lset

func (cluster *Cluster) Lset(list string, pos int64, value string) (interface{}, error)

func (*Cluster) Mget

func (cluster *Cluster) Mget(keys ...string) (interface{}, error)

func (*Cluster) Mset

func (cluster *Cluster) Mset(keyvalues ...string) (interface{}, error)

func (*Cluster) RandomSelect

func (cluster *Cluster) RandomSelect() *ConnPool

func (*Cluster) Rpop

func (cluster *Cluster) Rpop(list string) (interface{}, error)

func (*Cluster) Rpush

func (cluster *Cluster) Rpush(list string, elements ...string) (interface{}, error)

func (*Cluster) Sadd

func (cluster *Cluster) Sadd(set string, elements ...string) (interface{}, error)

func (*Cluster) Scard

func (cluster *Cluster) Scard(set string) (interface{}, error)

func (*Cluster) Sdiff

func (cluster *Cluster) Sdiff(sets ...string) (interface{}, error)

*

  • 返回给定所有集合的差集

func (*Cluster) SelectOne

func (cluster *Cluster) SelectOne(url string) *ConnPool

func (*Cluster) Set

func (cluster *Cluster) Set(key string, value string) (interface{}, error)

func (*Cluster) Setex

func (cluster *Cluster) Setex(key string, time int64, value string) (interface{}, error)

func (*Cluster) Setnx

func (cluster *Cluster) Setnx(key string, value string) (interface{}, error)

func (*Cluster) Sismember

func (cluster *Cluster) Sismember(set string, value string) (interface{}, error)

func (*Cluster) Smembers

func (cluster *Cluster) Smembers(set string) (interface{}, error)

func (*Cluster) Spop

func (cluster *Cluster) Spop(set string) (interface{}, error)

func (*Cluster) Srandmembers

func (cluster *Cluster) Srandmembers(set string, count int64) (interface{}, error)

func (*Cluster) Srem

func (cluster *Cluster) Srem(set string, elements ...string) (interface{}, error)

func (*Cluster) Ttl

func (cluster *Cluster) Ttl(key string) (interface{}, error)

func (*Cluster) Zadd

func (cluster *Cluster) Zadd(zset string, scoresvalues ...string) (interface{}, error)

func (*Cluster) Zcard

func (cluster *Cluster) Zcard(zset string) (interface{}, error)

func (*Cluster) Zrange

func (cluster *Cluster) Zrange(zset string, start int64, end int64) (interface{}, error)

func (*Cluster) ZrangeWithScores

func (cluster *Cluster) ZrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Cluster) Zrank

func (cluster *Cluster) Zrank(zset string, value string) (interface{}, error)

func (*Cluster) Zrem

func (cluster *Cluster) Zrem(zset string, elements ...string) (interface{}, error)

func (*Cluster) Zrevrange

func (cluster *Cluster) Zrevrange(zset string, start int64, end int64) (interface{}, error)

func (*Cluster) ZrevrangeWithScores

func (cluster *Cluster) ZrevrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Cluster) Zrevrank

func (cluster *Cluster) Zrevrank(zset string, value string) (interface{}, error)

func (*Cluster) Zscore

func (cluster *Cluster) Zscore(zset string, value string) (interface{}, error)

type ClusterConfig

type ClusterConfig struct {
	Nodes             []*Node
	HeartBeatInterval int
}

type ConnConfig

type ConnConfig struct {
	ConnString string
	Pwd        string
	InitActive int
	MinActive  int
	MaxActive  int
}

type ConnPool

type ConnPool struct {
	// contains filtered or unexported fields
}

func NewConnPool

func NewConnPool(config ConnConfig) (*ConnPool, error)

*

  • 初始化连接池

func NewSingleConnPool

func NewSingleConnPool(config ConnConfig) *ConnPool

*

  • 单例的连接池(线程安全)

func (*ConnPool) GetConn

func (pool *ConnPool) GetConn() (*net.TCPConn, error)

*

  • 从连接池中获取连接

func (*ConnPool) PoolSize

func (pool *ConnPool) PoolSize() int

*

  • 返回连接池当前连接数

func (*ConnPool) PutConn

func (pool *ConnPool) PutConn(conn *net.TCPConn) error

*

  • 将连接归还到连接池

type Consistent

type Consistent struct {
	Nodes map[uint32]ShardInfo

	Resources map[int]bool

	sync.RWMutex
	// contains filtered or unexported fields
}

func NewConsistent

func NewConsistent() *Consistent

func (*Consistent) Add

func (c *Consistent) Add(node *ShardInfo) bool

将节点加入到ring上

func (*Consistent) GetShardInfo

func (c *Consistent) GetShardInfo(key string) ShardInfo

根据key获取分片信息

func (*Consistent) Remove

func (c *Consistent) Remove(node *ShardInfo)

type HashRing

type HashRing []uint32

hash环

func (HashRing) Len

func (c HashRing) Len() int

func (HashRing) Less

func (c HashRing) Less(i, j int) bool

func (HashRing) Swap

func (c HashRing) Swap(i, j int)

type Node

type Node struct {
	Url        string
	Pwd        string
	InitActive int
	MinActive  int
	MaxActive  int
}

*

  • 节点
  • master:主节点ip+port
  • slaves:从节点ip+port集合

type Shard

type Shard struct {
	Url        string
	Pwd        string
	InitActive int
	MinActive  int
	MaxActive  int
}

*

  • 分片信息 *

type ShardConfig

type ShardConfig struct {
	Shards            []*Shard
	HeartBeatInterval int
}

分片配置

type ShardInfo

type ShardInfo struct {
	Id     int
	Url    string
	Weight int
}

分片信息

func NewShardInfo

func NewShardInfo(id int, url string, weight int) *ShardInfo

type Sharding

type Sharding struct {
	// contains filtered or unexported fields
}

*

  • 客户端分片
  • heartBeatInterval 心跳检测时间间隔,单位s
  • shardingPool key:连接串 value:连接池

func NewSharding

func NewSharding(shardConfig ShardConfig) *Sharding

初始化分片客户端

func (*Sharding) Auth

func (sharding *Sharding) Auth(pwd string) (interface{}, error)

func (*Sharding) Decr

func (sharding *Sharding) Decr(key string) (interface{}, error)

func (*Sharding) Del

func (sharding *Sharding) Del(key string) (interface{}, error)

func (*Sharding) Expire

func (sharding *Sharding) Expire(key string, value int64) (interface{}, error)

func (*Sharding) Get

func (sharding *Sharding) Get(key string) (interface{}, error)

func (*Sharding) GetShardInfo

func (sharding *Sharding) GetShardInfo() []*Shard

获取分片信息

func (*Sharding) GetShardingPool

func (sharding *Sharding) GetShardingPool() map[string]*ConnPool

获取分片连接池

func (*Sharding) Hget

func (sharding *Sharding) Hget(hash string, key string) (interface{}, error)

func (*Sharding) Hset

func (sharding *Sharding) Hset(hash string, key string, value string) (interface{}, error)

func (*Sharding) Incr

func (sharding *Sharding) Incr(key string) (interface{}, error)

func (*Sharding) Lindex

func (sharding *Sharding) Lindex(list string, pos int64) (interface{}, error)

func (*Sharding) LinsertAfter

func (sharding *Sharding) LinsertAfter(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Sharding) LinsertBefore

func (sharding *Sharding) LinsertBefore(list string, target string, value string) (interface{}, error)

*

  • target 目标元素

func (*Sharding) Llen

func (sharding *Sharding) Llen(list string) (interface{}, error)

func (*Sharding) Lpop

func (sharding *Sharding) Lpop(list string) (interface{}, error)

func (*Sharding) Lpush

func (sharding *Sharding) Lpush(list string, elements ...string) (interface{}, error)

func (*Sharding) Lrange

func (sharding *Sharding) Lrange(list string, start int64, end int64) (interface{}, error)

func (*Sharding) Lrem

func (sharding *Sharding) Lrem(list string, count int64, value string) (interface{}, error)

*

  • count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
  • count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
  • count = 0 : 移除表中所有与 VALUE 相等的值。

func (*Sharding) Lset

func (sharding *Sharding) Lset(list string, pos int64, value string) (interface{}, error)

func (*Sharding) Rpop

func (sharding *Sharding) Rpop(list string) (interface{}, error)

func (*Sharding) Rpush

func (sharding *Sharding) Rpush(list string, elements ...string) (interface{}, error)

func (*Sharding) Sadd

func (sharding *Sharding) Sadd(set string, elements ...string) (interface{}, error)

func (*Sharding) Scard

func (sharding *Sharding) Scard(set string) (interface{}, error)

func (*Sharding) Set

func (sharding *Sharding) Set(key string, value string) (interface{}, error)

func (*Sharding) Setex

func (sharding *Sharding) Setex(key string, time int64, value string) (interface{}, error)

func (*Sharding) Setnx

func (sharding *Sharding) Setnx(key string, value string) (interface{}, error)

func (*Sharding) Sismember

func (sharding *Sharding) Sismember(set string, value string) (interface{}, error)

func (*Sharding) Smembers

func (sharding *Sharding) Smembers(set string) (interface{}, error)

func (*Sharding) Spop

func (sharding *Sharding) Spop(set string) (interface{}, error)

func (*Sharding) Srandmembers

func (sharding *Sharding) Srandmembers(set string, count int64) (interface{}, error)

func (*Sharding) Srem

func (sharding *Sharding) Srem(set string, elements ...string) (interface{}, error)

func (*Sharding) Ttl

func (sharding *Sharding) Ttl(key string) (interface{}, error)

func (*Sharding) Zadd

func (sharding *Sharding) Zadd(zset string, scoresvalues ...string) (interface{}, error)

func (*Sharding) Zcard

func (sharding *Sharding) Zcard(zset string) (interface{}, error)

func (*Sharding) Zrange

func (sharding *Sharding) Zrange(zset string, start int64, end int64) (interface{}, error)

func (*Sharding) ZrangeWithScores

func (sharding *Sharding) ZrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Sharding) Zrank

func (sharding *Sharding) Zrank(zset string, value string) (interface{}, error)

func (*Sharding) Zrem

func (sharding *Sharding) Zrem(zset string, elements ...string) (interface{}, error)

func (*Sharding) Zrevrange

func (sharding *Sharding) Zrevrange(zset string, start int64, end int64) (interface{}, error)

func (*Sharding) ZrevrangeWithScores

func (sharding *Sharding) ZrevrangeWithScores(zset string, start int64, end int64) (interface{}, error)

func (*Sharding) Zrevrank

func (sharding *Sharding) Zrevrank(zset string, value string) (interface{}, error)

func (*Sharding) Zscore

func (sharding *Sharding) Zscore(zset string, value string) (interface{}, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL