Programming language: Go
License: Apache License 2.0

go-mysql-lock alternatives and similar packages

Based on the "Distributed Systems" category.
Alternatively, view go-mysql-lock alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of go-mysql-lock or a related project?

Add another 'Distributed Systems' Package



GoDoc Azure DevOps builds Azure DevOps coverage Go Report Card

go-mysql-lock provides locking primitive based on MySQL's GET_LOCK Lock names are strings and MySQL enforces a maximum length on lock names of 64 characters.

Use cases

Though there are mature locking primitives provided by systems like Zookeeper and etcd, when you have an application which is primarily dependent on MySQL for its uptime and health, added resiliency provided by systems just mentioned doesn't add much benefit. go-mysql-lock helps when you have multiple application instances which are backed by a common mysql instance and you want only one of those application instances to hold a lock and do certain tasks.


go get github.com/sanketplus/go-mysql-lock


package main

import (

    _ "github.com/go-sql-driver/mysql"

func main() {
    db, _ := sql.Open("mysql", "[email protected](localhost:3306)/dyno_test")

    locker := gomysqllock.NewMysqlLocker(db)

    lock, _ := locker.Obtain("foo")


Customizable Refresh Period

Once the lock is obtained, a goroutine periodically (default every 1 second) keeps pinging on connection since the lock is valid on a connection(session). To configure the refresh interval

locker := gomysqllock.NewMysqlLocker(db, gomysqllock.WithRefreshInterval(time.Millisecond*500))

Obtain Lock With Context

By default, an attempt to obtain a lock is backed by background context. That means the Obtain call would block indefinitely. Optionally, an Obtain call can be made with user given context which will get cancelled with the given context. The following call will give up after a second if the lock was not obtained.

locker := gomysqllock.NewMysqlLocker(db)
ctxShort, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
lock, err := locker.ObtainContext(ctxShort, "key")

Know When The Lock is Lost

Obtained lock has a context which is cancelled if the lock is lost. This is determined while a goroutine keeps pinging the connection. If there is an error while pinging, assuming connection has an error, the context is cancelled. And the lock owner gets notified of the lost lock.

context := lock.GetContext()