So maybe this is not a good practice but at this point my web application is coexisting with its' API in the same app/ under the same server. I am wondering if there is any way to speed up how server process requests in this case?
For example, when I am sending a request to the API under the same server, I use:
require 'rest-client'
tasks = RestClient.get 'localhost:8000/tasks',
{
content_type: :json,
accept: :json,
"X-User-Email" => "blahblah@gmail.com",
"X-User-Token" => "blahblah"
}
However, sometimes it takes a ridiculously long time to get the result. Or it results in an 'Timeout reading data from server' error. I am wondering if it is because the server is sending requests to itself AND receiving requests from itself at the same time.I have tried opening another server on port 8000, send request to port 3000 server and it is so much faster. Is rails that bad at multithreading?
$ rails s -p 8000 -P 42323
Also, if I refreshed the port 8000 server page first and send the request, then refreshing the port 3000 server page would also be much faster. Is it because the response has been cached by Rack?
P.S I apologize if I used different terms incorrectly.
EDIT:
I have tried debugging from the API and the application controller. It seems like the most delay happens right before actually hitting the API controller. Server output if sending request from port 8000 to port 8000 API:
Started GET "/" for 127.0.0.1 at 2016-11-11 17:37:14 +0800
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1234567]]
Processing by TasksApplicationController#index as HTML
Started GET "/tasks" for 127.0.0.1 at 2016-11-11 17:38:14 +0800
Processing by APIController#index as JSON
So before it start loading the route for the API, ONE MINUTE has passed!
So if I tried having two local servers running and send request from port 3000 to port 8000 server, this is the output of the much faster version:
Started GET "/tasks" for 127.0.0.1 at 2016-11-11 17:42:25 +0800
Processing by APIController#index as JSON
The difference is that this part of the output is gone, and thus the 1-minute delay is gone.
Started GET "/" for 127.0.0.1 at 2016-11-11 17:37:14 +0800
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1234567]]
Processing by TasksApplicationController#index as HTML
Why is it that if I send request to port 8000 server FROM 8000 server, I have this additional part that causes so much time delay?