Class: Omnizip::Temp::TempFilePool

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/temp/temp_file_pool.rb

Overview

Resource pool for managing reusable temp files

Constant Summary collapse

DEFAULT_POOL_SIZE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: DEFAULT_POOL_SIZE) ⇒ TempFilePool

Create new temp file pool

Parameters:

  • size (Integer) (defaults to: DEFAULT_POOL_SIZE)

    Maximum pool size



13
14
15
16
17
18
19
# File 'lib/omnizip/temp/temp_file_pool.rb', line 13

def initialize(size: DEFAULT_POOL_SIZE)
  @size = size
  @pool = []
  @mutex = Mutex.new
  @created_count = 0
  @reuse_count = 0
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/omnizip/temp/temp_file_pool.rb', line 9

def size
  @size
end

Instance Method Details

#acquire(**options) {|temp_file| ... } ⇒ Object

Acquire temp file from pool

Yields:

  • (temp_file)

    Block called with temp file

Returns:

  • (Object)

    Block return value



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omnizip/temp/temp_file_pool.rb', line 24

def acquire(**options)
  temp_file = get_or_create(**options)

  begin
    result = yield temp_file
    release(temp_file)
    result
  rescue StandardError => e
    # Ensure cleanup even on exception
    temp_file.unlink
    raise e
  end
end

#available_countInteger

Get available file count in pool

Returns:

  • (Integer)

    Number of available files



68
69
70
# File 'lib/omnizip/temp/temp_file_pool.rb', line 68

def available_count
  @mutex.synchronize { @pool.size }
end

#clearObject

Clear all files from pool



55
56
57
58
59
60
61
62
63
64
# File 'lib/omnizip/temp/temp_file_pool.rb', line 55

def clear
  @mutex.synchronize do
    @pool.each do |tf|
      tf.unlink
    rescue StandardError
      nil
    end
    @pool.clear
  end
end

#release(temp_file) ⇒ Object

Release temp file back to pool or delete if pool is full

Parameters:

  • temp_file (TempFile)

    File to release



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/omnizip/temp/temp_file_pool.rb', line 40

def release(temp_file)
  @mutex.synchronize do
    if @pool.size < @size
      # Return to pool for reuse
      temp_file.rewind
      @pool << temp_file
      @reuse_count += 1
    else
      # Pool full, delete it
      temp_file.unlink
    end
  end
end

#statsHash

Get statistics

Returns:

  • (Hash)

    Pool statistics



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/omnizip/temp/temp_file_pool.rb', line 74

def stats
  @mutex.synchronize do
    {
      pool_size: @size,
      available: @pool.size,
      created: @created_count,
      reused: @reuse_count,
      efficiency: efficiency_ratio,
    }
  end
end