Rails takes extremely slow time to process ajax json - ruby-on-rails

It takes my rails app an insane amount time to server ajax request. This is my test code
def test
respond_to do |format|
format.json { render json: '{"text" : "simpleJson"}' }
end
end
base on server log, it take my apps only 1ms to execute request :
but in browser, it is always recieved response in about 20k+ ms
When i change the json return to nothing ( format.json { render json: {} } ) the request only takes ~18ms. So I guess rails has some trouble when sending json back to client (because server log say the method test execute very fast). But I'm not sure about that because I'm very new to ruby and rails. My localhost is run by puma 3.10.0.
Any help would be appreciated.

Change from puma server to webrick solve my issue, but after doing some google the root cause of my problem maybe because I run rails on Window Subsystem Linux. At the moment, puma is slow in WSL.

Related

discrepancy in latency between rails request logs and chrome network tab

A Rails API call shows a duration of 894m in the request logs locally, where the render looks like this: render status: :ok, json: { ... }
:status => 200,
:duration => 894.95,
:view => 7.87,
I'm rendering an array of objects (not ActiveRecord), but I didn't override as_json with crazy implementations or anything.
This is a SPA, an Angular frontend. When I open the network tab in Chrome console for the same API call, it shows that it's been waiting for 3s before the first byte:
I'm trying to speed up a page in my app and this API call is a big one. How can I figure what what is causing this discrepancy?

Different response time for post on RoR

I have a web service to which data is posted. What I do is parse the json and store the data.
And my response is
render json: { success: true, message: "ok" }, status: 200
Active record takes about 10ms to complete the inserts. There are no views so rails returns something like 0.4 ms to render them. What I find strange are the response time, that are always different (ok, 20 ms up or down is ok, but those are the response times): 130ms, 62ms, 149ms, 71ms, 77ms, 150ms, 48ms, 72ms
My app is on Heroku (I only have new relic enabled, no other request). Does anyone know why the difference or a good way to test it? Maybe because of new Relic?

Sporadic ActionController::UnknownFormat on ajax calls of a remote form

I'm currently using rails 4 remote forms with json responses:
respond_to do |format|
if read_only || #object.update_attributes(object_params)
format.json { render json: {}, status: :ok }
else
format.json { render json: #object.errors.messages, status: :unprocessable_entity }
end
end
Most of the time it works well, but sometimes, on chrome, it gives a strange ActionController::UnknownFormat error.
This is very hard to trace since it is sporadic...
Anyone experienced this issue or has a solution?
EDIT:
As requested, from logs
Started PATCH "/objects/727"
I, INFO -- : Processing by ObjectsController#update as JS
Started PATCH "/objects/727"
I, INFO -- : Processing by ObjectsController#update as HTML
it seams you are right, the failing request is not always JS, but I cannot think of a reason it doesn't always behave the same way and also why does it happen only on chrome?
I had this issue a few months back, where my error aggregator was showing me similar errors. The errors were only for users accessing the site using IE7 and earlier browsers. The existing jquery version - 2.2.0 that I was using did not have support for older browsers, so I had to downgrade jquery version to 1.9.1, which solved the problem for me.

how do you write json response destroy methods correctly in rails?

Here's the code for sending a json response associated with a working and non working status. Does anyone have recommendations for other resources? This isn't working. Thank you.
if #content.destroy
format.json { redirect_to #collection, status: :destroyed, notice 'Content was removed from collection.' }, :status => 200
else
format.json { render json #content.errors, status: :unprocessable_entity }, :status => 400
end
TL;DR: if you are using some JS framework, then look up how that framework wants success and failure response to look like, if you are rolling your own - then do whatever makes more sense for you.
Long version:
There are couple ways you can return response from rails to your web app. Rails by default returns status code 2xx for success and 4xx for failed requests. Some web frameworks such as Extjs, like to receive response with 200 code always and look at success key in the response to see if the request was successfult. If you are writing your own JS that utilizes XHR, then you can do whatever you want: its up to you how you architect your API. I find it useful to return json response from server with 200 code and set success key to true or false so in my callback JS i can write something like:
if(request.data.success){
successfulPost(response);
} else {
failed(response);
}
one of the reasons I prefer that approach is that, if there is an exception, then I can actually distinguish from record not being saved due to validation errors as opposed to bug in code that caused exception. But that is a personal preference and some may argue that error is error and it does not matter what caused it.

How to set "Connection: close" header in Rails

To fix issues with Safari frequently hanging when uploading files, I need to make a request to my Rails server and have it return an empty body with a "Connection: close" header. More details about this fix can be found here.
So far, I have tried:
def close
return head :ok, {'Connection' => 'close'}
end
def close
response.headers['Connection'] = 'close'
render :nothing => true
end
def close
response.headers['Connection'] = 'close'
return head :ok
end
None of these approaches seem to work. Inspecting the request in Firebug and Safari's developer console reveals that the response header, Connection, is always set to "keep-alive"
I'm running Rails 2.3.5 with Mongrel and Nginx. Setting a header such as Content-Type does work by the way.
Any ideas on how to fix this?
So I never figured out how to do this in Rails, but I did find out that nginx version 0.7.66+ disables keepalive connections for Safari. See the nginx changelog.
So I upgraded my nginx and all is well with Safari now.

Resources