/vendor/bundle/jruby/2.1/gems/redis-3.0.7/lib/redis/connection/hiredis.rb

https://github.com/delowong/logstash · Ruby · 63 lines · 51 code · 11 blank · 1 comment · 4 complexity · 1bdca8b977cbb788abded9a1a181500a MD5 · raw file

  1. require "redis/connection/registry"
  2. require "redis/errors"
  3. require "hiredis/connection"
  4. require "timeout"
  5. class Redis
  6. module Connection
  7. class Hiredis
  8. def self.connect(config)
  9. connection = ::Hiredis::Connection.new
  10. if config[:scheme] == "unix"
  11. connection.connect_unix(config[:path], Integer(config[:timeout] * 1_000_000))
  12. else
  13. connection.connect(config[:host], config[:port], Integer(config[:timeout] * 1_000_000))
  14. end
  15. instance = new(connection)
  16. instance.timeout = config[:timeout]
  17. instance
  18. rescue Errno::ETIMEDOUT
  19. raise TimeoutError
  20. end
  21. def initialize(connection)
  22. @connection = connection
  23. end
  24. def connected?
  25. @connection && @connection.connected?
  26. end
  27. def timeout=(timeout)
  28. # Hiredis works with microsecond timeouts
  29. @connection.timeout = Integer(timeout * 1_000_000)
  30. end
  31. def disconnect
  32. @connection.disconnect
  33. @connection = nil
  34. end
  35. def write(command)
  36. @connection.write(command.flatten(1))
  37. rescue Errno::EAGAIN
  38. raise TimeoutError
  39. end
  40. def read
  41. reply = @connection.read
  42. reply = CommandError.new(reply.message) if reply.is_a?(RuntimeError)
  43. reply
  44. rescue Errno::EAGAIN
  45. raise TimeoutError
  46. rescue RuntimeError => err
  47. raise ProtocolError.new(err.message)
  48. end
  49. end
  50. end
  51. end
  52. Redis::Connection.drivers << Redis::Connection::Hiredis