Class: MultipleMan::ChannelMaintenance::GC

Inherits:
Object
  • Object
show all
Defined in:
lib/multiple_man/channel_maintenance/gc.rb

Defined Under Namespace

Classes: AddCommand, RemoveCommand, SweepCommand

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_, reaper) ⇒ GC

Returns a new instance of GC.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/multiple_man/channel_maintenance/gc.rb', line 8

def initialize(_, reaper)
  @reaper = reaper
  @queue = Queue.new

  @executor = Thread.new do
    channels_by_thread = Hash.new {|h, k| h[k] = [] }
    loop do
      begin
        command = queue.pop
        command.execute(channels_by_thread)
      rescue
        puts "Sweeper died", $!
      end
    end
  end

  @sweeper_thread = Thread.new do
    loop do
      sleep 15
      queue << SweepCommand.new(queue, reaper)
    end
  end
end

Class Method Details

.finalizer(thread_id, channel, queue, reaper) ⇒ Object



4
5
6
# File 'lib/multiple_man/channel_maintenance/gc.rb', line 4

def self.finalizer(thread_id, channel, queue, reaper)
  proc { queue << RemoveCommand.new(thread_id); reaper.push(channel) }
end

Instance Method Details

#push(channel) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/multiple_man/channel_maintenance/gc.rb', line 32

def push(channel)
  thread_id = Thread.current.object_id

  finalizer = self.class.finalizer(thread_id, channel, queue, reaper)
  ObjectSpace.define_finalizer(Thread.current, finalizer)

  queue << AddCommand.new(thread_id, channel)

  puts "Opened channel #{channel.number}"
  self
end

#stopObject



44
45
46
47
48
49
50
51
52
# File 'lib/multiple_man/channel_maintenance/gc.rb', line 44

def stop
  executor.kill
  executor.join

  sweeper_thread.kill
  sweeper_thread.join

  reaper.stop
end