Ajax request speed issue












1















I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.



def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end


The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.




-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------


But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.




-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------


I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?










share|improve this question




















  • 1





    Try setting request.session_options[:skip] = true

    – razvans
    Nov 21 '18 at 13:03











  • It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

    – david0116
    Nov 21 '18 at 18:03











  • Did you add it before if session[:expires_at].nil? ?

    – razvans
    Nov 22 '18 at 8:52











  • Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

    – david0116
    Nov 22 '18 at 9:28


















1















I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.



def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end


The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.




-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------


But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.




-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------


I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?










share|improve this question




















  • 1





    Try setting request.session_options[:skip] = true

    – razvans
    Nov 21 '18 at 13:03











  • It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

    – david0116
    Nov 21 '18 at 18:03











  • Did you add it before if session[:expires_at].nil? ?

    – razvans
    Nov 22 '18 at 8:52











  • Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

    – david0116
    Nov 22 '18 at 9:28
















1












1








1


1






I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.



def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end


The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.




-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------


But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.




-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------


I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?










share|improve this question
















I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.



def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end


The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.




-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------


But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.




-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------


I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?







ruby-on-rails ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:09







david0116

















asked Nov 21 '18 at 7:01









david0116david0116

318




318








  • 1





    Try setting request.session_options[:skip] = true

    – razvans
    Nov 21 '18 at 13:03











  • It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

    – david0116
    Nov 21 '18 at 18:03











  • Did you add it before if session[:expires_at].nil? ?

    – razvans
    Nov 22 '18 at 8:52











  • Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

    – david0116
    Nov 22 '18 at 9:28
















  • 1





    Try setting request.session_options[:skip] = true

    – razvans
    Nov 21 '18 at 13:03











  • It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

    – david0116
    Nov 21 '18 at 18:03











  • Did you add it before if session[:expires_at].nil? ?

    – razvans
    Nov 22 '18 at 8:52











  • Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

    – david0116
    Nov 22 '18 at 9:28










1




1





Try setting request.session_options[:skip] = true

– razvans
Nov 21 '18 at 13:03





Try setting request.session_options[:skip] = true

– razvans
Nov 21 '18 at 13:03













It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

– david0116
Nov 21 '18 at 18:03





It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.

– david0116
Nov 21 '18 at 18:03













Did you add it before if session[:expires_at].nil? ?

– razvans
Nov 22 '18 at 8:52





Did you add it before if session[:expires_at].nil? ?

– razvans
Nov 22 '18 at 8:52













Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

– david0116
Nov 22 '18 at 9:28







Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.

– david0116
Nov 22 '18 at 9:28














1 Answer
1






active

oldest

votes


















1














it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?



you should find a way to be sure a variable you are using is persistent over more requests in parallel.






share|improve this answer
























  • Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

    – david0116
    Nov 21 '18 at 18:06











  • I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

    – david0116
    Nov 22 '18 at 6:28











  • Two steps validation works fine for now. Thanks for the help.

    – david0116
    Nov 22 '18 at 7:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406793%2fajax-request-speed-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?



you should find a way to be sure a variable you are using is persistent over more requests in parallel.






share|improve this answer
























  • Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

    – david0116
    Nov 21 '18 at 18:06











  • I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

    – david0116
    Nov 22 '18 at 6:28











  • Two steps validation works fine for now. Thanks for the help.

    – david0116
    Nov 22 '18 at 7:14
















1














it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?



you should find a way to be sure a variable you are using is persistent over more requests in parallel.






share|improve this answer
























  • Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

    – david0116
    Nov 21 '18 at 18:06











  • I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

    – david0116
    Nov 22 '18 at 6:28











  • Two steps validation works fine for now. Thanks for the help.

    – david0116
    Nov 22 '18 at 7:14














1












1








1







it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?



you should find a way to be sure a variable you are using is persistent over more requests in parallel.






share|improve this answer













it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?



you should find a way to be sure a variable you are using is persistent over more requests in parallel.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 10:18









devananddevanand

3,4401418




3,4401418













  • Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

    – david0116
    Nov 21 '18 at 18:06











  • I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

    – david0116
    Nov 22 '18 at 6:28











  • Two steps validation works fine for now. Thanks for the help.

    – david0116
    Nov 22 '18 at 7:14



















  • Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

    – david0116
    Nov 21 '18 at 18:06











  • I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

    – david0116
    Nov 22 '18 at 6:28











  • Two steps validation works fine for now. Thanks for the help.

    – david0116
    Nov 22 '18 at 7:14

















Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

– david0116
Nov 21 '18 at 18:06





Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.

– david0116
Nov 21 '18 at 18:06













I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

– david0116
Nov 22 '18 at 6:28





I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.

– david0116
Nov 22 '18 at 6:28













Two steps validation works fine for now. Thanks for the help.

– david0116
Nov 22 '18 at 7:14





Two steps validation works fine for now. Thanks for the help.

– david0116
Nov 22 '18 at 7:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406793%2fajax-request-speed-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?