etcd

package
v0.0.0-...-a8c716a Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidParam = errors.New("invalid param")
)

Functions

func NewProvider

func NewProvider(ctx context.Context, c *Client, key, value string, ttl int64, f ProviderFunc) error

NewProvider 创建一个Provider.

func NewProviderKey

func NewProviderKey(preifx, host string, debug bool) (k, v string)

Types

type Client

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

func NewETCD

func NewETCD(ctx context.Context, endpoints []string, ops ...OptionFunc) (*Client, error)

NewETCD 创建ETCD客户端

func (*Client) AddMember

func (cli *Client) AddMember(ctx context.Context, peerURLs []string) error

AddMember 增加节点.例如: peerURLs=[]string{"http://127.0.0.1:2180"}

func (*Client) Close

func (cli *Client) Close() error

Close 关闭etcd客户端

func (*Client) Compact

func (cli *Client) Compact(ctx context.Context, threshold int64, isdefrag bool)

func (*Client) CreateLeaseID

func (cli *Client) CreateLeaseID(ctx context.Context, ttl int64) (int64, error)

CreateLeaseID 创建租期ID

func (*Client) DelMember

func (cli *Client) DelMember(ctx context.Context, memberID uint64) error

DelMember 删除节点.

func (*Client) Delete

func (cli *Client) Delete(ctx context.Context, key string) error

Delete 删除一个Key

func (*Client) DeleteWithPrefix

func (cli *Client) DeleteWithPrefix(ctx context.Context, key string) error

DeleteDeleteWithPrefix 使用前缀删除一批Key

func (*Client) DestroyLock

func (cli *Client) DestroyLock(ctx context.Context, key string)

func (*Client) Get

func (cli *Client) Get(ctx context.Context, prefix string) ([]string, error)

Get 从ETCD中获取一个KEY,通过前缀获得,所以有可能有多个值

func (*Client) GetKeys

func (cli *Client) GetKeys(ctx context.Context, prefix string) ([]string, error)

func (*Client) GetKvs

func (cli *Client) GetKvs(ctx context.Context, prefix string) (map[string]string, error)

GetKvs 从ETCD中获取一个Key前缀的Key:value对应的map集合

func (*Client) GetLimit

func (cli *Client) GetLimit(ctx context.Context, prefix string, offset int64) ([]string, []string, error)

GetLimit 从ETCD中通过前缀获得,所以有可能有多个值

func (*Client) GetOne

func (cli *Client) GetOne(ctx context.Context, prefix string) (string, error)

GetOne 仅获取一个值

func (*Client) GetPriorityQueueList

func (cli *Client) GetPriorityQueueList(ctx context.Context, key string) ([]string, error)

GetPriorityQueueList 获取指定前缀的优先级队列名称

func (*Client) GetTTLWithLease

func (cli *Client) GetTTLWithLease(ctx context.Context, leaseID int64) (int64, error)

GetTTLWithLease 获取租约TTL

func (*Client) KeepAliveOnce

func (cli *Client) KeepAliveOnce(ctx context.Context, leaseID int64) error

KeepAliveOnce 给租约进行保活

func (*Client) ListMembers

func (cli *Client) ListMembers(ctx context.Context) ([]*Member, error)

ListMembers 节点成员

func (*Client) Lock

func (cli *Client) Lock(ctx context.Context, key string) error

func (*Client) NewElection

func (cli *Client) NewElection(ctx context.Context, key string, conf *ElectionConfig, f ElectionFunc) error

NewElection 通过ETCD Client创建一个Election.

func (*Client) NewWatcher

func (cli *Client) NewWatcher(ctx context.Context, prefix string, f WatcherFunc) error

NewWatcher 通过ETCD Client创建一个Watcher对象,需要输入监听的前缀. 异步的

func (*Client) Pop

func (cli *Client) Pop(ctx context.Context, prefix string, limit int64) ([]string, error)

Pop 从ETCD中弹出一个key

func (*Client) Put

func (cli *Client) Put(ctx context.Context, key, value string) (err error)

Put向ETCD永久写入一个Key.

func (*Client) PutWithLease

func (cli *Client) PutWithLease(ctx context.Context, key, value string, lease int64) error

