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
	}

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

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

	s := &Session{
		Pool:     pool,
		cons:     cfg.Consistency,
		prefetch: 0.25,
		cfg:      cfg,
	}

	//See if there are any connections in the pool
	if pool.Size() > 0 {
		s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)

		s.SetConsistency(cfg.Consistency)
		s.SetPageSize(cfg.PageSize)

		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
	}

	s.Close()

	return nil, ErrNoConnectionsStarted
}