Example #1
0
func initStmtsLRU(max int) {
	if stmtsLRU.lru != nil {
		stmtsLRU.Max(max)
	} else {
		stmtsLRU.lru = lru.New(max)
	}
}
Example #2
0
// NewSession wraps an existing Node.
func NewSession(cfg ClusterConfig) (*Session, error) {
	//Check that hosts in the ClusterConfig is not empty
	if len(cfg.Hosts) < 1 {
		return nil, ErrNoHosts
	}

	maxStreams := 128
	if cfg.ProtoVersion > protoVersion2 {
		maxStreams = 32768
	}

	if cfg.NumStreams <= 0 || cfg.NumStreams > maxStreams {
		cfg.NumStreams = maxStreams
	}

	//Adjust the size of the prepared statements cache to match the latest configuration
	stmtsLRU.Lock()
	initStmtsLRU(cfg.MaxPreparedStmts)
	stmtsLRU.Unlock()

	s := &Session{
		cons:     cfg.Consistency,
		prefetch: 0.25,
		cfg:      cfg,
		pageSize: cfg.PageSize,
	}

	pool, err := cfg.PoolConfig.buildPool(&s.cfg)
	if err != nil {
		return nil, err
	}
	s.pool = pool

	//See if there are any connections in the pool
	if pool.Size() == 0 {
		s.Close()
		return nil, ErrNoConnectionsStarted
	}

	s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)

	if !cfg.disableControlConn {
		s.control = createControlConn(s)
	}

	if cfg.DiscoverHosts {
		s.hostSource = &ringDescriber{
			session:    s,
			dcFilter:   cfg.Discovery.DcFilter,
			rackFilter: cfg.Discovery.RackFilter,
			closeChan:  make(chan bool),
		}

		go s.hostSource.run(cfg.Discovery.Sleep)
	}

	return s, nil
}
Example #3
0
// NewSession wraps an existing Node.
func NewSession(cfg ClusterConfig) (*Session, error) {
	//Check that hosts in the ClusterConfig is not empty
	if len(cfg.Hosts) < 1 {
		return nil, ErrNoHosts
	}

	maxStreams := 128
	if cfg.ProtoVersion > protoVersion2 {
		maxStreams = 32768
	}

	if cfg.NumStreams <= 0 || cfg.NumStreams > maxStreams {
		cfg.NumStreams = maxStreams
	}

	//Adjust the size of the prepared statements cache to match the latest configuration
	stmtsLRU.Lock()
	initStmtsLRU(cfg.MaxPreparedStmts)
	stmtsLRU.Unlock()

	s := &Session{
		cons:     cfg.Consistency,
		prefetch: 0.25,
		cfg:      cfg,
		pageSize: cfg.PageSize,
	}

	pool, err := cfg.PoolConfig.buildPool(&s.cfg)
	if err != nil {
		return nil, err
	}
	s.pool = pool

	// See if there are any connections in the pool
	if pool.Size() == 0 {
		s.Close()
		return nil, ErrNoConnectionsStarted
	}

	s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)

	// I think it might be a good idea to simplify this and make it always discover
	// hosts, maybe with more filters.
	if cfg.DiscoverHosts {
		s.hostSource = &ringDescriber{
			session:    s,
			dcFilter:   cfg.Discovery.DcFilter,
			rackFilter: cfg.Discovery.RackFilter,
			closeChan:  make(chan bool),
		}
	}

	if !cfg.disableControlConn {
		s.control = createControlConn(s)
		s.control.reconnect(false)

		// need to setup host source to check for rpc_address in system.local
		localHasRPCAddr, err := checkSystemLocal(s.control)
		if err != nil {
			log.Printf("gocql: unable to verify if system.local table contains rpc_address, falling back to connection address: %v", err)
		}

		if cfg.DiscoverHosts {
			s.hostSource.localHasRpcAddr = localHasRPCAddr
		}
	}

	if cfg.DiscoverHosts {
		s.hostSource.refreshRing()
		go s.hostSource.run(cfg.Discovery.Sleep)
	}

	return s, nil
}