{"id":19240895,"url":"https://github.com/binocarlos/locked","last_synced_at":"2025-07-11T15:43:25.381Z","repository":{"id":19407781,"uuid":"22649736","full_name":"binocarlos/locked","owner":"binocarlos","description":"distributed consensus on a single value using etcd","archived":false,"fork":false,"pushed_at":"2014-10-02T13:27:03.000Z","size":294,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-10T19:56:32.420Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binocarlos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-05T15:59:12.000Z","updated_at":"2020-12-15T05:18:32.000Z","dependencies_parsed_at":"2022-09-26T22:11:05.402Z","dependency_job_id":null,"html_url":"https://github.com/binocarlos/locked","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/binocarlos/locked","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binocarlos%2Flocked","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binocarlos%2Flocked/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binocarlos%2Flocked/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binocarlos%2Flocked/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binocarlos","download_url":"https://codeload.github.com/binocarlos/locked/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binocarlos%2Flocked/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264842951,"owners_count":23672097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T17:09:23.880Z","updated_at":"2025-07-11T15:43:25.047Z","avatar_url":"https://github.com/binocarlos.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"locked\n------\n\n[![Travis](http://img.shields.io/travis/binocarlos/locked.svg?style=flat)](https://travis-ci.org/binocarlos/locked)\n\ndistributed consensus on a single value using [etcd](http://github.com/coreos/etcd)\n\n[status: not maintained] - I've started using consul which has a [leader election](http://www.consul.io/docs/guides/leader-election.html) module\n\n## install\n\n```\n$ npm install locked\n```\n\n## usage\n\nFirst create a locker that is pointing at some nodes of your etcd cluster\n\n```js\nvar locked = require('locked')\n\n\n// create a locker pointing with an etcd connection string\nvar locker = locked('127.0.0.1:4001,127.0.0.1:4002')\n\n// create a node - this points to a single key and represents its value locked across the cluster\nvar node = locker({\n\tid:'node1',     // this is auto-completed if left blank\n\tpath:'/master', // the etcd path we use for the lock\n\tvalue:10 ,      // the value for this node\n\tttl:10          // we will check the lock every (ttl/2) seconds\n})\n\n// this is run when the values changes regardless of what node is elected\nnode.on('change', function(value, nodeid){\n\tconsole.log(value + ' was written by ' + nodeid)\n})\n\n// this is run when this node has been selected\nnode.on('select', function(){\n\tconsole.log('this server has been elected as the master!')\n})\n\n// this is run when this node is no longer the leader\nnode.on('deselect', function(){\n\tconsole.log('this server is no longer the master!')\n})\n\n// this starts the lock for this node\nnode.start()\n```\n\nyou can also pass an existing etcdjs object:\n\n```js\nvar etcdjs = require('etcdjs')\nvar locker = locked(etcdjs('127.0.0.1:4001,127.0.0.1:4002'))\n```\n\n## api\n\n#### `var locker = locked(etcdhosts)`\n\nCreate a new locker pointing to some of the etcd servers in your cluster - `etcdhosts` can be an array or a single address string.\n\n#### `var node = locker(path, opts)`\n\nCreate a node that represents a single key - other machines will have created nodes on the same path - they are all essentially competing.\n\nOpts has the following keys:\n\n * id - specify the id of this node manually, this is auto-generated if left blank\n * value - the value this node will write to the registry\n * ttl - the amount of seconds that each value is valid - the value is automatically refreshed every ttl/2 seconds\n\n#### `node.start()`\n\nStart the lock process for this node\n\n#### `node.stop()`\n\nStop the lock process for this node\n\n#### `node.id()`\n\nGet the currently active id across the lock\n\n#### `node.value()`\n\nGet the currently active value across the lock\n\n#### `node.localid()`\n\nThe id for this specific node\n\n#### `node.localvalue()`\n\nThe value for this specific node\n\n#### `node.localdata()`\n\nThe data string for this specific node\n\nThis is id + ':::' + value\n\n#### `node.isSelected()`\n\nreturn boolean indicating if the node is currently the elected leader\n\n## events\n\n#### `node.on('change', function(value, leaderid){})`\n\nThis event is triggered when the lock value has changed regardless of which node was elected.\n\nThe nodeid is of the elected machine is passed as the second argument.\n\n#### `node.on('select', function(value, leaderid){})`\n\nThis event is triggered when the node has been elected and it's value distributed to the cluster.\n\nYou can run logic in this function that should only be running on one server at a time.\n\n#### `node.on('deselect', function(){})`\n\nThis event is triggered when the node was the leader and now is not the leader (either because another node took leadership or because we were unable to refresh to leader itself)\n\n#### `node.on('refresh', function(value, leaderid){})`\n\nTriggered when the currently selected node has refreshed its value\n\n#### `node.on('ping', function(value, leaderid){})`\n\nTriggered when the leader has checked in and confirmed its leadership\n\n#### `node.on('error', function(error){})`\n\nIf there is an error connecting to the etcd instance or with the HTTP request for each leadership poll, this event will be called with the error.\n\nUpon such an error - the lock will cancel its polling for leadership and will leave you to clean up / restart the lock.\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinocarlos%2Flocked","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinocarlos%2Flocked","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinocarlos%2Flocked/lists"}