Class: Omnizip::Temp::TempFilePool
- Inherits:
-
Object
- Object
- Omnizip::Temp::TempFilePool
- 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
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#acquire(**options) {|temp_file| ... } ⇒ Object
Acquire temp file from pool.
-
#available_count ⇒ Integer
Get available file count in pool.
-
#clear ⇒ Object
Clear all files from pool.
-
#initialize(size: DEFAULT_POOL_SIZE) ⇒ TempFilePool
constructor
Create new temp file pool.
-
#release(temp_file) ⇒ Object
Release temp file back to pool or delete if pool is full.
-
#stats ⇒ Hash
Get statistics.
Constructor Details
#initialize(size: DEFAULT_POOL_SIZE) ⇒ TempFilePool
Create new temp file pool
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
#size ⇒ Object (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
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(**) temp_file = get_or_create(**) 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_count ⇒ Integer
Get available file count in pool
68 69 70 |
# File 'lib/omnizip/temp/temp_file_pool.rb', line 68 def available_count @mutex.synchronize { @pool.size } end |
#clear ⇒ Object
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
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 |
#stats ⇒ Hash
Get 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 |