Example #1
0
func NewCommandReceiver(conn net.Conn, hostList HostList, poolMan PoolManager) *CommandReceiver {
	sock, _ := thrift.NewTNonblockingSocketConn(conn) // err is always nil, so just ignore it
	trans := thriftutils.NewTFramedTransport(sock)
	protoFac := thrift.NewTBinaryProtocolFactoryDefault()
	prot := protoFac.GetProtocol(trans)

	cmr := &CommandReceiver{}
	cmr.remoteConn = conn
	cmr.hostList = hostList
	cmr.poolMan = poolMan
	cmr.tsocket = sock
	cmr.transport = trans
	cmr.protocol = prot

	return cmr
}
Example #2
0
func Dial(node string, keyspace string, timeout time.Duration) (*CassConnection, error) {
	// resolve the address
	addr, err := net.ResolveTCPAddr("tcp", node)
	if err != nil {
		return nil, err
	}
	// get a socket
	sock, err := thrift.NewTNonblockingSocketAddr(addr)
	if err != nil {
		return nil, err
	}
	sock.SetTimeout(int64(timeout))

	// build the client
	transport := thriftutils.NewTFramedTransport(sock)
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	cli := cassandra.NewCassandraClientFactory(transport, protocolFactory)

	// connect, with a timeout of `timeout`
	ch := make(chan bool, 1)
	go func() {
		err = transport.Open()
		ch <- true
	}()
	timedOut := false
	select {
	case <-time.After(timeout):
		timedOut = true
	case <-ch:
	}
	if timedOut {
		return nil, ErrorConnectionTimeout
	}
	// get the version and check for min version and what not
	ver, err := cli.DescribeVersion()
	if err != nil {
		transport.Close()
		return nil, err
	}
	log.Print("Opened new connection, protocol version: ", ver)
	return &CassConnection{sock, transport, cli, node, keyspace, protocolFactory}, nil
}