Sha256: 041975fd8714f19a1fd34178c51e3d8d7ade2a8dafb48f09ab9c5972d731aa3f
Contents?: true
Size: 1.73 KB
Versions: 4
Compression:
Stored size: 1.73 KB
Contents
module CassandraObject module AsyncConnection extend ActiveSupport::Concern included do class_attribute :connection_spec class_eval do @@fiber_connections = {} def self.connection() @@fiber_connections[Fiber.current.object_id] ||= begin spec = connection_spec.dup if const_defined?(:EM) && EM.reactor_running? require 'thrift_client/event_machine' spec[:thrift].merge!(:transport => Thrift::EventMachineTransport, :transport_wrapper => nil) end Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift]) end end def self.connection?() !!connection end def self.disconnect! @@fiber_connections.delete(Fiber.current.object_id).tap { |conn| conn.disconnect! if conn } end def connection defined?(@connection) ? @connection : singleton_class.connection end def connection? !!connection end end end module ClassMethods DEFAULT_OPTIONS = { servers: "127.0.0.1:9160", } DEFAULT_THRIFT_OPTIONS = { exception_class_overrides: [], } # This doesn't open a connection. It merely conifgures the connection object. def establish_connection(config) spec = config.reverse_merge(DEFAULT_OPTIONS) spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS) spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize) self.connection_spec = spec end end end end
Version data entries
4 entries across 4 versions & 1 rubygems