policies.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. //
  16. // See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
  17. // interface.
  18. type RetryPolicy interface {
  19. Attempt(RetryableQuery) bool
  20. }
  21. /*
  22. SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  23. See below for examples of usage:
  24. //Assign to the cluster
  25. cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
  26. //Assign to a query
  27. query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
  28. */
  29. type SimpleRetryPolicy struct {
  30. NumRetries int //Number of times to retry a query
  31. }
  32. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  33. // than the NumRetries defined in the policy.
  34. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  35. return q.Attempts() <= s.NumRetries
  36. }