{"id":15288811,"url":"https://github.com/rnd-soft/lusnoc","last_synced_at":"2025-04-13T08:10:43.910Z","repository":{"id":48749654,"uuid":"210049251","full_name":"RND-SOFT/lusnoc","owner":"RND-SOFT","description":"[MIRROR] Lusnoc is reliable gem to deal with consul locks and sessions","archived":false,"fork":false,"pushed_at":"2023-04-03T14:37:50.000Z","size":37,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-12T01:41:32.725Z","etag":null,"topics":["consul","distributed","distributed-computing","distributed-systems","gem","lock","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"https://br.rnds.pro/ruby/lusnoc","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RND-SOFT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-21T20:27:41.000Z","updated_at":"2023-11-09T19:14:41.000Z","dependencies_parsed_at":"2024-10-22T10:19:32.742Z","dependency_job_id":null,"html_url":"https://github.com/RND-SOFT/lusnoc","commit_stats":{"total_commits":39,"total_committers":1,"mean_commits":39.0,"dds":0.0,"last_synced_commit":"d4b3b33ab00eded5b37eacf43d4e48dd066c9ef1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RND-SOFT%2Flusnoc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RND-SOFT%2Flusnoc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RND-SOFT%2Flusnoc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RND-SOFT%2Flusnoc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RND-SOFT","download_url":"https://codeload.github.com/RND-SOFT/lusnoc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681491,"owners_count":21144700,"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":["consul","distributed","distributed-computing","distributed-systems","gem","lock","ruby","ruby-on-rails"],"created_at":"2024-09-30T15:53:16.876Z","updated_at":"2025-04-13T08:10:43.890Z","avatar_url":"https://github.com/RND-SOFT.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lusnoc\n\n[![Gem Version](https://badge.fury.io/rb/lusnoc.svg)](https://rubygems.org/gems/lusnoc)\n[![Gem](https://img.shields.io/gem/dt/lusnoc.svg)](https://rubygems.org/gems/lusnoc/versions)\n[![YARD](https://badgen.net/badge/YARD/doc/blue)](http://www.rubydoc.info/gems/lusnoc)\n\n[![Coverage](https://lysander.rnds.pro/api/v1/badges/lusnoc_coverage.svg)](https://lysander.rnds.pro/api/v1/badges/lusnoc_coverage.html)\n[![Quality](https://lysander.rnds.pro/api/v1/badges/lusnoc_quality.svg)](https://lysander.rnds.pro/api/v1/badges/lusnoc_quality.html)\n[![Outdated](https://lysander.rnds.pro/api/v1/badges/lusnoc_outdated.svg)](https://lysander.rnds.pro/api/v1/badges/lusnoc_outdated.html)\n[![Vulnerabilities](https://lysander.rnds.pro/api/v1/badges/lusnoc_vulnerable.svg)](https://lysander.rnds.pro/api/v1/badges/lusnoc_vulnerable.html)\n\nLusnoc is reliable gem to deal with [Consul](https://www.consul.io). It is designed to be simple and work without dark background magic.\nIt is inspired by [consul-mutex](https://github.com/kinnalru/consul-mutex)(which has hard background magic). \n\nArticle about gem: https://blog.rnds.pro/lusnoc (in russian)\n\n## FAQ\n\n#### What's Lusnoc for?\n\nLusnoc allows you to interact with Consul to provide distributed locks(mutex) to your application.\n\n#### What's the difference between lusnoc and [consul-mutex](https://github.com/kinnalru/consul-mutex) or [diplomat](https://github.com/WeAreFarmGeek/diplomat)\n* consul-mutex starts background thread and  ***the block of code that you pass to #synchronize runs on a separate thread, and can be killed without warning if the mutex determines that it no longer holds the lock.***\n* diplomat provides the basic session/locks functionality but no automated control over it\n\n#### How Lusnoc deal with sessions/mutexes?\n* Lusnoc ensures session creation/destruction upon block execution\n* Lusnoc uses only sessions with TTL to protect you system from stale sessions/locks\n* Lusnoc enforces you to manualy renew session(through callback or explicit check) but provide background session checker\n* Lusnoc tries to carefuly handle timeouts and expiration using Consul [blocking queries](https://www.consul.io/api/features/blocking.html)\n\n## Usage\n\nSimply instantiate a new `Lusnoc::Mutex`, giving it the key you want to use\nas the \"lock\":\n\n```ruby\n  require 'lusnoc/mutex'\n  mutex = Lusnoc::Mutex.new('/locks/mx1', ttl: 20)\n```\nTTL will be used in session creation on `#synchronize`:\n```ruby\n  mutex.synchronize(timeout: 10) do |mx|\n    puts \"We are exclusively owns resource\"\n  end\n```\nIf mutex cannot be acquired within given timeout `Lusnoc::TimeoutError` is raised.\nBy default, the \"value\" of the lock resource will be the hostname of the\nmachine that it's running on (so you know who has the lock).  If, for some\nreason, you'd like to set the value to something else, you can do that, too:\n```ruby\n  Lusnoc::Mutex.new('/some/key', value: {time: Time.now}).synchronize do |mx|\n    #...\n  end\n```\nSession invalidation/renewal handled through mutex instance:\n```ruby\n  Lusnoc::Mutex.new('/some/key').synchronize do |mx|\n    mx.time_to_expiration # seconds to session expiration in consul. \n    mx.ttl                # session ttl. \n    mx.need_renew?        # true when time_to_expiration less than half of ttl\n    \n    mx.need_renew?        # false\n    sleep (mx.ttl / 2) + 1\n    mx.need_renew?        # true\n    \n    mx.on_mutex_lost do |mutex|\n      # this callback will be called from other(guard) thread when mutex is lost(session invalidated)\n    end\n    \n    mx.locked?    # true while session is not expired or invalidated by admin\n    mx.owned?     # true while session is not expired or invalidated by admin and owner is a Thread.current\n    mx.session_id # id of Consul session\n    mx.expired?   # is session expired?\n    mx.alive?     # is session alive?\n    mx.alive!     # ensures session alive or raise Lusnoc::ExpiredError\n    mx.renew      # renew session or raise Lusnoc::ExpiredError if session already expired\n  end\n```\n\nYou can use only Session:\n```ruby\n  Session.new(\"session_name\", ttl: 20) do |session|\n    session.on_session_die do\n      # this callback will be called from other(guard) thread when session invalidated\n    end\n\n    session.expired?   # is session expired?\n    session.alive?     # is session alive?\n    session.alive!     # ensures session alive or raise Lusnoc::ExpiredError\n    session.renew      # renew session or raise Lusnoc::ExpiredError if session already expired\n  end\n```\nTypical usage scenario:\n\n```ruby\n  Lusnoc::Mutex.new('/some/key').synchronize do |mx|\n    # do some work\n    mx.renew if mx.need_renew?\n    # do other work\n    mx.renew if mx.need_renew?\n    # ...\n  rescue Lusnoc::ExpiredError =\u003e e\n    # Session was invalidated and mutex was lost!\n  end\n```\n\n## Installation\n\nIt's a gem:\n```bash\n  gem install lusnoc\n```\nThere's also the wonders of [the Gemfile](http://bundler.io):\n```ruby\n  gem 'lusnoc'\n```\n\n## Mirrors\n\n* GitFlic: [https://gitflic.ru/project/rndsoft/lusnoc](https://gitflic.ru/project/rndsoft/lusnoc)\n* Github: [https://github.com/RND-SOFT/lusnoc](https://github.com/RND-SOFT/lusnoc) \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnd-soft%2Flusnoc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frnd-soft%2Flusnoc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnd-soft%2Flusnoc/lists"}