mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob 6033adae8a0f108f2ac54694451569a392097c39 775 bytes (raw)
$ git show pipeline:lib/mogilefs/pool.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
# -*- encoding: binary -*-
require 'thread'

class MogileFS::Pool

  class BadObjectError < RuntimeError; end

  def initialize(klass, *args)
    @args = args
    @klass = klass
    @queue = Queue.new
    @objects = []
  end

  def get
    begin
      object = @queue.pop true
    rescue ThreadError
      object = @klass.new(*@args)
      @objects << object
    end
    object
  end

  def put(o)
    raise BadObjectError unless @objects.include? o
    @queue.push o
    purge
  end

  def use
    object = get
    yield object
    nil
  ensure
    put object
    nil
  end

  def purge
    return if @queue.length < 5
    begin
      until @queue.length <= 2 do
        obj = @queue.pop true
        @objects.delete obj
      end
    rescue ThreadError
    end
  end

end


git clone https://yhbt.net/mogilefs-client.git