Sha256: 4c44a6d16fcfc0ef00336ecf58e57e6ab18646b839c28561e0bacbb271ec62b7

Contents?: true

Size: 969 Bytes

Versions: 51

Compression:

Stored size: 969 Bytes

Contents

require 'timeout'
require 'socket'
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

51 entries across 51 versions & 1 rubygems

Version Path
kurchatov-0.4.7 lib/kurchatov/riemann/client.rb
kurchatov-0.4.6 lib/kurchatov/riemann/client.rb
kurchatov-0.4.5 lib/kurchatov/riemann/client.rb
kurchatov-0.4.4 lib/kurchatov/riemann/client.rb
kurchatov-0.4.3 lib/kurchatov/riemann/client.rb
kurchatov-0.4.2 lib/kurchatov/riemann/client.rb
kurchatov-0.4.1 lib/kurchatov/riemann/client.rb
kurchatov-0.3.8 lib/kurchatov/riemann/client.rb
kurchatov-0.3.7 lib/kurchatov/riemann/client.rb
kurchatov-0.3.6 lib/kurchatov/riemann/client.rb
kurchatov-0.3.5 lib/kurchatov/riemann/client.rb
kurchatov-0.3.4 lib/kurchatov/riemann/client.rb
kurchatov-0.3.3 lib/kurchatov/riemann/client.rb
kurchatov-0.3.2 lib/kurchatov/riemann/client.rb
kurchatov-0.3.1 lib/kurchatov/riemann/client.rb
kurchatov-0.3.0 lib/kurchatov/riemann/client.rb
kurchatov-0.2.9 lib/kurchatov/riemann/client.rb
kurchatov-0.2.8 lib/kurchatov/riemann/client.rb
kurchatov-0.2.7 lib/kurchatov/riemann/client.rb
kurchatov-0.2.6 lib/kurchatov/riemann/client.rb