@openland/foundationdb

@openland/foundationdb

  • Docs
  • Chat

›Layers

Getting Started

  • Getting Started
  • Installation
  • Hello world

Entity Layer

  • Overview

Layers

  • Singleton Worker
  • Migrations
  • PubSub
  • Random ID
  • Distributed Lock

Reference

  • Database
  • Transactions
  • Subspaces
  • Directory Layer

Distributed Lock layer

Low level distributed lock that can be used to coordinate execution within your cluster. This locks can (and should) be executed inside a related transaction to guarantee absolute exclusive locking.

Dependencies

This layer does not have any depdendencies

Install

yarn install @openland/foundationdb-locks

and then add layer in your initialization code:

import { Database } from '@openland/foundationdb';
import { LockLayer } from '@openland/foundationdb-locks';

let db = await Database.open({
    layers: [
        /* other layers */
        new LockLayer()
    ]
})

Usage

import { DistributedLock } from '@openland/foundationdb-locks';

// Version is monotonic value that prohibits all locks with version 
// that is less than last known maximum version
let version = 1;

// Unique identifier. Beware this ID is global and can conflict with 
// other layers or modules in your app
let lockName = 'email-worker';

// Timeout after which lock will be automatically released
let timeout = 30000;

// Create lock object
let lock = new DistributedLock(lockName, db, version);

// Try to lock
let lockSuccessful: boolean = await lock.tryLock(ctx /* Context with transaction */, timeout);

// Release lock
await lock.releaseLock(ctx /* Context with transaction */);

// Refresh lock timeout to keep it for longer time
await lock.refresh(ctx, timeout);

← Random IDDatabase →
  • Dependencies
  • Install
  • Usage
Docs
Getting Started
Community
Chat in Openland
Sources
GitHubStar
Copyright © 2021 Openland