cluster.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "errors"
  7. "time"
  8. )
  9. // ClusterConfig is a struct to configure the default cluster implementation
  10. // of gocoql. It has a varity of attributes that can be used to modify the
  11. // behavior to fit the most common use cases. Applications that requre a
  12. // different setup must implement their own cluster.
  13. type ClusterConfig struct {
  14. Hosts []string // addresses for the initial connections
  15. CQLVersion string // CQL version (default: 3.0.0)
  16. ProtoVersion int // version of the native protocol (default: 2)
  17. Timeout time.Duration // connection timeout (default: 600ms)
  18. DefaultPort int // default port (default: 9042)
  19. Keyspace string // initial keyspace (optional)
  20. NumConns int // number of connections per host (default: 2)
  21. NumStreams int // number of streams per connection (default: 128)
  22. Consistency Consistency // default consistency level (default: Quorum)
  23. Compressor Compressor // compression algorithm (default: nil)
  24. Authenticator Authenticator // authenticator (default: nil)
  25. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  26. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  27. ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
  28. DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
  29. }
  30. // NewCluster generates a new config for the default cluster implementation.
  31. func NewCluster(hosts ...string) *ClusterConfig {
  32. cfg := &ClusterConfig{
  33. Hosts: hosts,
  34. CQLVersion: "3.0.0",
  35. ProtoVersion: 2,
  36. Timeout: 600 * time.Millisecond,
  37. DefaultPort: 9042,
  38. NumConns: 2,
  39. NumStreams: 128,
  40. Consistency: Quorum,
  41. ConnPoolType: NewSimplePool,
  42. DiscoverHosts: false,
  43. }
  44. return cfg
  45. }
  46. // CreateSession initializes the cluster based on this config and returns a
  47. // session object that can be used to interact with the database.
  48. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  49. //Check that hosts in the ClusterConfig is not empty
  50. if len(cfg.Hosts) < 1 {
  51. return nil, ErrNoHosts
  52. }
  53. pool := cfg.ConnPoolType(cfg)
  54. //See if there are any connections in the pool
  55. if pool.Size() > 0 {
  56. s := NewSession(pool, *cfg)
  57. s.SetConsistency(cfg.Consistency)
  58. if cfg.DiscoverHosts {
  59. //Fill out cfg.Hosts
  60. query := "SELECT peer FROM system.peers"
  61. peers := s.Query(query).Iter()
  62. var ip string
  63. for peers.Scan(&ip) {
  64. exists := false
  65. for ii := 0; ii < len(cfg.Hosts); ii++ {
  66. if cfg.Hosts[ii] == ip {
  67. exists = true
  68. }
  69. }
  70. if !exists {
  71. cfg.Hosts = append(cfg.Hosts, ip)
  72. }
  73. }
  74. if err := peers.Close(); err != nil {
  75. return s, ErrHostQueryFailed
  76. }
  77. }
  78. return s, nil
  79. }
  80. pool.Close()
  81. return nil, ErrNoConnectionsStarted
  82. }
  83. var (
  84. ErrNoHosts = errors.New("no hosts provided")
  85. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  86. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  87. )