Sha256: 63c9a9fa1852f7789f737864a9352aaa337dc13198bdbcff1955bc7c40173ddc
Contents?: true
Size: 1000 Bytes
Versions: 9
Compression:
Stored size: 1000 Bytes
Contents
require 'socket' require 'fcntl' module PulseMeter module CommandAggregator class UDP def initialize(host, port = nil) @servers = if host.is_a?(Array) host else [[host, port]] end raise ArgumentError, "No servers specified" if @servers.empty? @buffer = [] @in_multi = false @sock = UDPSocket.new @sock.fcntl(Fcntl::F_SETFL, @sock.fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK) end def multi @in_multi = true yield ensure @in_multi = false send_buffer end def method_missing(*args) @buffer << args send_buffer unless @in_multi end private def send_buffer data = @buffer.to_json @sock.send(data, 0, *@servers.sample) rescue StandardError => e PulseMeter.error "error sending data: #{e}, #{e.backtrace.join("\n")}" ensure @buffer = [] end end end end
Version data entries
9 entries across 9 versions & 2 rubygems