Sha256: 8d289fc5f373f5f432774d897247245d34fb7736bfd4b71442f489131d82769f

Contents?: true

Size: 1.88 KB

Versions: 11

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2018 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'futex'
require_relative 'log'

# Sync collection of wallets.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module Zold
  # Synchronized collection of wallets
  class SyncWallets
    def initialize(wallets, timeout: 30, log: Log::NULL, dir: wallets.path)
      @wallets = wallets
      @log = log
      @timeout = timeout
      @dir = dir
    end

    def to_s
      @wallets.to_s
    end

    def path
      @wallets.path
    end

    def all
      @wallets.all
    end

    def acq(id, exclusive: false)
      @wallets.acq(id, exclusive: exclusive) do |wallet|
        Futex.new(wallet.path, log: @log, lock: File.join(@dir, "#{id}.lock")).open(exclusive) do
          yield wallet
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
zold-0.17.10 lib/zold/sync_wallets.rb
zold-0.17.9 lib/zold/sync_wallets.rb
zold-0.17.8 lib/zold/sync_wallets.rb
zold-0.17.7 lib/zold/sync_wallets.rb
zold-0.17.6 lib/zold/sync_wallets.rb
zold-0.17.5 lib/zold/sync_wallets.rb
zold-0.17.4 lib/zold/sync_wallets.rb
zold-0.17.3 lib/zold/sync_wallets.rb
zold-0.17.2 lib/zold/sync_wallets.rb
zold-0.17.1 lib/zold/sync_wallets.rb
zold-0.17.0 lib/zold/sync_wallets.rb