https://github.com/apainintheneck/time-limit
Spawn fibers with execution time limits in Crystal
https://github.com/apainintheneck/time-limit
crystal library timeout
Last synced: 9 months ago
JSON representation
Spawn fibers with execution time limits in Crystal
- Host: GitHub
- URL: https://github.com/apainintheneck/time-limit
- Owner: apainintheneck
- License: mit
- Created: 2024-04-21T21:19:45.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-01T19:31:04.000Z (almost 2 years ago)
- Last Synced: 2025-03-30T18:46:25.532Z (12 months ago)
- Topics: crystal, library, timeout
- Language: Crystal
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
time-limit:
github: apainintheneck/time-limit
```
2. Run `shards install`
## Usage
```crystal
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
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [apainintheneck](https://github.com/apainintheneck) - creator and maintainer