Async endpoints for Grape APIs
Enable asyncronous endpoints to avoid blocking slow requests within EventMachine or Threads.
This can be used with any Ruby server supporting env['async.callback']
or
env['rack.hijack']
(the Rack Hijack API).
Installation
Add this line to your application's Gemfile:
gem 'grape', '>= 0.14.0'
gem 'grape-async'
Usage
class API < Grape::API
use Grape::Async
async
get do
# code to run asyncronously ...
end
end
The async
directive accepts either :em
for EventMachine based async or :threaded
for thread based async.
The default is :threaded
.
Examples
To run the provided example with Thin:
bundle exec thin start -R examples/config.ru
or with Puma
bundle exec puma examples/config.ru -t 8:32 -w 3 -b tcp://0.0.0.0:3000
Contributing
- Fork it ( https://github.com/stuart/grape-async/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