Sha256: 75952f00a648251edfcb007bedf934aeb6286f35fa20f9080b945994d3deffd0

Contents?: true

Size: 1.65 KB

Versions: 39

Compression:

Stored size: 1.65 KB

Contents

require 'prometheus/client/helper/entry_parser'
require 'prometheus/client/helper/file_locker'

# load precompiled extension if available
begin
  ruby_version = /(\d+\.\d+)/.match(RUBY_VERSION)
  require_relative "../../../#{ruby_version}/fast_mmaped_file_rs"
rescue LoadError
  require 'fast_mmaped_file_rs'
end

module Prometheus
  module Client
    module Helper
      class MmapedFile < FastMmapedFileRs
        include EntryParser

        attr_reader :filepath, :size

        def initialize(filepath)
          @filepath = filepath

          File.open(filepath, 'a+b') do |file|
            file.truncate(initial_mmap_file_size) if file.size < MINIMUM_SIZE
            @size = file.size
          end

          super(filepath)
        end

        def close
          munmap
          FileLocker.unlock(filepath)
        end

        private

        def initial_mmap_file_size
          Prometheus::Client.configuration.initial_mmap_file_size
        end

        public

        class << self
          def open(filepath)
            MmapedFile.new(filepath)
          end

          def ensure_exclusive_file(file_prefix = 'mmaped_file')
            (0..Float::INFINITY).lazy
              .map { |f_num| "#{file_prefix}_#{Prometheus::Client.pid}-#{f_num}.db" }
              .map { |filename| File.join(Prometheus::Client.configuration.multiprocess_files_dir, filename) }
              .find { |path| Helper::FileLocker.lock_to_process(path) }
          end

          def open_exclusive_file(file_prefix = 'mmaped_file')
            filename = Helper::MmapedFile.ensure_exclusive_file(file_prefix)
            open(filename)
          end
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
vinted-prometheus-client-mmap-1.7.0 lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.7.0-x86_64-linux lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.6.0 lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.6.0-x86_64-linux lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.5.0-x86_64-linux lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.4.0 lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.3.0 lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.2.2 lib/prometheus/client/helper/mmaped_file.rb
vinted-prometheus-client-mmap-1.2.1 lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.1 lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.1-x86_64-linux lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.1-x86_64-darwin lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.1-arm64-darwin lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.1-aarch64-linux lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.0 lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.0-x86_64-linux lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.0-x86_64-darwin lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.0-arm64-darwin lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.1.0-aarch64-linux lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-1.0.3 lib/prometheus/client/helper/mmaped_file.rb