retry_strategy.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/io/retry_strategy.h>
  6. void aws_retry_strategy_acquire(struct aws_retry_strategy *retry_strategy) {
  7. size_t old_value = aws_atomic_fetch_add_explicit(&retry_strategy->ref_count, 1, aws_memory_order_relaxed);
  8. AWS_ASSERT(old_value > 0 && "aws_retry_strategy refcount had been zero, it's invalid to use it again.");
  9. (void)old_value;
  10. }
  11. void aws_retry_strategy_release(struct aws_retry_strategy *retry_strategy) {
  12. if (retry_strategy) {
  13. size_t old_value = aws_atomic_fetch_sub_explicit(&retry_strategy->ref_count, 1, aws_memory_order_seq_cst);
  14. AWS_ASSERT(old_value > 0 && "aws_retry_strategy refcount has gone negative");
  15. if (old_value == 1) {
  16. retry_strategy->vtable->destroy(retry_strategy);
  17. }
  18. }
  19. }
  20. int aws_retry_strategy_acquire_retry_token(
  21. struct aws_retry_strategy *retry_strategy,
  22. const struct aws_byte_cursor *partition_id,
  23. aws_retry_strategy_on_retry_token_acquired_fn *on_acquired,
  24. void *user_data,
  25. uint64_t timeout_ms) {
  26. AWS_PRECONDITION(retry_strategy);
  27. AWS_PRECONDITION(retry_strategy->vtable->acquire_token);
  28. return retry_strategy->vtable->acquire_token(retry_strategy, partition_id, on_acquired, user_data, timeout_ms);
  29. }
  30. int aws_retry_strategy_schedule_retry(
  31. struct aws_retry_token *token,
  32. enum aws_retry_error_type error_type,
  33. aws_retry_strategy_on_retry_ready_fn *retry_ready,
  34. void *user_data) {
  35. AWS_PRECONDITION(token);
  36. AWS_PRECONDITION(token->retry_strategy);
  37. AWS_PRECONDITION(token->retry_strategy->vtable->schedule_retry);
  38. return token->retry_strategy->vtable->schedule_retry(token, error_type, retry_ready, user_data);
  39. }
  40. int aws_retry_token_record_success(struct aws_retry_token *token) {
  41. AWS_PRECONDITION(token);
  42. AWS_PRECONDITION(token->retry_strategy);
  43. AWS_PRECONDITION(token->retry_strategy->vtable->record_success);
  44. return token->retry_strategy->vtable->record_success(token);
  45. }
  46. void aws_retry_token_acquire(struct aws_retry_token *token) {
  47. size_t old_value = aws_atomic_fetch_add_explicit(&token->ref_count, 1u, aws_memory_order_relaxed);
  48. AWS_ASSERT(old_value > 0 && "aws_retry_token refcount had been zero, it's invalid to use it again.");
  49. (void)old_value;
  50. }
  51. void aws_retry_token_release(struct aws_retry_token *token) {
  52. if (token) {
  53. AWS_PRECONDITION(token->retry_strategy);
  54. AWS_PRECONDITION(token->retry_strategy->vtable->release_token);
  55. size_t old_value = aws_atomic_fetch_sub_explicit(&token->ref_count, 1u, aws_memory_order_seq_cst);
  56. AWS_ASSERT(old_value > 0 && "aws_retry_token refcount has gone negative");
  57. if (old_value == 1u) {
  58. token->retry_strategy->vtable->release_token(token);
  59. }
  60. }
  61. }