policies.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. GetConsistency() Consistency
  11. }
  12. // RetryPolicy interace is used by gocql to determine if a query can be attempted
  13. // again after a retryable error has been received. The interface allows gocql
  14. // users to implement their own logic to determine if a query can be attempted
  15. // again.
  16. //
  17. // See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
  18. // interface.
  19. type RetryPolicy interface {
  20. Attempt(RetryableQuery) bool
  21. }
  22. /*
  23. SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  24. See below for examples of usage:
  25. //Assign to the cluster
  26. cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
  27. //Assign to a query
  28. query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
  29. */
  30. type SimpleRetryPolicy struct {
  31. NumRetries int //Number of times to retry a query
  32. }
  33. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  34. // than the NumRetries defined in the policy.
  35. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  36. return q.Attempts() <= s.NumRetries
  37. }