Giter Site home page Giter Site logo

go-sync's Introduction

Go Sync License GoDoc

Go Sync is a synchronization framework for distributed systems.

Overview

Distributed systems by their very nature are decoupled and independent. In most cases they must honour 2 out of 3 letters of the CAP theorem e.g Availability and Partitional tolerance but sacrificing consistency. In the case of microservices we often offload this concern to an external database or eventing system. Go Sync provides a framework for synchronization which can be used in the application by the developer.

Getting Started

  • Data - simple distributed data storage
  • Leader - leadership election for group coordination
  • Lock - distributed locking for exclusive resource access
  • Task - distributed job execution
  • Time - provides synchronized time

Lock

The Lock interface provides distributed locking. Multiple instances attempting to lock the same id will block until available.

import "github.com/micro/go-sync/lock/consul"

lock := consul.NewLock()

// acquire lock
err := lock.Acquire("id")
// handle err

// release lock
err = lock.Release("id")
// handle err

Leader

Leader provides leadership election. Useful where one node needs to coordinate some action.

import (
	"github.com/micro/go-sync/leader"
	"github.com/micro/go-sync/leader/consul"
)

l := consul.NewLeader(
	leader.Group("name"),
)

// elect leader
e, err := l.Elect("id")
// handle err


// operate while leader
revoked := e.Revoked()

for {
	select {
	case <-revoked:
		// re-elect
		e.Elect("id")
	default:
		// leader operation
	}
}

// resign leadership
e.Resign() 

Data

Data provides a simple interface for distributed data storage.

import (
	"github.com/micro/go-sync/data"
	"github.com/micro/go-sync/data/consul"
)

keyval := consul.NewData()

err := keyval.Write(&data.Record{
	Key: "foo",
	Value: []byte(`bar`),
})
// handle err

v, err := keyval.Read("foo")
// handle err

err = keyval.Delete("foo")

Task

Task provides distributed job execution. It's a simple way to distribute work across a coordinated pool of workers.

import (
	"github.com/micro/go-sync/task"
	"github.com/micro/go-sync/task/local"
)

t := local.NewTask(
	task.WithPool(10),
)

err := t.Run(task.Command{
	Name: "atask",
	Func: func() error {
		// exec some work
		return nil
	},
})

if err != nil {
	// do something
}

Time

Time provides synchronized time. Local machines may have clock skew and time cannot be guaranteed to be the same everywhere. Synchronized Time allows you to decide how time is defined for your applications.

import (
	"github.com/micro/go-sync/time/ntp"
)


t := ntp.NewTime()
time, err := t.Now()

TODO

  • Event package - strongly consistent event stream e.g kafka

go-sync's People

Contributors

asim avatar nilslice avatar

Watchers

James Cloos avatar xunleer avatar  avatar

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.