PutWithLease 使用租期ID创建数据

func (*Client) PutWithTTL

func (cli *Client) PutWithTTL(ctx context.Context, key, value string, ttl int64) (err error)

PutWithTTL 向ETCD写入一个带TTL的Key.

func (*Client) UnLock

func (cli *Client) UnLock(ctx context.Context, key string) error

func (*Client) WithNodeName

func (cli *Client) WithNodeName(name string) *Client

type Election

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

Election 用来进行选举,如果成功,就会通过回调函数返回选举成功,默认情况下,当前节点不是主节点.

func (*Election) Destroy

func (e *Election) Destroy()

Stop 停止进行选举.

func (*Election) Start

func (e *Election) Start(ctx context.Context) (err error)

type ElectionConfig

type ElectionConfig struct {
	Timeout int `json:"timeout"`
}

type ElectionFunc

type ElectionFunc func(ctx context.Context, e *Election, isLeader bool, err error)

ElectionFunc 选举成功之后会回调这个函数,只会回调一次.

type EventType

type EventType int
const (
	// ETCD 操作类型
	UpdateEventTypePut EventType = iota + 1
	UpdateEventTypeDelete
	UpdateEventTypeUnKnown
)

type Member

type Member struct {
	// name is the human-readable name of the member. If the member is not started, the name will be an empty string.
	Name string `json:"name,omitempty"`
	// peerURLs is the list of URLs the member exposes to the cluster for communication.
	PeerURLs   []string `json:"peerURLs,omitempty"`
	ClientURLs []string `json:"clientURLs,omitempty"`
	// ID is the member ID for this member.
	ID uint64 `json:"ID,omitempty"`
}

type OptionFunc

type OptionFunc func(*etcdcli.Config) error

func WithBaseAuth

func WithBaseAuth(user, passwd string) OptionFunc

WithBaseAuth 基础鉴权

func WithDialKeepAliveTime

func WithDialKeepAliveTime(time time.Duration) OptionFunc

WithDialKeepAliveTime 客户端超时时间

func Withlogger

func Withlogger(lg *zap.Logger) OptionFunc

Withlogger 配置logger

type PriorityQueue

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

func NewPriorityQueue

func NewPriorityQueue(ctx context.Context, endpoints []string, key string, ops ...OptionFunc) (*PriorityQueue, error)

func NewPriorityQueueByClient

func NewPriorityQueueByClient(ctx context.Context, client *Client, key string) (*PriorityQueue, error)

func (*PriorityQueue) Delete

func (p *PriorityQueue) Delete(ctx context.Context) error

Delete 删除这个优先级队列的所有记录

func (*PriorityQueue) Len

func (p *PriorityQueue) Len(ctx context.Context) (int, error)

Len 获取优先级队列的剩余元素

func (*PriorityQueue) Pop

func (p *PriorityQueue) Pop(ctx context.Context) (val string, err error)

Pop 如果消息队列为空, 则该方法将阻塞 MARK: @zcf ~~如果消息队列为空, 则该方法将阻塞1秒钟, 直到有消息可以被取出; 或为空字符串""~~

func (*PriorityQueue) Push

func (p *PriorityQueue) Push(ctx context.Context, val string, pr uint16) (err error)

type Provider

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

Provider 注册一个Key,并且随时保持心跳,对象同样有Start和Stop函数.

func (*Provider) Start

func (p *Provider) Start() error

Start 保持与ETCD之间的连接.

func (*Provider) Stop

func (p *Provider) Stop()

type ProviderFunc

type ProviderFunc func(p *Provider, err error)

type UpdateEvent

type UpdateEvent struct {
	Key       string
	Value     string
	PreKey    string
	PreValue  string
	EventType EventType
}

func (UpdateEvent) String

func (ue UpdateEvent) String() string

type Watcher

type Watcher struct {
	Logger *zap.Logger
	// contains filtered or unexported fields
}

Watcher 对象,用于监听一个Key的变化对象,这个对象有Start、Stop参数.

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop 停止监听.

type WatcherFunc

type WatcherFunc func(w *Watcher, updateEvent []*UpdateEvent, err error)

WatcherFunc 来自于Watcher的回调,通过这个回调来进行通知.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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