Git Product home page Git Product logo

master-election's Introduction

master-election

master-election是进行主从选举的工具包。在分布式场景,我们经常需要多个实例中的某一个实例去单独的运行某一串逻辑。 此包可以帮助你快速上手,并且忽略掉选主的具体细节。

支持组件

  • Mysql (已完成)
  • Redis (待开发)
  • PostgreSQL (待开发)
  • ETCD (待开发)
  • Consul (待开发)
  • Mongodb (待开发)

安装

go get github.com/changsongl/master-election

示例

package main

import (
	"fmt"
	"time"

	"github.com/changsongl/master-election"
	"github.com/changsongl/master-election/lock/mysql"
)

func main() {
	m, err := master.New(
		mysql.NewMasterLock(
			&mysql.Config{
				User:      "root",
				Password:  "123456",
				Host:      "127.0.0.1",
				Port:      3306,
				TableName: "master_election_table",
				DBName:    "master_election_database",
				CreateDB:  true,
			},
		),
		master.OptionHeartbeat(time.Second*2),
		master.OptionMasterStartHook(func(epoch uint64) {
			fmt.Printf("master start: epoch %d\n", epoch)
		}),
		master.OptionMasterEndHook(func(epoch uint64) {
			fmt.Printf("master end: epoch %d\n", epoch)
		}),
		// other option available
	)

	if err != nil {
		panic(err)
	}

	go func() {
		err = m.Start()
		if err != nil {
			panic(err)
		}
	}()

	for {
		if m.IsMaster() {
			// do some master logic
		} else {
			// do some slave logic or do nothing
		}
	}

	// m.Stop()
}

master-election's People

Contributors

changsongl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cleowung

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.