election

package module
v0.0.0-...-baf2d7e Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2020 License: MIT Imports: 3 Imported by: 0

README

github.com/domgoer/election

主从选举

依赖

etcd 或者 zookeeper

Example

package main

import (
    "github.com/domgoer/election"
    "github.com/domgoer/election/etcd"
)

func main(){
    cli , _ := etcd.New([]string{"addr"},nil)
    id := "uuid"
    elec := github.com/domgoer/election.New(cli,id)
    stopCh := make(chan struct{}{})
    idCh , _ := elec.Elect(stopCh)

    leaderID := <- idCh
    if leaderID == id {
        fmt.Println("leader")
    }
}

异常处理

如果etcd或者zk不可用, 会让所有服务都升级为leader, 直到etcd或者zk恢复会重新进行选举。

Documentation

Overview

====================================================

# Copyright (C)2019 All rights reserved. # # Author : domchan # Email : 814172254@qq.com # File Name : common.go # Created : 2019-04-29 12:02 # Last Modified : 2019-04-29 12:02 # Describe : # # ====================================================

====================================================

# Copyright (C)2019 All rights reserved. # # Author : domchan # Email : 814172254@qq.com # File Name : selection.go # Created : 2019-04-29 11:06 # Last Modified : 2019-04-29 11:06 # Describe : # # ====================================================

====================================================

# Copyright (C)2019 All rights reserved. # # Author : domchan # Email : 814172254@qq.com # File Name : interface.go # Created : 2019-04-29 11:07 # Last Modified : 2019-04-29 11:07 # Describe : # # ====================================================

Index

Constants

View Source
const (

	// DefaultDialTimeout dial timeout
	DefaultDialTimeout = time.Second * 10
)

Variables

View Source
var (
	// ErrKeyIsExist key exists
	ErrKeyIsExist = errors.New("selection: key is already exist")

	// ErrKeyIsNotExist key is not exist at the beginning
	ErrKeyIsNotExist = errors.New("selection watch: key is not exist")

	// ErrHeartBeatFail connection is not available
	ErrHeartBeatFail = errors.New("heart beat failed")

	// ErrIsNotLeader node is not leader
	ErrIsNotLeader = errors.New("selection: is not leader")
)

Functions

This section is empty.

Types

type Election

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

Election automatic voting

func New

func New(sw Elector, id string) *Election

New selection

func (*Election) DoSelect

func (s *Election) DoSelect() error

DoSelector put value to store, if successful return nil

func (*Election) Elect

func (s *Election) Elect(stopCh <-chan struct{}) (chan string, error)

Elect monitor election results. If any new process is successful, all processes will be notified through chan will not block

func (*Election) ID

func (s *Election) ID() string

func (*Election) Resign

func (s *Election) Resign() error

Resign give up the leader status

type Elector

type Elector interface {
	// Watch watch what happens to key
	Watch(ctx context.Context, key string, stopCh <-chan struct{}) (<-chan *WatchRes, error)
	// Put change the key-value
	SetNx(ctx context.Context, key string, value []byte, opt *WriteOptions) error

	// Delete key-value
	Delete(ctx context.Context, key string) error

	// HeartBeat checks if the connection is available
	HeartBeat() error
}

Elector use set-watch mechanism to realize main-subordinate election

type KVPair

type KVPair struct {
	Key       string
	Value     []byte
	LastIndex int64
}

KVPair encapsulate the results of the query

type OptType

type OptType int

OptType key change type

const (
	// OptionTypeNew event type new
	OptionTypeNew OptType = iota
	// OptionTypeUpdate event type update
	OptionTypeUpdate
	// OptionTypeDelete event type delete
	OptionTypeDelete
)

type WatchRes

type WatchRes struct {
	KV   *KVPair
	Type OptType
}

WatchRes encapsulate the values that change when you watch

type WriteOptions

type WriteOptions struct {
	// expire time of key
	TTL time.Duration
	// Key does not disappear until the connection is disconnected
	KeepAlive bool
}

WriteOptions configuration when writing values

Directories

Path Synopsis
==================================================== # Copyright (C)2019 All rights reserved.
==================================================== # Copyright (C)2019 All rights reserved.
pkg
==================================================== # Copyright (C)2019 All rights reserved.
==================================================== # Copyright (C)2019 All rights reserved.

Jump to

Keyboard shortcuts

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