policies.go 1.2 KB

12345678910111213141516171819202122232425262728293031
  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. //This file will be the future home for more policies
  5. package gocql
  6. //RetryableQuery is an interface that represents a query or batch statement that
  7. //exposes the correct functions for the retry policy logic to evaluate correctly.
  8. type RetryableQuery interface {
  9. Attempts() int
  10. }
  11. // RetryPolicy interace is used by gocql to determine if a query can be attempted
  12. // again after a retryable error has been received. The interface allows gocql
  13. // users to implement their own logic to determine if a query can be attempted
  14. // again.
  15. // See SimpleRetryPolicy as an example of implementing the RetryPolicy interface.
  16. type RetryPolicy interface {
  17. Attempt(RetryableQuery) bool
  18. }
  19. // SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  20. type SimpleRetryPolicy struct {
  21. NumRetries int //Number of times to retry a query
  22. }
  23. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  24. // than the NumRetries defined in the policy.
  25. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  26. return q.Attempts() <= s.NumRetries
  27. }