mongolock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 11 Imported by: 0

README

Distributed locks

Available drivers:

  • MongoDB
  • Redis red locks.
  • Etcd.

Note: currently Mongodb is good for us, but if someday we needed to the redis red locks or etcd, we can implement them.

Docs

You can lock everything between multiple instances of your app using distributed locks. for example if you want to run a DB migration using at you app's bootstrap time, to prevent for multiple run of that migration by each instance of the app on your server(say pods in a k8s cluster, e.g., pod-my-app-893yr) you need to lock migration on one instance and other instances need to wait for that lock to release, after that other instances can check and if migration has been done by another instance, so they skip it and run the app.

We have two important concepts:

  • DLM(distributed lock manager): manges locks in our apps.
  • Mutex: Lock instance. We create mutex by DLM. Each mutex has three fields:
    • name (key): the lock name.
    • owner: owner (e.g., machine that acquires the lock) of the lock.
    • ttl: The lock ttl.

Rules:

  • Different mutexes with same lock name and same owner can lock and unlock each other.

How to use?

Just create a new instance of DLM in your app as a service and everywhere that you needed you can create a new mutex using your dlm. Please note at dlm creation time, use your host name as owner of the lock If you want to have different owners for each instance of the app.
To see Examples check test files please.

Documentation

Index

Constants

View Source
const CollectionName = "locks"

CollectionName is default collection name.

Variables

This section is empty.

Functions

func NewDlm

func NewDlm(o DlmOptions) (hexa.DLM, error)

Types

type DlmOptions

type DlmOptions struct {
	Collection *mongo.Collection
	// If a lock already held by another mutex, we need to interval
	// to check it again.
	WaitingInterval time.Duration
	// Default ttl value for a lock. e.g, 2s.
	DefaultTTL time.Duration
	// usually your machine name. e.g., k8s-pod-199831uf.
	DefaultOwner string
}

Jump to

Keyboard shortcuts

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