cluster.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  29. // NewCluster generates a new config for the default cluster implementation.
  30. func NewCluster(hosts ...string) *ClusterConfig {
  31. cfg := &ClusterConfig{
  32. Hosts: hosts,
  33. CQLVersion: "3.0.0",
  34. ProtoVersion: 2,
  35. Timeout: 600 * time.Millisecond,
  36. DefaultPort: 9042,
  37. NumConns: 2,
  38. NumStreams: 128,
  39. Consistency: Quorum,
  40. ConnPoolType: NewSimplePool,
  41. }
  42. return cfg
  43. }
  44. // CreateSession initializes the cluster based on this config and returns a
  45. // session object that can be used to interact with the database.
  46. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  47. //Check that hosts in the ClusterConfig is not empty
  48. if len(cfg.Hosts) < 1 {
  49. return nil, ErrNoHosts
  50. }
  51. pool := cfg.ConnPoolType(cfg)
  52. //See if there are any connections in the pool
  53. if pool.Size() > 0 {
  54. s := NewSession(pool, *cfg)
  55. s.SetConsistency(cfg.Consistency)
  56. return s, nil
  57. }
  58. pool.Close()
  59. return nil, ErrNoConnectionsStarted
  60. }
  61. var (
  62. ErrNoHosts = errors.New("no hosts provided")
  63. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  64. )