Sha256: 54f5f27846b4c62592fe0d1e0b20b071731af4247589fd72ce892032cc7a9c87

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

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

module Prometheus
  module Client
    module Helper
      class MmapedFile < Mmap
        include FastMmapedFile
        include EntryParser
        attr_reader :filepath, :size

        def initialize(filepath, mode = 'r', protection = Mmap::MAP_SHARED, options = {})
          @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, mode, protection, options)
        end

        def used=(value)
          self[0..3] = [value].pack('l')
        end

        def close
          munmap
        end

        private

        def initial_mmap_file_size
          Prometheus::Client.configuration.initial_mmap_file_size
        end

        public

        class << self
          def open(filepath)
            MmapedFile.new(filepath, 'rw', Mmap::MAP_SHARED)
          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

3 entries across 3 versions & 1 rubygems

Version Path
prometheus-client-mmap-0.7.0.beta32 lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-0.7.0.beta31 lib/prometheus/client/helper/mmaped_file.rb
prometheus-client-mmap-0.7.0.beta30 lib/prometheus/client/helper/mmaped_file.rb