Class: HeapPeriscopeUi::UdpListener

Inherits:
Object
  • Object
show all
Defined in:
lib/heap_periscope_ui/udp_listener.rb

Constant Summary collapse

BATCH_WRITE_INTERVAL =

seconds

5
BATCH_MAX_SIZE =

records

1000

Instance Method Summary collapse

Constructor Details

#initialize(host: HeapPeriscopeUi.udp_host, port: HeapPeriscopeUi.udp_port) ⇒ UdpListener

Returns a new instance of UdpListener.



10
11
12
13
14
15
16
# File 'lib/heap_periscope_ui/udp_listener.rb', line 10

def initialize(host: HeapPeriscopeUi.udp_host, port: HeapPeriscopeUi.udp_port)
  @host = host
  @port = port
  @socket = UDPSocket.new
  @gc_report_batch = Queue.new
  @running = false
end

Instance Method Details

#runObject



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
# File 'lib/heap_periscope_ui/udp_listener.rb', line 18

def run
  @running = true
  puts "[HeapPeriscopeUi::UdpListener] Attempting to bind UDP server to #{@host}:#{@port}..." # Changed to puts
    
  @socket.bind(@host, @port)
  puts "[HeapPeriscopeUi::UdpListener] Successfully listening on UDP #{@host}:#{@port}" # Changed to puts
      
  writer_thread = start_writer_thread
      
  while @running
    begin
      data, addr = @socket.recvfrom(65536)
      Thread.new { process_packet(data, addr) }
    rescue IO::WaitReadable, Errno::EINTR
      # These errors are expected during non-blocking I/O or when the socket is interrupted.
      # Retry if the server is still supposed to be running.
      retry if @running
    rescue => e
      # Using puts for consistency if Rails.logger is problematic in this thread
      puts "[HeapPeriscopeUi::UdpListener] ERROR in receive loop: #{e.message}\n#{e.backtrace.join("\n")}"
    end
  end
      
rescue Interrupt
  puts "[UDPServer] Shutting down..."
ensure
  @running = false
  writer_thread&.join(BATCH_WRITE_INTERVAL + 2)
  write_gc_batch
  @socket.close if @socket
  puts "[UDPServer] UDP server stopped."
end