当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
leader.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
package consul
import (
"encoding/json"
"errors"
mtx "sync"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/registry"
sync "github.com/micro/go-os/sync"
)
type consulLeader struct {
opts sync.LeaderOptions
c *api.Client
id string
key string
// marshalled registry node
srv []byte
// used to sync back status
statusCh chan sync.LeaderStatus
mtx.Mutex
status sync.LeaderStatus
}
type consulElected struct {
ch chan sync.LeaderStatus
rv <-chan struct{}
l *api.Lock
}
func (c *consulLeader) Leader() (*registry.Node, error) {
kv, _, err := c.c.KV().Get(c.key, nil)
if err != nil || kv == nil {
return nil, err
}
var node *registry.Node
if err := json.Unmarshal(kv.Value, &node); err != nil {
return nil, err
}
return node, nil
}
func (c *consulLeader) Id() string {
return c.id
}
func (c *consulLeader) Elect() (sync.Elected, error) {
lc, err := c.c.LockOpts(&api.LockOptions{
Key: c.key,
Value: c.srv,
})
if err != nil {
return nil, err
}
rv, err := lc.Lock(nil)
if err != nil {
return nil, err
}
c.statusCh <- sync.ElectedStatus
// lock acquired
return &consulElected{
rv: rv,
l: lc,
ch: c.statusCh,
}, nil
}
func (c *consulLeader) Status() (sync.LeaderStatus, error) {
c.Lock()
defer c.Unlock()
return c.status, nil
}
func (c *consulElected) Revoked() (chan struct{}, error) {
select {
case <-c.rv:
return nil, errors.New("already revoked")
default:
}
ch := make(chan struct{}, 1)
go func() {
st := <-ch
c.ch <- sync.FollowerStatus
ch <- st
}()
return ch, nil
}
func (c *consulElected) Resign() error {
c.ch <- sync.FollowerStatus
err := c.l.Unlock()
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.8.0

搜索帮助