-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update examples to remove usage of Rainbows #1853
Conversation
You can look at my branch where I switched the integration tests to Puma (and kept Rainbows for when Rack 2 is used): master...dentarg:sinatra:rack-3
I really think we should use Puma in the examples, not Thin. I recall that being discussed before and I think there was agreement from @jkowens |
Exactly, the chat example doesn't work with Puma, that's why I used Thin instead.
Nice! Didn't know you were working on it. What do you think on removing Rainbows completely? Do you think is worth to run tests with it? |
I'd be in favor or removing Rainbows completely if others agree 🧹 ✨ |
Me too! ⬆️ |
We can always update the examples to not use |
@dentarg I think maybe we could also consider a similar implementation as Roda: What we have doesn't seem too far off. |
Sure, that sounds great. I tried your branch locally, |
Yeah I had the same issue shutting down the server. Puma won’t shut down if there are open connections. I think there would have to be code to trap SIGINT and close all the open connections, or possibly have a heartbeat event that will close stale connections.
|
Thin hasn't been updated to support Rack 3: https://github.com/macournoyer/thin/blob/v1.8.1/thin.gemspec#L25 so I think we should to find a way to use Puma or Falcon or drop these examples or something |
@jkowens Can you walk me through the changes in your branch? I don't think I'm following them fully diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb
index 63a70c75..03b41c1c 100644
--- a/lib/sinatra/base.rb
+++ b/lib/sinatra/base.rb
@@ -477,8 +477,9 @@ module Sinatra
@back.call(self)
rescue Exception => e
@scheduler.schedule { raise e }
+ ensure
+ close
end
- close unless @keep_open
end
end
@@ -509,7 +510,16 @@ module Sinatra
def stream(keep_open = false)
scheduler = env['async.callback'] ? EventMachine : Stream
current = @params.dup
- body Stream.new(scheduler, keep_open) { |out| with_params(current) { yield(out) } }
+ block = if scheduler == Stream && keep_open
+ proc do |out|
+ until out.closed?
+ with_params(current) { yield(out) }
+ end
+ end
+ else
+ proc { |out| with_params(current) { yield(out) } }
+ end
+ body Stream.new(scheduler, keep_open, &block)
end
# Specify response freshness policy for HTTP caches (Cache-Control header). I tried those changes and the chat example as a modular Sinatra app # app.rb
require 'sinatra/base'
$connections = Set.new
class App < Sinatra::Base
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
$connections << out
out.callback { $connections.delete(out) }
sleep 1
end
end
post '/' do
$connections.each do |out|
out << "data: #{params[:msg]}\n\n"
rescue
out.close
end
204 # response without entity body
end
end
# config.ru
require_relative "app"
run App and I started Puma 6.0.1 with |
@dentarg I modified that branch a bit to clean up some issues. For app servers that don't use EventMachine, it creates a loop to hold the connection open (when keep_open specified). I also modified the chat example to send a "heartbeat". That way if the browser window is closed, the connection should be closed and Puma will be able to shut down. I tried to test it with Falcon too, but it doesn't seem to work. I'm not sure if Falcon buffers the response or if I'm just doing something wrong 😵💫 |
The Rainbows server isn't maintained so IMO we shouldn't use it for example or running integration tests with it (if you agree I can remove them in another PR)
Here I've updated the chat example, so Thin is used instead. Thin was deprecated some time ago because work on it stopped, but it's being updated again and even Rack 3 is being worked on.