Giter VIP home page Giter VIP logo

go-dynamolock's Introduction

Go Reference

go-dynamolock

why

locking around dynamodb should be simple and easy.

what

a minimal go library for locking around dynamodb.

compared to alternatives it has less code and fewer features.

how

a record in dynamodb uses a uuid and a timetamp to coordinate callers.

to lock, a caller finds the uuid missing and adds it.

while locked, the caller heartbeats the timestamp.

to unlock, the caller removes the uuid.

arbitrary data can be stored atomically in the lock record. it is read via lock, and written via unlock.

manipulation of external state while the lock is held is subject to concurrent updates depending on maxAge, heartbeatInterval, and caller clock drift.

in practice, a small heartbeatInterval, a large maxAge, and reasonable clock drift should be safe.

prefer to store data within the lock when possible.

install

go get github.com/nathants/go-dynamolock

usage

package main

import (
	"context"
	"time"
	"github.com/nathants/go-dynamolock"
	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)


type Data struct {
    Value string
}

func main() {
	ctx := context.Background()

	// dynamodb table
	table := "table"

	// dynamodb key
	id := "lock1"

	// after a failure to unlock/heartbeat, this much time must pass before lock is available
	maxAge := time.Second * 30

	// how often to heartbeat lock timestamp
	heartbeat := time.Second * 1

	// lock and read data
	unlock, item, err := dynamolock.Lock(ctx, table, id, maxAge, heartbeat)
	if err != nil {
		// TODO handle lock contention
		panic(err)
	}
	data := &Data{}
	err = dynamodbattribute.UnmarshalMap(item, data)
	if err != nil {
		panic(err)
	}

	// do work with the lock
	time.Sleep(time.Second * 1)
	data.Value = "updated"

	// unlock and write data
	item, err = dynamodbattribute.MarshalMap(data)
	if err != nil {
		panic(err)
	}
	err = unlock(item)
	if err != nil {
		panic(err)
	}
}

go-dynamolock's People

Contributors

nathants avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.