control.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package gocql
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync/atomic"
  6. "time"
  7. )
  8. type controlConn struct {
  9. connecting uint64
  10. session *Session
  11. conn atomic.Value
  12. retry RetryPolicy
  13. quit chan struct{}
  14. }
  15. func createControlConn(session *Session) *controlConn {
  16. control := &controlConn{
  17. session: session,
  18. quit: make(chan struct{}),
  19. retry: &SimpleRetryPolicy{NumRetries: 3},
  20. }
  21. control.conn.Store((*Conn)(nil))
  22. go control.heartBeat()
  23. return control
  24. }
  25. func (c *controlConn) heartBeat() {
  26. for {
  27. select {
  28. case <-c.quit:
  29. return
  30. case <-time.After(5 * time.Second):
  31. }
  32. resp, err := c.writeFrame(&writeOptionsFrame{})
  33. if err != nil {
  34. goto reconn
  35. }
  36. switch resp.(type) {
  37. case *supportedFrame:
  38. continue
  39. case error:
  40. goto reconn
  41. default:
  42. panic(fmt.Sprintf("gocql: unknown frame in response to options: %T", resp))
  43. }
  44. reconn:
  45. c.reconnect(true)
  46. // time.Sleep(5 * time.Second)
  47. continue
  48. }
  49. }
  50. func (c *controlConn) reconnect(refreshring bool) {
  51. if !atomic.CompareAndSwapUint64(&c.connecting, 0, 1) {
  52. return
  53. }
  54. success := false
  55. defer func() {
  56. // debounce reconnect a little
  57. if success {
  58. go func() {
  59. time.Sleep(500 * time.Millisecond)
  60. atomic.StoreUint64(&c.connecting, 0)
  61. }()
  62. } else {
  63. atomic.StoreUint64(&c.connecting, 0)
  64. }
  65. }()
  66. oldConn := c.conn.Load().(*Conn)
  67. // TODO: should have our own roundrobbin for hosts so that we can try each
  68. // in succession and guantee that we get a different host each time.
  69. host, conn := c.session.pool.Pick(nil)
  70. if conn == nil {
  71. return
  72. }
  73. newConn, err := Connect(conn.addr, conn.cfg, c, c.session)
  74. if err != nil {
  75. host.Mark(err)
  76. // TODO: add log handler for things like this
  77. return
  78. }
  79. host.Mark(nil)
  80. c.conn.Store(newConn)
  81. success = true
  82. if oldConn != nil {
  83. oldConn.Close()
  84. }
  85. if refreshring && c.session.cfg.DiscoverHosts {
  86. c.session.hostSource.refreshRing()
  87. }
  88. }
  89. func (c *controlConn) HandleError(conn *Conn, err error, closed bool) {
  90. if !closed {
  91. return
  92. }
  93. oldConn := c.conn.Load().(*Conn)
  94. if oldConn != conn {
  95. return
  96. }
  97. c.reconnect(true)
  98. }
  99. func (c *controlConn) writeFrame(w frameWriter) (frame, error) {
  100. conn := c.conn.Load().(*Conn)
  101. if conn == nil {
  102. return nil, errNoControl
  103. }
  104. framer, err := conn.exec(w, nil)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return framer.parseFrame()
  109. }
  110. func (c *controlConn) withConn(fn func(*Conn) *Iter) *Iter {
  111. const maxConnectAttempts = 5
  112. connectAttempts := 0
  113. for i := 0; i < maxConnectAttempts; i++ {
  114. conn := c.conn.Load().(*Conn)
  115. if conn == nil {
  116. if connectAttempts > maxConnectAttempts {
  117. break
  118. }
  119. connectAttempts++
  120. c.reconnect(false)
  121. continue
  122. }
  123. return fn(conn)
  124. }
  125. return &Iter{err: errNoControl}
  126. }
  127. // query will return nil if the connection is closed or nil
  128. func (c *controlConn) query(statement string, values ...interface{}) (iter *Iter) {
  129. q := c.session.Query(statement, values...).Consistency(One)
  130. for {
  131. iter = c.withConn(func(conn *Conn) *Iter {
  132. return conn.executeQuery(q)
  133. })
  134. q.attempts++
  135. if iter.err == nil || !c.retry.Attempt(q) {
  136. break
  137. }
  138. }
  139. return
  140. }
  141. func (c *controlConn) awaitSchemaAgreement() error {
  142. return c.withConn(func(conn *Conn) *Iter {
  143. return &Iter{err: conn.awaitSchemaAgreement()}
  144. }).err
  145. }
  146. func (c *controlConn) addr() string {
  147. conn := c.conn.Load().(*Conn)
  148. if conn == nil {
  149. return ""
  150. }
  151. return conn.addr
  152. }
  153. func (c *controlConn) close() {
  154. // TODO: handle more gracefully
  155. close(c.quit)
  156. }
  157. var errNoControl = errors.New("gocql: no control connection available")