time-limit
time-limit
A simple helper to allow you to run logic in a fiber with a time limit. All return values and exceptions are propogated from the spawned fiber back to the original fiber.
Installation
-
Add the dependency to your
shard.yml
:dependencies: time-limit: github: apainintheneck/time-limit
-
Run
shards install
Usage
require "time-limit"
# When the calculation completes in the time limit,
# the return value from the block is returned.
result = TimeLimit.spawn(5.seconds) do
5 * 5 * 5 * 5 * 5
end
puts result # => 3125
# When the calculation raises an error,
# it gets re-raised.
begin
TimeLimit.spawn(5.seconds) do
raise ArgumentError.new
end
rescue ex
puts ex.class # => ArgumentError
end
# When the calculation goes beyond the time limit,
# a timeout exception is raised.
begin
TimeLimit.spawn(5.seconds) do
sleep 10.seconds
end
rescue ex
puts ex.class # => TimeLimit::TimeoutException
end
See the specs for more examples.
Development
crystal spec
is used for testing and crystal tool format
is used for linting.
Contributing
- Fork it (https://github.com/your-github-user/time-limit/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- apainintheneck - creator and maintainer
Repository
time-limit
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- 5 months ago
- April 21, 2024
License
MIT License
Links
Synced at
Sun, 17 Nov 2024 04:54:40 GMT
Languages