leader_follower_pattern

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: MIT Imports: 5 Imported by: 0

README

主备系统模式

原理

使用分布式锁实现主备节点系统。通过对分布式锁进行续期,保持长期锁, 从而使当前服务节点处于主服务节点

无法获取分布式锁的服务节点,则作为备选节点, 当主节点下线或异常,分布式锁被释放或无法正常续期, 则其中备选节点会成为主节点

注册策略

锁竞争遵循公平策略,根据抢占分布式锁的顺序, 先抢占锁的进程先获取锁

流程
  1. CAS事务: 抢占prefix前缀的锁, 当leaseId 与 当前客户端的leaseId 相同, 则当前进程为主节点

  2. 抢占锁失败, 依据 Revision 值 进行排队。当主节点释放锁, 将从下一个 Revision 的进程节点成为主节点

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HeartBeatTTL

func HeartBeatTTL(ttl int) option

func NewLeaderFollower

func NewLeaderFollower(client *clientv3.Client, op ...option) (*leaderFollower, error)

func Prefix

func Prefix(prefix string) option

Types

type ObserveRes

type ObserveRes struct {
	Res       clientv3.GetResponse
	EventType mvccpb.Event_EventType
}

type Watcher

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

func NewWatcher

func NewWatcher(cctx context.Context, pfx string, c *clientv3.Client) (*Watcher, error)

func (*Watcher) Observe

func (w *Watcher) Observe(ctx context.Context) chan ObserveRes

自定义封装监听重新选举与无法获取选取等事件

func (*Watcher) Stop

func (w *Watcher) Stop() error

func (*Watcher) Watch

func (w *Watcher) Watch(watchHandle func(res clientv3.GetResponse)) (string, error)

func (*Watcher) WatchWithEventType

func (w *Watcher) WatchWithEventType(watchHandle func(res ObserveRes)) (string, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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