time-limit

Spawn fibers with execution time limits in Crystal

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:

    dependencies:
      time-limit:
        github: apainintheneck/time-limit
    
  2. 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

  1. Fork it (https://github.com/your-github-user/time-limit/fork)
  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

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