Sha256: 61d6574b7d8deb44dadbb72af26ccaf9cd384c299854a7412e32bf8504b5fecb

Contents?: true

Size: 1007 Bytes

Versions: 3

Compression:

Stored size: 1007 Bytes

Contents

require "timeout"
require "socket"
require "beefcake"
require "kurchatov/riemann/event"
require "kurchatov/riemann/message"

module Kurchatov
  module Riemann
    class Client

      attr_accessor :host, :port

      CONNECT_TIMEOUT = 5
      SEND_TIMEOUT = 5
      RIEMANN_PORT = 5555

      def initialize(opts = {})
        @host = opts[:host]
        @port = opts[:port] || RIEMANN_PORT
        @mutex = Mutex.new
      end

      def <<(events)
       events = events.map {|e| Event.new(e) }
       message = Message.new(:events => events)
       with_connection do |socket|
        x = message.encode_with_length
        Timeout::timeout(SEND_TIMEOUT) {
          socket.write(x)
          socket.flush
        }
       end
      end

      def with_connection
        @mutex.synchronize do
          yield(@socket || connect)
        end
      end

      def connect
        Timeout::timeout(CONNECT_TIMEOUT) {
          @socket ||= TCPSocket.new(@host, @port)
        }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kurchatov-0.0.2 lib/kurchatov/riemann/client.rb
kurchatov-0.0.2b lib/kurchatov/riemann/client.rb
kurchatov-0.0.1 lib/kurchatov/riemann/client.rb