how to set timeout using thin server

2,713 views
Skip to first unread message

ranc

unread,
Feb 19, 2012, 7:29:38 AM2/19/12
to sinatrarb
hi,
I've encounter a strange problem when running this:

get '/' do
sleep 32
'hello world'
end
and problem is, the browser get nothing.
the magic number 32, is searched by binary_search(-_-b), when it get
smaller, the browser works happy.

Then I change the number to 60 and use tcpdump to check what
happens. It seems that Thin close connection about 30s after it
receive the request, then it log normally as if it send back the
response ..

I can't find relative config about this timeout ..

ericgj

unread,
Feb 20, 2012, 11:59:25 PM2/20/12
to sinatrarb
Not sure what you are trying to do, but Thin runs requests within an
EventMachine reactor thread, so sleep is not a good thing to do.. it
will block other requests too while sleeping.

If `sleep 32` is intended to represent a long-running task, you could
do something like

get '/' do
EM.defer { sleep 32 }
'hello world, waiting for task to end'
end

which offloads the task to a different thread. But this is for the
most simplistic cases - in most cases you want to send a response
during/after the long-running task. In those cases the Sinatra stream
helper is your best bet.

get '/' do
  stream do |out|
   11.times do |i|
     sleep(2)
     out << 'hello world'[i]
   end
  end
end

Michael Gorsuch

unread,
Feb 19, 2012, 4:15:53 PM2/19/12
to sina...@googlegroups.com
Hello -

I was able to put a sample project together witnessing this timeout
and also demonstrating a work around.

https://github.com/gorsuch/thin-timeout-example

Let me know if the README is not sufficient. I haven't jumped into
the sinatra codebase to see if there is a better way around this, but
this might work for you.

Best,

Michael Gorsuch

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
>

ranc

unread,
Feb 19, 2012, 11:58:00 PM2/19/12
to sinatrarb
Sorry to bother, but it's solved now ;)
I employ thin server directly rather than start the application with
"ruby myapp.rb".
The configure of thin server allows to set the server timeout, and
execution start point of ruby script(myapp.rb in this case).

If sinatra provide the configure of server timeout, things will be
easier, though

Carl Hörberg

unread,
Feb 19, 2012, 3:26:52 PM2/19/12
to sina...@googlegroups.com
thin is evented, you should never ever pause it's event loop, the
server will not be able to server other requests while it's paused.
That said you can change the timeout with the -t flag eg:

thin -t 60

default timeout is 30 seconds.

Reply all
Reply to author
Forward
0 new messages