This is a header-only library that provides a simple and efficient way to retry functions based on their return values. The API is straightforward: users pass the target function, a list of values that will trigger a retry if the return value matches, the maximum number of attempts or retry period, and the list of arguments originally passed to their function.
- Supports retrying various function types, including free functions, static member functions,
std::function
objects, and anonymous lambdas. - Handles both value and reference return types.
- Allows customization of the maximum retry period or the number of retry attempts.
- Provides options to set custom retry intervals.
The retry_until
and retry_at_intervals_until
functions retry the user function until the return value no longer matches the specified list or the attempt/time limit is reached. Examples:
// User's function
Status join_room(const Member& member, int member_id) {
// Function implementation
}
// Retry
Member user{"Alice"};
Status status = tsejhuang::retry_until(join_room, {Status::Busy, Status::MaxUsersReached}, std::chrono::seconds{10}, user, 1);
Status status = tsejhuang::retry_until(join_room, {Status::Busy, Status::MaxUsersReached}, 10, user, 1);
Status status = tsejhuang::retry_at_intervals_until(join_room, {Status::Busy, Status::MaxUsersReached}, std::chrono::seconds{10}, std::chrono::seconds{2}, user, 1);
Status status = tsejhuang::retry_at_intervals_until(join_room, {Status::Busy, Status::MaxUsersReached}, 10, std::chrono::seconds{2}, user, 1);
If the return type of the user function is a reference, wrap each value in the list with std::ref
or std::cref
:
// User's function
const Member& get_current_speaker() {
// Function implementation
}
// Retry
Member me{"Alice"};
Member other_user1{"Bob"};
Member other_user2{"Carol"};
const auto& speaker = tsejhuang::retry_at_intervals_until(get_current_speaker, {std::cref(other_user1), std::cref(other_user2)}, std::chrono::seconds{10}, std::chrono::seconds{2});
if (speaker != me) {
// Retry failure
}
See retry.hpp for API documentation and tests for example usage.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m "Add feature-name"
. - Push the branch:
git push origin feature-name
. - Submit a pull request.
This project is licensed under the MIT License.